Class: TTY::Table::Columns
- Inherits:
-
Object
- Object
- TTY::Table::Columns
- Defined in:
- lib/tty/table/columns.rb
Overview
A class responsible for enforcing column constraints
Constant Summary collapse
- MIN_WIDTH =
1
- BORDER_WIDTH =
1
Instance Attribute Summary collapse
-
#renderer ⇒ Object
readonly
Returns the value of attribute renderer.
-
#table ⇒ Object
readonly
Returns the value of attribute table.
Instance Method Summary collapse
-
#adjust_padding ⇒ Object
private
Adjust column widths to account for padding whitespace.
-
#assert_minimum_width ⇒ Object
private
Assert minimum width for the table content.
-
#border_size ⇒ Integer
Total border size.
-
#distribute_extra_width(widths) ⇒ Object
private
Distribute remaining width to meet the total width requirement.
-
#enforce ⇒ Object
Return the constrained column widths.
-
#expand ⇒ Object
private
Expand column widths to match the requested width.
-
#initialize(renderer) ⇒ Columns
constructor
Initialize a Columns.
-
#minimum_width ⇒ Integer
Estimate minimum table width to be able to display content.
-
#natural_width ⇒ Integer
Return column’s natural unconstrained widths.
-
#outside_border_size ⇒ Integer
Estimate outside border size.
-
#rotate ⇒ Object
private
Rotate table to vertical orientation and print information to stdout.
-
#shrink ⇒ Object
private
Shrink column widths to match the requested width.
Constructor Details
#initialize(renderer) ⇒ Columns
Initialize a Columns
22 23 24 25 |
# File 'lib/tty/table/columns.rb', line 22 def initialize(renderer) @renderer = renderer @table = renderer.table end |
Instance Attribute Details
#renderer ⇒ Object (readonly)
Returns the value of attribute renderer.
11 12 13 |
# File 'lib/tty/table/columns.rb', line 11 def renderer @renderer end |
#table ⇒ Object (readonly)
Returns the value of attribute table.
9 10 11 |
# File 'lib/tty/table/columns.rb', line 9 def table @table end |
Instance Method Details
#adjust_padding ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Adjust column widths to account for padding whitespace
89 90 91 92 93 94 95 96 |
# File 'lib/tty/table/columns.rb', line 89 def adjust_padding padding = renderer.padding column_size = table.column_size (0...column_size).reduce([]) do |lengths, col| lengths + [padding.left + renderer.column_widths[col] + padding.right] end end |
#assert_minimum_width ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Assert minimum width for the table content
139 140 141 142 143 144 145 146 |
# File 'lib/tty/table/columns.rb', line 139 def assert_minimum_width width = renderer.width if width <= minimum_width raise ResizeError, "Table's width is too small to contain the content " + "(min width #{minimum_width}, currently set #{width})" end end |
#border_size ⇒ Integer
Total border size
41 42 43 |
# File 'lib/tty/table/columns.rb', line 41 def border_size BORDER_WIDTH * (table.column_size - 1) + outside_border_size end |
#distribute_extra_width(widths) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Distribute remaining width to meet the total width requirement.
153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/tty/table/columns.rb', line 153 def distribute_extra_width(widths) column_size = table.column_size extra_width = renderer.width - (widths.reduce(:+) + border_size) per_field_width = extra_width / column_size remaining_width = extra_width % column_size extra = [1] * remaining_width + [0] * (column_size - remaining_width) widths.map.with_index do |width, index| width + per_field_width + extra[index] end end |
#enforce ⇒ Object
Return the constrained column widths. Account for table field widths and any user defined constraints on the table width.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/tty/table/columns.rb', line 67 def enforce assert_minimum_width if not renderer.padding.empty? renderer.column_widths = adjust_padding end if natural_width <= renderer.width renderer.column_widths = if renderer.resize else if renderer.resize renderer.column_widths = shrink else rotate renderer.column_widths = ColumnSet.widths_from(table) end end end |
#expand ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Expand column widths to match the requested width
111 112 113 114 115 116 117 118 119 |
# File 'lib/tty/table/columns.rb', line 111 def column_size = table.column_size ratio = ((renderer.width - natural_width) / column_size.to_f).floor widths = (0...column_size).reduce([]) do |lengths, col| lengths + [renderer.column_widths[col] + ratio] end distribute_extra_width(widths) end |
#minimum_width ⇒ Integer
Estimate minimum table width to be able to display content
50 51 52 |
# File 'lib/tty/table/columns.rb', line 50 def minimum_width table.column_size * MIN_WIDTH + border_size end |
#natural_width ⇒ Integer
Return column’s natural unconstrained widths
59 60 61 |
# File 'lib/tty/table/columns.rb', line 59 def natural_width renderer.column_widths.inject(0, &:+) + border_size end |
#outside_border_size ⇒ Integer
Estimate outside border size
32 33 34 |
# File 'lib/tty/table/columns.rb', line 32 def outside_border_size renderer.border_class == TTY::Table::Border::Null ? 0 : 2 end |
#rotate ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Rotate table to vertical orientation and print information to stdout
101 102 103 104 105 106 |
# File 'lib/tty/table/columns.rb', line 101 def rotate TTY.shell.warn 'The table size exceeds the currently set width.' + 'To avoid error either. Defaulting to vertical orientation.' table.orientation= :vertical table.rotate end |
#shrink ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Shrink column widths to match the requested width
124 125 126 127 128 129 130 131 132 |
# File 'lib/tty/table/columns.rb', line 124 def shrink column_size = table.column_size ratio = ((natural_width - renderer.width) / column_size.to_f).ceil widths = (0...column_size).reduce([]) do |lengths, col| lengths + [renderer.column_widths[col] - ratio] end distribute_extra_width(widths) end |