Class: Terminal::Table::Cell
- Inherits:
-
Object
- Object
- Terminal::Table::Cell
- Defined in:
- lib/terminal-table/lib/terminal-table/cell.rb
Instance Attribute Summary collapse
-
#colspan ⇒ Object
readonly
Column span.
-
#value ⇒ Object
readonly
Cell value.
-
#width ⇒ Object
readonly
Returns the width of this cell.
Instance Method Summary collapse
- #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).
- #wrap(width) ⇒ Object
Constructor Details
#initialize(options = nil) ⇒ Cell
Initialize with options.
24 25 26 27 28 29 30 31 32 |
# File 'lib/terminal-table/lib/terminal-table/cell.rb', line 24 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.
19 20 21 |
# File 'lib/terminal-table/lib/terminal-table/cell.rb', line 19 def colspan @colspan end |
#value ⇒ Object (readonly)
Cell value.
14 15 16 |
# File 'lib/terminal-table/lib/terminal-table/cell.rb', line 14 def value @value end |
#width ⇒ Object (readonly)
Returns the width of this cell
9 10 11 |
# File 'lib/terminal-table/lib/terminal-table/cell.rb', line 9 def width @width end |
Instance Method Details
#alignment ⇒ Object
38 39 40 |
# File 'lib/terminal-table/lib/terminal-table/cell.rb', line 38 def alignment @alignment || @table.style.alignment || :left end |
#alignment=(val) ⇒ Object
42 43 44 45 46 47 48 49 |
# File 'lib/terminal-table/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? ⇒ Boolean
34 35 36 |
# File 'lib/terminal-table/lib/terminal-table/cell.rb', line 34 def alignment? !@alignment.nil? end |
#escape(line) ⇒ Object
removes all ANSI escape sequences (e.g. color)
91 92 93 94 95 |
# File 'lib/terminal-table/lib/terminal-table/cell.rb', line 91 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
51 52 53 |
# File 'lib/terminal-table/lib/terminal-table/cell.rb', line 51 def lines @value.to_s.split(/\n/) end |
#render(line = 0) ⇒ Object Also known as: to_s
Render the cell.
58 59 60 61 62 63 |
# File 'lib/terminal-table/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 |
#value_for_column_width_recalc ⇒ Object
Returns the longest line in the cell and removes all ANSI escape sequences (e.g. color)
70 71 72 |
# File 'lib/terminal-table/lib/terminal-table/cell.rb', line 70 def value_for_column_width_recalc lines.map{ |s| escape(s) }.max_by{ |s| s.size } end |
#wrap(width) ⇒ Object
85 86 87 |
# File 'lib/terminal-table/lib/terminal-table/cell.rb', line 85 def wrap(width) @value.gsub!(/(.{1,#{width}})( +|$\n?)|(.{1,#{width}})/, "\\1\\3\n") end |