| Class | Terminal::Table::Cell |
| In: |
lib/terminal-table/cell.rb
lib/terminal-table/cell.rb |
| Parent: | Object |
Initialize with options.
# 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.
# 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
# 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
# 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
Render the cell.
# 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.
# 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
Returns the width of this cell
# 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
# 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