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: "#{left}#{lines[line]}#{right}".align(alignment, width + @table.cell_padding) 62: 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: "#{left}#{lines[line]}#{right}".align(alignment, width + @table.cell_padding) 62: end
Returns the longest line in the cell and removes all ANSI escape sequences (e.g. color)
# 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)
# 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
# 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
# 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