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
The name for the value
@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 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
# File lib/tty/table/field.rb, line 142 def chars content.chars end
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
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
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
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 to original value
@api public
# File lib/tty/table/field.rb, line 98 def reset! @content = @value.to_s end
Return field content
@return [String]
@api public
# File lib/tty/table/field.rb, line 151 def to_s content end
The content width
@api public
# File lib/tty/table/field.rb, line 105 def width @width || Strings::Align.display_width(@content) end