Class: TTY::Table::Field
- Inherits:
-
Object
- Object
- TTY::Table::Field
- Includes:
- Equatable
- Defined in:
- lib/tty/table/field.rb
Overview
A class that represents a unique element in a table.
Instance Attribute Summary collapse
-
#align ⇒ Object
readonly
Returns the value of attribute align.
-
#colspan ⇒ Object
readonly
Number of columns this field spans.
-
#rowspan ⇒ Object
readonly
Number of rows this field spans.
-
#value ⇒ Object
The value inside the field.
-
#width ⇒ Object
readonly
The field value width.
Attributes included from Equatable
Instance Method Summary collapse
- #chars ⇒ Object
-
#height ⇒ Integer
Extract the number of lines this value spans.
-
#initialize(value) ⇒ Field
constructor
private
Initialize a Field.
-
#length ⇒ Integer
If the string contains unescaped new lines then the longest token deterimines the actual field length.
-
#lines ⇒ Array[String]
Return number of lines this value spans.
-
#render ⇒ Object
Render value inside this field box.
-
#to_s ⇒ Object
Return field value.
- #value_height ⇒ Object
-
#value_width ⇒ Object
Return the width this field would normally have bar other contraints.
Methods included from Equatable
#attr_reader, included, #inherited
Constructor Details
#initialize(value) ⇒ Field
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Initialize a Field
47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/tty/table/field.rb', line 47 def initialize(value) if value.class <= Hash = value @value = .fetch(:value) else @value = value = {} end @width = .fetch(:width) { @value.to_s.size } @align = .fetch(:align) { nil } @colspan = .fetch(:colspan) { 1 } @rowspan = .fetch(:rowspan) { 1 } end |
Instance Attribute Details
#align ⇒ Object (readonly)
Returns the value of attribute align.
31 32 33 |
# File 'lib/tty/table/field.rb', line 31 def align @align end |
#colspan ⇒ Object (readonly)
Number of columns this field spans. Defaults to 1.
25 26 27 |
# File 'lib/tty/table/field.rb', line 25 def colspan @colspan end |
#rowspan ⇒ Object (readonly)
Number of rows this field spans. Defaults to 1.
29 30 31 |
# File 'lib/tty/table/field.rb', line 29 def rowspan @rowspan end |
#value ⇒ Object
The value inside the field
13 14 15 |
# File 'lib/tty/table/field.rb', line 13 def value @value end |
#width ⇒ Object (readonly)
The field value width
21 22 23 |
# File 'lib/tty/table/field.rb', line 21 def width @width end |
Instance Method Details
#chars ⇒ Object
102 103 104 |
# File 'lib/tty/table/field.rb', line 102 def chars value.chars end |
#height ⇒ Integer
Extract the number of lines this value spans
98 99 100 |
# File 'lib/tty/table/field.rb', line 98 def height lines.size end |
#length ⇒ Integer
If the string contains unescaped new lines then the longest token deterimines the actual field length.
89 90 91 |
# File 'lib/tty/table/field.rb', line 89 def length lines.max_by(&:length).size end |
#lines ⇒ Array[String]
Return number of lines this value spans. A distinction is being made between escaped and non-escaped strings.
78 79 80 81 |
# File 'lib/tty/table/field.rb', line 78 def lines escaped = value.to_s.scan(/(\\n|\\t|\\r)/) escaped.empty? ? value.to_s.split(/\n/) : [value.to_s] end |
#render ⇒ Object
Render value inside this field box
109 110 |
# File 'lib/tty/table/field.rb', line 109 def render end |
#to_s ⇒ Object
Return field value
115 116 117 |
# File 'lib/tty/table/field.rb', line 115 def to_s value end |
#value_height ⇒ Object
68 69 70 |
# File 'lib/tty/table/field.rb', line 68 def value_height @height end |
#value_width ⇒ Object
Return the width this field would normally have bar other contraints
64 65 66 |
# File 'lib/tty/table/field.rb', line 64 def value_width @width end |