Class Terminal::Table::Cell
In: lib/terminal-table/cell.rb
lib/terminal-table/cell.rb
Parent: Object

Methods

Attributes

colspan  [R]  Column span.
colspan  [R]  Column span.
value  [R]  Cell value.
value  [R]  Cell value.
width  [R]  Cell width.
width  [R]  Cell width.

Public Class methods

Initialize with options.

[Source]

    # File lib/terminal-table/cell.rb, line 24
24:       def initialize options = nil
25:         @value, options = options, {} unless Hash === options
26:         @value = options.fetch :value, value
27:         @alignment = options.fetch :alignment, nil
28:         @colspan = options.fetch :colspan, 1
29:         @width = options.fetch :width, @value.to_s.size
30:         @index = options.fetch :index
31:         @table = options.fetch :table
32:       end

Initialize with options.

[Source]

    # File lib/terminal-table/cell.rb, line 24
24:       def initialize options = nil
25:         @value, options = options, {} unless Hash === options
26:         @value = options.fetch :value, value
27:         @alignment = options.fetch :alignment, nil
28:         @colspan = options.fetch :colspan, 1
29:         @width = options.fetch :width, @value.to_s.size
30:         @index = options.fetch :index
31:         @table = options.fetch :table
32:       end

Public Instance methods

[Source]

    # File lib/terminal-table/cell.rb, line 38
38:       def alignment
39:         @alignment || :left
40:       end

[Source]

    # File lib/terminal-table/cell.rb, line 38
38:       def alignment
39:         @alignment || :left
40:       end

[Source]

    # File lib/terminal-table/cell.rb, line 42
42:       def alignment=(val)
43:         supported = %w(left center right)
44:         if supported.include?(val.to_s)
45:           @alignment = val
46:         else
47:           raise "Aligment must be one of: #{supported.join(' ')}"
48:         end
49:       end

[Source]

    # File lib/terminal-table/cell.rb, line 42
42:       def alignment=(val)
43:         supported = %w(left center right)
44:         if supported.include?(val.to_s)
45:           @alignment = val
46:         else
47:           raise "Aligment must be one of: #{supported.join(' ')}"
48:         end
49:       end

[Source]

    # File lib/terminal-table/cell.rb, line 34
34:       def alignment?
35:         !@alignment.nil?
36:       end

[Source]

    # File lib/terminal-table/cell.rb, line 34
34:       def alignment?
35:         !@alignment.nil?
36:       end

[Source]

    # File lib/terminal-table/cell.rb, line 51
51:       def lines
52:         @value.to_s.split(/\n/)
53:       end

[Source]

    # File lib/terminal-table/cell.rb, line 51
51:       def lines
52:         @value.to_s.split(/\n/)
53:       end

Render the cell.

[Source]

    # File lib/terminal-table/cell.rb, line 58
58:       def render(line = 0)
59:         left = " " * @table.style.padding_left
60:         right = " " * @table.style.padding_right
61:         "#{left}#{lines[line]}#{right}".align(alignment, width + @table.cell_padding)
62:       end

Render the cell.

[Source]

    # File lib/terminal-table/cell.rb, line 58
58:       def render(line = 0)
59:         left = " " * @table.style.padding_left
60:         right = " " * @table.style.padding_right
61:         "#{left}#{lines[line]}#{right}".align(alignment, width + @table.cell_padding)
62:       end
to_s(line = 0)

Alias for render

to_s(line = 0)

Alias for render

Returns the longest line in the cell and removes all ANSI escape sequences (e.g. color)

[Source]

    # File lib/terminal-table/cell.rb, line 69
69:       def value_for_column_width_recalc
70:         str = lines.sort_by { |s| s.size }.last.to_s
71:         str = str.gsub(/\x1b(\[|\(|\))[;?0-9]*[0-9A-Za-z]/, '')
72:         str = str.gsub(/\x1b(\[|\(|\))[;?0-9]*[0-9A-Za-z]/, '')
73:         str.gsub(/[\x03|\x1a]/, '')
74:       end

Returns the longest line in the cell and removes all ANSI escape sequences (e.g. color)

[Source]

    # File lib/terminal-table/cell.rb, line 69
69:       def value_for_column_width_recalc
70:         str = lines.sort_by { |s| s.size }.last.to_s
71:         str = str.gsub(/\x1b(\[|\(|\))[;?0-9]*[0-9A-Za-z]/, '')
72:         str = str.gsub(/\x1b(\[|\(|\))[;?0-9]*[0-9A-Za-z]/, '')
73:         str.gsub(/[\x03|\x1a]/, '')
74:       end

Returns the width of this cell

[Source]

    # File lib/terminal-table/cell.rb, line 79
79:       def width
80:         padding = (colspan - 1) * @table.cell_spacing
81:         inner_width = (1..@colspan).to_a.inject(0) do |w, counter|
82:           w + @table.column_width(@index + counter - 1)
83:         end
84:         inner_width + padding
85:       end

Returns the width of this cell

[Source]

    # File lib/terminal-table/cell.rb, line 79
79:       def width
80:         padding = (colspan - 1) * @table.cell_spacing
81:         inner_width = (1..@colspan).to_a.inject(0) do |w, counter|
82:           w + @table.column_width(@index + counter - 1)
83:         end
84:         inner_width + padding
85:       end

[Validate]