Class: TTY::Table::ColumnConstraint Private

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(table, renderer) ⇒ ColumnConstraint

Initialize a Columns

Parameters:



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

#rendererObject (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

#tableObject (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_sizeInteger

Total border size

Returns:

  • (Integer)


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

#enforceObject

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
      expand_column_widths
    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_widthInteger

Estimate minimum table width to be able to display content

Returns:

  • (Integer)


68
69
70
# File 'lib/tty/table/column_constraint.rb', line 68

def minimum_width
  table.columns_size * MIN_WIDTH + border_size
end

#natural_widthInteger

Return column’s natural unconstrained widths

Returns:

  • (Integer)


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_sizeInteger

Estimate outside border size

Returns:

  • (Integer)


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_sizeInteger

Measure total padding size for a table

Returns:

  • (Integer)


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