Class: TTY::Table::Columns Private

Inherits:
Object
  • Object
show all
Defined in:
lib/tty/table/columns.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(renderer) ⇒ Columns

Initialize a Columns

Parameters:



25
26
27
28
# File 'lib/tty/table/columns.rb', line 25

def initialize(renderer)
  @renderer = renderer
  @table    = renderer.table
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.



14
15
16
# File 'lib/tty/table/columns.rb', line 14

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.



12
13
14
# File 'lib/tty/table/columns.rb', line 12

def table
  @table
end

Instance Method Details

#adjust_paddingObject

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



94
95
96
97
98
99
100
101
# File 'lib/tty/table/columns.rb', line 94

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_widthObject

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

Raises:



145
146
147
148
149
150
# File 'lib/tty/table/columns.rb', line 145

def assert_minimum_width
  width = renderer.width
  return unless width <= minimum_width
  fail ResizeError, "Table's width is too small to contain the content " \
                    "(min width #{minimum_width}, currently set #{width})"
end

#border_sizeInteger

Total border size

Returns:

  • (Integer)


44
45
46
# File 'lib/tty/table/columns.rb', line 44

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.

Parameters:

  • widths (Array[Integer])


157
158
159
160
161
162
163
164
165
166
167
# File 'lib/tty/table/columns.rb', line 157

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

#enforceObject

Return the constrained column widths.

Account for table field widths and any user defined constraints on the table width.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/tty/table/columns.rb', line 72

def enforce
  assert_minimum_width

  unless renderer.padding.empty?
    renderer.column_widths = adjust_padding
  end

  if natural_width <= renderer.width
    renderer.column_widths = expand if renderer.resize
  else
    if renderer.resize
      renderer.column_widths = shrink
    else
      rotate
      renderer.column_widths = ColumnSet.widths_from(table)
    end
  end
end

#expandObject

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



117
118
119
120
121
122
123
124
125
# File 'lib/tty/table/columns.rb', line 117

def expand
  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_widthInteger

Estimate minimum table width to be able to display content

Returns:

  • (Integer)


53
54
55
# File 'lib/tty/table/columns.rb', line 53

def minimum_width
  table.column_size * MIN_WIDTH + border_size
end

#natural_widthInteger

Return column’s natural unconstrained widths

Returns:

  • (Integer)


62
63
64
# File 'lib/tty/table/columns.rb', line 62

def natural_width
  renderer.column_widths.inject(0, &:+) + border_size
end

#outside_border_sizeInteger

Estimate outside border size

Returns:

  • (Integer)


35
36
37
# File 'lib/tty/table/columns.rb', line 35

def outside_border_size
  renderer.border_class == TTY::Table::Border::Null ? 0 : 2
end

#rotateObject

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



106
107
108
109
110
111
112
# File 'lib/tty/table/columns.rb', line 106

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

#shrinkObject

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



130
131
132
133
134
135
136
137
138
# File 'lib/tty/table/columns.rb', line 130

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