class TTY::Table::Field

A class that represents a unique element in a table.

Used internally by {Table::Header} and {Table::Row} to define internal structure.

@api private

Attributes

alignment[R]

The field alignment

@api public

colspan[R]

Number of columns this field spans. Defaults to 1.

@api public

content[RW]

The formatted value inside the field used for display

@api public

name[R]

The name for the value

@api public

rowspan[R]

Number of rows this field spans. Defaults to 1.

@api public

value[RW]

The value inside the field

@api public

Public Class Methods

new(value) click to toggle source

Initialize a Field

@example

field = TTY::Table::Field.new 'a1'
field.value  # => a1

@example

field = TTY::Table::Field.new value: 'a1'
field.value  # => a1

@example

field = TTY::Table::Field.new value: 'a1', alignment: :center
field.value     # => a1
field.alignment # => :center

@api private

# File lib/tty/table/field.rb, line 73
def initialize(value)
  @value, options = extract_options(value)
  @content = @value.to_s
  @width   = options[:width]
  @alignment = options.fetch(:alignment) { nil }
  @colspan = options.fetch(:colspan) { 1 }
  @rowspan = options.fetch(:rowspan) { 1 }
end

Public Instance Methods

chars() click to toggle source
# File lib/tty/table/field.rb, line 142
def chars
  content.chars
end
extract_options(value) click to toggle source

Extract options and set value

@api private

# File lib/tty/table/field.rb, line 85
def extract_options(value)
  if value.is_a?(Hash)
    options = value
    value = options.fetch(:value)
  else
    options = {}
  end
  [value, options]
end
height() click to toggle source

Extract the number of lines this value spans

@return [Integer]

@api public

# File lib/tty/table/field.rb, line 138
def height
  lines.size
end
length() click to toggle source

If the string contains unescaped new lines then the longest token deterimines the actual field length.

@return [Integer]

@api public

# File lib/tty/table/field.rb, line 127
def length
  (lines.map do |line|
    Strings::Align.display_width(line)
  end << 0).max
end
lines() click to toggle source

Return number of lines this value spans.

A distinction is being made between escaped and non-escaped strings.

@return [Array]

@api public

# File lib/tty/table/field.rb, line 116
def lines
  escaped = content.scan(/(\\n|\\t|\\r)/)
  escaped.empty? ? content.split(/\n/, -1) : [content]
end
reset!() click to toggle source

Reset to original value

@api public

# File lib/tty/table/field.rb, line 98
def reset!
  @content = @value.to_s
end
to_s() click to toggle source

Return field content

@return [String]

@api public

# File lib/tty/table/field.rb, line 151
def to_s
  content
end
width() click to toggle source

The content width

@api public

# File lib/tty/table/field.rb, line 105
def width
  @width || Strings::Align.display_width(@content)
end