class Terminal::Table::Cell
Attributes
colspan[R]
Column span.
value[R]
Cell value.
width[R]
Cell width.
Public Class Methods
new(options = nil)
click to toggle source
Initialize with options.
# File lib/terminal-table/cell.rb, line 24 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
alignment()
click to toggle source
# File lib/terminal-table/cell.rb, line 38 def alignment @alignment || :left end
alignment=(val)
click to toggle source
# File lib/terminal-table/cell.rb, line 42 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 34 def alignment? !@alignment.nil? end
escape(line)
click to toggle source
removes all ANSI escape sequences (e.g. color)
# File lib/terminal-table/cell.rb, line 87 def escape(line) line.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
# File lib/terminal-table/cell.rb, line 51 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 58 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 "#{left}#{lines[line]}#{right}".align(alignment, render_width + @table.cell_padding) end
Also aliased as: to_s
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| escape(s) }.max_by{ |s| s.size } end