class AsciiParadise::AsciiTable::Cell

Attributes

colspan[R]

attr_reader :width # Cell width.

Public Class Methods

new(options = nil) click to toggle source
#

initialize

Initialize with options.

#
# File lib/ascii_paradise/asciitable/cell.rb, line 21
def initialize(options = nil)
  reset
  @value, options = options, {} unless Hash === options
  @value = options.fetch :value, value
  @alignment = options.fetch :alignment, nil
  @alignment = :center if @alignment == :middle
  @colspan = options.fetch :colspan, 1
  @width = options.fetch :width, @value.to_s.size
  @index = options.fetch :index
  @table = options.fetch :table
end

Public Instance Methods

alignment() click to toggle source
#

alignment

#
# File lib/ascii_paradise/asciitable/cell.rb, line 114
def alignment
  @alignment || :left
end
alignment=(i) click to toggle source
#

alignment=

#
# File lib/ascii_paradise/asciitable/cell.rb, line 50
def alignment=(i)
  supported_alignments = %w( left center right )
  case i
  when :middle
    i = :center
  end
  if supported_alignments.include?(i.to_s)
    @alignment = i
  else
    raise "Aligment must be one of: #{supported_alignments.join(' ')}"
  end
end
alignment?() click to toggle source
#

alignment?

#
# File lib/ascii_paradise/asciitable/cell.rb, line 107
def alignment?
  ! @alignment.nil?
end
escape(i) click to toggle source
#

escape

This method removes all ANSI escape sequences (e.g. color).

#
# File lib/ascii_paradise/asciitable/cell.rb, line 98
def escape(i)
  i.to_s.gsub(/\x1b(\[|\(|\))[;?0-9]*[0-9A-Za-z]/, '').
         gsub(/\x1b(\[|\(|\))[;?0-9]*[0-9A-Za-z]/, '').
         gsub(/[\x03|\x1a]/, '')
end
lines() click to toggle source
#

lines

#
# File lib/ascii_paradise/asciitable/cell.rb, line 66
def lines
  @value.to_s.split(/\n/)
end
render(line = 0) click to toggle source
#

render

Render the cell.

alignment can be :left or :right or :center.

#
# File lib/ascii_paradise/asciitable/cell.rb, line 125
def render(line = 0)
  left  = ' ' * @table.style.padding_left
  right = ' ' * @table.style.padding_right
  render_width = lines[line].to_s.size - escape(lines[line]).size + width
  line = "#{left}#{lines[line]}#{right}"
  horizontal_length = render_width + @table.cell_padding
  case alignment
  when :left
    line.ljust(horizontal_length)
  when :right
    line.rjust(horizontal_length)
  when :center
    line.center(horizontal_length)
  end
end
Also aliased as: to_s
reset() click to toggle source
#

reset

#
# File lib/ascii_paradise/asciitable/cell.rb, line 36
def reset
  @value = 0
end
to_s(line = 0)
Alias for: render
value()
Alias for: value?
value?() click to toggle source
#

value?

#
# File lib/ascii_paradise/asciitable/cell.rb, line 43
def value?
  @value
end
Also aliased as: value
value_for_column_width_recalc() click to toggle source
#

value_for_column_width_recalc

Returns the longest line in the cell and removes all ANSI escape sequences (e.g. color).

#
# File lib/ascii_paradise/asciitable/cell.rb, line 76
def value_for_column_width_recalc
  lines.map{ |s| escape(s) }.max_by{ |s| s.size }
end
width() click to toggle source
#

width

Returns the width of this cell

#
# File lib/ascii_paradise/asciitable/cell.rb, line 85
def width
  padding = (colspan - 1) * @table.cell_spacing
  inner_width = (1..@colspan).to_a.inject(0) { |w, counter|
    w + @table.column_width(@index + counter - 1)
  }
  return inner_width + padding
end