Class: RubyRich::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_rich/table.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#alignObject

Returns the value of attribute align.



5
6
7
# File 'lib/ruby_rich/table.rb', line 5

def align
  @align
end

#headersObject

Returns the value of attribute headers.



5
6
7
# File 'lib/ruby_rich/table.rb', line 5

def headers
  @headers
end

#row_heightObject

Returns the value of attribute row_height.



5
6
7
# File 'lib/ruby_rich/table.rb', line 5

def row_height
  @row_height
end

#rowsObject

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

#renderObject



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