Class: IO::Table

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

Overview

Table Component.

Constant Summary collapse

BORDER =

the table border character.

{
  sep: '',
  line: '-',
  top: ['', '', ''],
  mid: ['', '', ''],
  bot: ['', '', '']
}.freeze

Instance Method Summary collapse

Constructor Details

#initializeTable

Returns a new instance of Table.



18
19
20
21
22
# File 'lib/term/table.rb', line 18

def initialize
  @data = []
  @size = []
  @column_width = []
end

Instance Method Details

#<<(row) ⇒ self

add row to the table

Parameters:

  • row (Array)

    the row to the table

Returns:

  • (self)

    table self



28
29
30
31
32
# File 'lib/term/table.rb', line 28

def <<(row)
  @data << row.map(&:to_s)
  @size << row.map { |cell| cell.to_s.display_width }
  self
end

#to_sString

display table.

Returns:



37
38
39
40
41
42
43
# File 'lib/term/table.rb', line 37

def to_s
  @column_width = calc_column_width
  res = get_border_row(BORDER[:top])
  res << @data.map { |row| get_content_row(row, BORDER[:sep]) }
         .join(get_border_row(BORDER[:mid]))
  res << get_border_row(BORDER[:bot])
end