Class: Terminal::Table::Cell
- Inherits:
-
Object
- Object
- Terminal::Table::Cell
- Defined in:
- lib/terminal-table/cell.rb
Instance Attribute Summary collapse
-
#colspan ⇒ Object
readonly
Column span.
-
#value ⇒ Object
readonly
Cell value.
Instance Method Summary collapse
- #align(val, position, length) ⇒ Object
- #alignment ⇒ Object
- #alignment=(val) ⇒ Object
- #alignment? ⇒ Boolean
-
#escape(line) ⇒ Object
removes all ANSI escape sequences (e.g. color).
-
#initialize(options = nil) ⇒ Cell
constructor
Initialize with options.
- #lines ⇒ Object
-
#render(line = 0) ⇒ Object
(also: #to_s)
Render the cell.
-
#value_for_column_width_recalc ⇒ Object
Returns the longest line in the cell and removes all ANSI escape sequences (e.g. color).
-
#width ⇒ Object
Returns the width of this cell.
Constructor Details
#initialize(options = nil) ⇒ Cell
Initialize with options.
17 18 19 20 21 22 23 24 25 |
# File 'lib/terminal-table/cell.rb', line 17 def initialize = nil @value, = , {} unless Hash === @value = .fetch :value, value @alignment = .fetch :alignment, nil @colspan = .fetch :colspan, 1 @width = .fetch :width, @value.to_s.size @index = .fetch :index @table = .fetch :table end |
Instance Attribute Details
#colspan ⇒ Object (readonly)
Column span.
12 13 14 |
# File 'lib/terminal-table/cell.rb', line 12 def colspan @colspan end |
#value ⇒ Object (readonly)
Cell value.
7 8 9 |
# File 'lib/terminal-table/cell.rb', line 7 def value @value end |
Instance Method Details
#align(val, position, length) ⇒ Object
44 45 46 47 |
# File 'lib/terminal-table/cell.rb', line 44 def align(val, position, length) positions = { :left => :ljust, :right => :rjust, :center => :center } val.public_send(positions[position], length) end |
#alignment ⇒ Object
31 32 33 |
# File 'lib/terminal-table/cell.rb', line 31 def alignment @alignment || @table.style.alignment || :left end |
#alignment=(val) ⇒ Object
35 36 37 38 39 40 41 42 |
# File 'lib/terminal-table/cell.rb', line 35 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? ⇒ Boolean
27 28 29 |
# File 'lib/terminal-table/cell.rb', line 27 def alignment? !@alignment.nil? end |
#escape(line) ⇒ Object
removes all ANSI escape sequences (e.g. color)
84 85 86 87 88 |
# File 'lib/terminal-table/cell.rb', line 84 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 ⇒ Object
48 49 50 |
# File 'lib/terminal-table/cell.rb', line 48 def lines @value.to_s.split(/\n/) end |
#render(line = 0) ⇒ Object Also known as: to_s
Render the cell.
55 56 57 58 59 60 |
# File 'lib/terminal-table/cell.rb', line 55 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 align("#{left}#{lines[line]}#{right}", alignment, render_width + @table.cell_padding) end |
#value_for_column_width_recalc ⇒ Object
Returns the longest line in the cell and removes all ANSI escape sequences (e.g. color)
67 68 69 |
# File 'lib/terminal-table/cell.rb', line 67 def value_for_column_width_recalc lines.map{ |s| escape(s) }.max_by{ |s| s.size } end |
#width ⇒ Object
Returns the width of this cell
74 75 76 77 78 79 80 |
# File 'lib/terminal-table/cell.rb', line 74 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 |