class TTY::Table::ColumnConstraint

A class responsible for enforcing column constraints.

Used internally by {Renderer::Basic} to enforce correct column widths.

@api private

Constants

BORDER_WIDTH
MIN_WIDTH

Attributes

renderer[R]
table[R]

Public Class Methods

new(table, renderer) click to toggle source

Initialize a Columns

@param [TTY::Table] table

@param [TTY::Table::Renderer] renderer

@api public

# File lib/tty/table/column_constraint.rb, line 29
def initialize(table, renderer)
  @table    = table
  @renderer = renderer
end

Public Instance Methods

border_size() click to toggle source

Total border size

@return [Integer]

@api public

# File lib/tty/table/column_constraint.rb, line 48
def border_size
  BORDER_WIDTH * (table.columns_size - 1) + outside_border_size
end
enforce() click to toggle source

Return the constrained column widths.

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

@api public

# File lib/tty/table/column_constraint.rb, line 86
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_width() click to toggle source

Estimate minimum table width to be able to display content

@return [Integer]

@api public

# File lib/tty/table/column_constraint.rb, line 67
def minimum_width
  table.columns_size * MIN_WIDTH + border_size
end
natural_width() click to toggle source

Return column's natural unconstrained widths

@return [Integer]

@api public

# File lib/tty/table/column_constraint.rb, line 76
def natural_width
  renderer.column_widths.inject(0, &:+) + border_size + padding_size
end
outside_border_size() click to toggle source

Estimate outside border size

@return [Integer]

@api public

# File lib/tty/table/column_constraint.rb, line 39
def outside_border_size
  renderer.border_class == TTY::Table::Border::Null ? 0 : 2
end
padding_size() click to toggle source

Measure total padding size for a table

@return [Integer]

@api public

# File lib/tty/table/column_constraint.rb, line 57
def padding_size
  padding = renderer.padding
  (padding.left + padding.right) * table.columns_count
end