Class: Terminal::Table::Cell

Inherits:
Object
  • Object
show all
Defined in:
lib/terminal-table/cell.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ Cell

Initialize with options.



19
20
21
22
23
24
25
26
27
# File 'lib/terminal-table/cell.rb', line 19

def initialize options = nil
  @value, options = options, {} unless Hash === options
  @value = options.fetch :value, value
  @alignment = options.fetch :alignment, nil
  @colspan = options.fetch :colspan, 1
  @width = options.fetch :width, @value.to_s.size
  @index = options.fetch :index
  @table = options.fetch :table
end

Instance Attribute Details

#colspanObject (readonly)

Column span.



14
15
16
# File 'lib/terminal-table/cell.rb', line 14

def colspan
  @colspan
end

#valueObject (readonly)

Cell value.



9
10
11
# File 'lib/terminal-table/cell.rb', line 9

def value
  @value
end

Instance Method Details

#align(val, position, length) ⇒ Object



46
47
48
49
# File 'lib/terminal-table/cell.rb', line 46

def align(val, position, length)
  positions = { :left => :ljust, :right => :rjust, :center => :center }
  val.public_send(positions[position], length)
end

#alignmentObject



33
34
35
# File 'lib/terminal-table/cell.rb', line 33

def alignment
  @alignment || @table.style.alignment || :left
end

#alignment=(val) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/terminal-table/cell.rb', line 37

def alignment=(val)
  supported = %w(left center right)
  if supported.include?(val.to_s)
    @alignment = val
  else
    raise "Aligment must be one of: #{supported.join(' ')}"
  end
end

#alignment?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/terminal-table/cell.rb', line 29

def alignment?
  !@alignment.nil?
end

#escape(line) ⇒ Object

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



87
88
89
90
91
# File 'lib/terminal-table/cell.rb', line 87

def escape(line)
  line.to_s.gsub(/\x1b(\[|\(|\))[;?0-9]*[0-9A-Za-z]/, '').
    gsub(/\x1b(\[|\(|\))[;?0-9]*[0-9A-Za-z]/, '').
    gsub(/(\x03|\x1a)/, '')
end

#linesObject



50
51
52
# File 'lib/terminal-table/cell.rb', line 50

def lines
  @value.to_s.split(/\n/)
end

#render(line = 0) ⇒ Object Also known as: to_s

Render the cell.



57
58
59
60
61
62
63
# File 'lib/terminal-table/cell.rb', line 57

def render(line = 0)
  left = " " * @table.style.padding_left
  right = " " * @table.style.padding_right
  display_width = Unicode::DisplayWidth.of(escape(lines[line]))
  render_width = lines[line].to_s.size - display_width + width
  align("#{left}#{lines[line]}#{right}", alignment, render_width + @table.cell_padding)
end

#value_for_column_width_recalcObject

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



70
71
72
# File 'lib/terminal-table/cell.rb', line 70

def value_for_column_width_recalc
  lines.map{ |s| escape(s) }.max_by{ |s| Unicode::DisplayWidth.of(s) }
end

#widthObject

Returns the width of this cell



77
78
79
80
81
82
83
# File 'lib/terminal-table/cell.rb', line 77

def width
  padding = (colspan - 1) * @table.cell_spacing
  inner_width = (1..@colspan).to_a.inject(0) do |w, counter|
    w + @table.column_width(@index + counter - 1)
  end
  inner_width + padding
end