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

removes all ANSI escape sequences (e.g. color)

[Source]

    # File lib/terminal-table/cell.rb, line 87
87:       def escape(line)
88:         line.to_s.gsub(/\x1b(\[|\(|\))[;?0-9]*[0-9A-Za-z]/, '').
89:           gsub(/\x1b(\[|\(|\))[;?0-9]*[0-9A-Za-z]/, '').
90:           gsub(/[\x03|\x1a]/, '')
91:       end

removes all ANSI escape sequences (e.g. color)

[Source]

    # File lib/terminal-table/cell.rb, line 87
87:       def escape(line)
88:         line.to_s.gsub(/\x1b(\[|\(|\))[;?0-9]*[0-9A-Za-z]/, '').
89:           gsub(/\x1b(\[|\(|\))[;?0-9]*[0-9A-Za-z]/, '').
90:           gsub(/[\x03|\x1a]/, '')
91:       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:         render_width = lines[line].to_s.size - escape(lines[line]).size + width
62:         "#{left}#{lines[line]}#{right}".align(alignment, render_width + @table.cell_padding)
63:       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:         render_width = lines[line].to_s.size - escape(lines[line]).size + width
62:         "#{left}#{lines[line]}#{right}".align(alignment, render_width + @table.cell_padding)
63:       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 70
70:       def value_for_column_width_recalc
71:         lines.map{ |s| escape(s) }.max_by{ |s| s.size }
72:       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 70
70:       def value_for_column_width_recalc
71:         lines.map{ |s| escape(s) }.max_by{ |s| s.size }
72:       end

Returns the width of this cell

[Source]

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

Returns the width of this cell

[Source]

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

[Validate]