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
The field alignment
@api public
Number of columns this field spans. Defaults to 1.
@api public
The formatted value inside the field used for display
@api public
Number of rows this field spans. Defaults to 1.
@api public
The value inside the field
@api public
Public Class Methods
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
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
# File lib/tty/table/field.rb, line 124 def chars content.chars end
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 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 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
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 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
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
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 to original value
@api public
# File lib/tty/table/field.rb, line 80 def reset! @content = @value.to_s end
Return field content
@return [String]
@api public
# File lib/tty/table/field.rb, line 133 def to_s content end
The content width
@api public
# File lib/tty/table/field.rb, line 87 def width @width || Strings::Align.display_width(@content) end