Class: Achoo::Term::Table
- Inherits:
-
Object
- Object
- Achoo::Term::Table
- Defined in:
- lib/achoo/term/table.rb
Instance Method Summary collapse
-
#initialize(headers, data_rows, summaries = nil) ⇒ Table
constructor
A new instance of Table.
- #print(io = $stdout) ⇒ Object
Constructor Details
#initialize(headers, data_rows, summaries = nil) ⇒ Table
Returns a new instance of Table.
33 34 35 36 37 |
# File 'lib/achoo/term/table.rb', line 33 def initialize(headers, data_rows, summaries=nil) @headers = headers @data_rows = data_rows @summaries = summaries end |
Instance Method Details
#print(io = $stdout) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/achoo/term/table.rb', line 39 def print(io=$stdout) lengths = calculate_table_cell_widths format = build_format(lengths) headers = center_table_headers(lengths) separator = lengths.map {|length| '─'*(length+2)} io.print '┌' << separator.join('┬') << "┐\n" io.print '│ ' << headers.join(' │ ') << " │\n" io.print '├' << separator.join('┼') << "┤\n" @data_rows.each {|r| io.printf format, *r } unless @summaries.nil? || @data_rows.length == 1 io.print '├' << separator.join('┼') << "┤\n" io.printf format, *@summaries end io.print '└' << separator.join('┴') << "┘\n" end |