Class: RubyRich::Table
- Inherits:
-
Object
- Object
- RubyRich::Table
- Defined in:
- lib/ruby_rich/table.rb
Instance Attribute Summary collapse
-
#align ⇒ Object
Returns the value of attribute align.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#row_height ⇒ Object
Returns the value of attribute row_height.
-
#rows ⇒ Object
Returns the value of attribute rows.
Instance Method Summary collapse
- #add_row(row) ⇒ Object
-
#initialize(headers: [], align: :left, row_height: 1) ⇒ Table
constructor
A new instance of Table.
- #render ⇒ Object
Constructor Details
#initialize(headers: [], align: :left, row_height: 1) ⇒ Table
Returns a new instance of Table.
7 8 9 10 11 12 |
# File 'lib/ruby_rich/table.rb', line 7 def initialize(headers: [], align: :left, row_height: 1) @headers = headers.map { |h| format_cell(h) } @rows = [] @align = align @row_height = row_height end |
Instance Attribute Details
#align ⇒ Object
Returns the value of attribute align.
5 6 7 |
# File 'lib/ruby_rich/table.rb', line 5 def align @align end |
#headers ⇒ Object
Returns the value of attribute headers.
5 6 7 |
# File 'lib/ruby_rich/table.rb', line 5 def headers @headers end |
#row_height ⇒ Object
Returns the value of attribute row_height.
5 6 7 |
# File 'lib/ruby_rich/table.rb', line 5 def row_height @row_height end |
#rows ⇒ Object
Returns the value of attribute rows.
5 6 7 |
# File 'lib/ruby_rich/table.rb', line 5 def rows @rows end |
Instance Method Details
#add_row(row) ⇒ Object
14 15 16 |
# File 'lib/ruby_rich/table.rb', line 14 def add_row(row) @rows << row.map { |cell| format_cell(cell) } end |
#render ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/ruby_rich/table.rb', line 18 def render column_widths = calculate_column_widths lines = [] # Render headers lines << render_row(@headers, column_widths, bold: true) lines << "-" * (column_widths.sum { |w| w + 3 } + 1) # Render rows @rows.each do |row| lines.concat(render_multiline_row(row, column_widths)) end lines.join("\n") end |