Class: TTY::Table::ColumnConstraint Private
- Inherits:
-
Object
- Object
- TTY::Table::ColumnConstraint
- Defined in:
- lib/tty/table/column_constraint.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
A class responsible for enforcing column constraints.
Used internally by Renderer::Basic to enforce correct column widths.
Constant Summary collapse
- MIN_WIDTH =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
1
- BORDER_WIDTH =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
1
Instance Attribute Summary collapse
- #renderer ⇒ Object readonly private
- #table ⇒ Object readonly private
Instance Method Summary collapse
-
#border_size ⇒ Integer
Total border size.
-
#enforce ⇒ Object
Return the constrained column widths.
-
#initialize(table, renderer) ⇒ ColumnConstraint
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.
-
#padding_size ⇒ Integer
Measure total padding size for a table.
Constructor Details
#initialize(table, renderer) ⇒ ColumnConstraint
Initialize a Columns
30 31 32 33 |
# File 'lib/tty/table/column_constraint.rb', line 30 def initialize(table, renderer) @table = table @renderer = renderer end |
Instance Attribute Details
#renderer ⇒ Object (readonly)
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.
21 22 23 |
# File 'lib/tty/table/column_constraint.rb', line 21 def renderer @renderer end |
#table ⇒ Object (readonly)
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.
19 20 21 |
# File 'lib/tty/table/column_constraint.rb', line 19 def table @table end |
Instance Method Details
#border_size ⇒ Integer
Total border size
49 50 51 |
# File 'lib/tty/table/column_constraint.rb', line 49 def border_size BORDER_WIDTH * (table.columns_size - 1) + outside_border_size end |
#enforce ⇒ Object
Return the constrained column widths.
Account for table field widths and any user defined constraints on the table width.
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/tty/table/column_constraint.rb', line 87 def enforce assert_minimum_width padding = renderer.padding if natural_width <= renderer.width if renderer.resize else renderer.column_widths.map do |width| padding.left + width + padding.right end end else if renderer.resize shrink else rotate end end end |
#minimum_width ⇒ Integer
Estimate minimum table width to be able to display content
68 69 70 |
# File 'lib/tty/table/column_constraint.rb', line 68 def minimum_width table.columns_size * MIN_WIDTH + border_size end |
#natural_width ⇒ Integer
Return column’s natural unconstrained widths
77 78 79 |
# File 'lib/tty/table/column_constraint.rb', line 77 def natural_width renderer.column_widths.inject(0, &:+) + border_size + padding_size end |
#outside_border_size ⇒ Integer
Estimate outside border size
40 41 42 |
# File 'lib/tty/table/column_constraint.rb', line 40 def outside_border_size renderer.border_class == TTY::Table::Border::Null ? 0 : 2 end |
#padding_size ⇒ Integer
Measure total padding size for a table
58 59 60 61 |
# File 'lib/tty/table/column_constraint.rb', line 58 def padding_size padding = renderer.padding (padding.left + padding.right) * table.columns_count end |