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

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 55
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

==(other) click to toggle source

Compare fields for equivalence of value attribute

@return [Boolean]

@api public

# File lib/tty/table/field.rb, line 151
def ==(other)
  other.is_a?(self.class) && value == other.value
end
chars() click to toggle source
# File lib/tty/table/field.rb, line 124
def chars
  content.chars
end
eql?(other) click to toggle source

Compare fields for equality of value attribute

@return [Boolean]

@api public

# File lib/tty/table/field.rb, line 142
def eql?(other)
  instance_of?(other.class) && value.eql?(other.value)
end
extract_options(value) click to toggle source

Extract options and set value

@api private

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

Hash for this instance and its attributes

@return [Numeric]

@api public

# File lib/tty/table/field.rb, line 170
def hash
  [self.class, value].hash
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 120
def height
  lines.size
end
inspect() click to toggle source

Inspect this instance attributes

@return [String]

@api public

# File lib/tty/table/field.rb, line 160
def inspect
  "#<#{self.class.name} value=#{value.inspect} " \
    "rowspan=#{rowspan.inspect} colspan=#{colspan.inspect}>"
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 109
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 98
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 80
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 133
def to_s
  content
end
width() click to toggle source

The content width

@api public

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