Class: VisualWidth::Table
- Inherits:
-
Object
- Object
- VisualWidth::Table
- Defined in:
- lib/visual_width/table.rb
Constant Summary collapse
- LEFT =
align (left, right, center)
-> (cell, fill) { cell + (' ' * fill) }
- RIGHT =
-> (cell, fill) { (' ' * fill) + cell }
- CENTER =
-> (cell, fill) { half = fill / 2.0 (' ' * half.floor) + cell + (' ' * half.ceil) }
Instance Attribute Summary collapse
-
#footer ⇒ Object
Returns the value of attribute footer.
-
#format ⇒ Object
Returns the value of attribute format.
-
#header ⇒ Object
Returns the value of attribute header.
Instance Method Summary collapse
- #draw(rows, header: nil, output: "") ⇒ Object
-
#initialize(header: nil, format: []) ⇒ Table
constructor
A new instance of Table.
Constructor Details
#initialize(header: nil, format: []) ⇒ Table
Returns a new instance of Table.
20 21 22 23 |
# File 'lib/visual_width/table.rb', line 20 def initialize(header: nil, format: []) @header = header @format = format end |
Instance Attribute Details
#footer ⇒ Object
Returns the value of attribute footer.
4 5 6 |
# File 'lib/visual_width/table.rb', line 4 def @footer end |
#format ⇒ Object
Returns the value of attribute format.
4 5 6 |
# File 'lib/visual_width/table.rb', line 4 def format @format end |
#header ⇒ Object
Returns the value of attribute header.
4 5 6 |
# File 'lib/visual_width/table.rb', line 4 def header @header end |
Instance Method Details
#draw(rows, header: nil, output: "") ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/visual_width/table.rb', line 25 def draw(rows, header: nil, output: "") max_widths = calc_max_widths(rows) h = header || @header if h max_widths = h.map { |cell| VisualWidth.measure(cell) } .zip(max_widths) .map { |values| values.max } format_header = [CENTER] * h.length else format_header = nil end draw_row(output, max_widths, format_header, h, separated: true) rows.each do |row| draw_row(output, max_widths, @format, row) end line(output, max_widths) output end |