class Terminal::Table::Cell

Attributes

colspan[R]

Column span.

value[R]

Cell value.

Public Class Methods

new(options = nil) click to toggle source

Initialize with options.

# File lib/terminal-table/cell.rb, line 19
def initialize options = nil
  @value, options = options, {} unless Hash === options
  @value = options.fetch :value, value
  @alignment = options.fetch :alignment, nil
  @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

align(val, position, length) click to toggle source
# File lib/terminal-table/cell.rb, line 46
def align(val, position, length)
  positions = { :left => :ljust, :right => :rjust, :center => :center }
  val.public_send(positions[position], length)
end
alignment() click to toggle source
# File lib/terminal-table/cell.rb, line 33
def alignment
  @alignment || @table.style.alignment || :left
end
alignment=(val) click to toggle source
# File lib/terminal-table/cell.rb, line 37
def alignment=(val)
  supported = %w(left center right)
  if supported.include?(val.to_s)
    @alignment = val
  else
    raise "Aligment must be one of: #{supported.join(' ')}"
  end
end
alignment?() click to toggle source
# File lib/terminal-table/cell.rb, line 29
def alignment?
  !@alignment.nil?
end
inspect() click to toggle source
# File lib/terminal-table/cell.rb, line 85
def inspect
  fields = %i[alignment colspan index value width].map do |name|
    val = self.instance_variable_get('@'+name.to_s)
    "@#{name}=#{val.inspect}"
  end.join(', ')
  return "#<#{self.class} #{fields}>"
end
lines() click to toggle source
# File lib/terminal-table/cell.rb, line 50
def lines
  @value.to_s.split(/\n/)
end
render(line = 0) click to toggle source

Render the cell.

# File lib/terminal-table/cell.rb, line 57
def render(line = 0)
  left = " " * @table.style.padding_left
  right = " " * @table.style.padding_right
  display_width = Unicode::DisplayWidth.of(Util::ansi_escape(lines[line]))
  render_width = lines[line].to_s.size - display_width + width
  align("#{left}#{lines[line]}#{right}", alignment, render_width + @table.cell_padding)
end
Also aliased as: to_s
to_s(line = 0)
Alias for: render
value_for_column_width_recalc() click to toggle source

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

# File lib/terminal-table/cell.rb, line 70
def value_for_column_width_recalc
  lines.map{ |s| Util::ansi_escape(s) }.max_by{ |s| Unicode::DisplayWidth.of(s) }
end
width() click to toggle source

Returns the width of this cell

# File lib/terminal-table/cell.rb, line 77
def width
  padding = (colspan - 1) * @table.cell_spacing
  inner_width = (1..@colspan).to_a.inject(0) do |w, counter|
    w + @table.column_width(@index + counter - 1)
  end
  inner_width + padding
end