Class: TTY::Table::Field

Inherits:
Object
  • Object
show all
Includes:
Equatable
Defined in:
lib/tty/table/field.rb

Overview

A class that represents a unique element in a table.

Instance Attribute Summary collapse

Attributes included from Equatable

#comparison_attrs

Instance Method Summary collapse

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

Examples:

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

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

field = new TTY::Table::Field {:value => 'a1', :align => :center}
field.value  # => a1
field.align  # => :center


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
    options = value
    @value = options.fetch(:value)
  else
    @value = value
    options = {}
  end
  @width   = options.fetch(:width) { @value.to_s.size }
  @align   = options.fetch(:align) { nil }
  @colspan = options.fetch(:colspan) { 1 }
  @rowspan = options.fetch(:rowspan) { 1 }
end

Instance Attribute Details

#alignObject (readonly)

Returns the value of attribute align.



31
32
33
# File 'lib/tty/table/field.rb', line 31

def align
  @align
end

#colspanObject (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

#rowspanObject (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

#valueObject

The value inside the field



13
14
15
# File 'lib/tty/table/field.rb', line 13

def value
  @value
end

#widthObject (readonly)

The field value width



21
22
23
# File 'lib/tty/table/field.rb', line 21

def width
  @width
end

Instance Method Details

#charsObject



102
103
104
# File 'lib/tty/table/field.rb', line 102

def chars
  value.chars
end

#heightInteger

Extract the number of lines this value spans

Returns:

  • (Integer)


98
99
100
# File 'lib/tty/table/field.rb', line 98

def height
  lines.size
end

#lengthInteger

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

Returns:

  • (Integer)


89
90
91
# File 'lib/tty/table/field.rb', line 89

def length
  lines.max_by(&:length).size
end

#linesArray[String]

Return number of lines this value spans. A distinction is being made between escaped and non-escaped strings.

Returns:

  • (Array[String])


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

#renderObject

Render value inside this field box



109
110
# File 'lib/tty/table/field.rb', line 109

def render
end

#to_sObject

Return field value



115
116
117
# File 'lib/tty/table/field.rb', line 115

def to_s
  value
end

#value_heightObject



68
69
70
# File 'lib/tty/table/field.rb', line 68

def value_height
  @height
end

#value_widthObject

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