Class: Terminal::Table::Row

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

Direct Known Subclasses

Separator

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table, array = []) ⇒ Row

Initialize with width and options.



15
16
17
18
19
20
# File 'lib/glom/terminal-table/row.rb', line 15

def initialize table, array = []
  @cell_index = 0
  @table = table
  @cells = []
  array.each { |item| self << item }
end

Instance Attribute Details

#cellsObject (readonly)

Row cells



8
9
10
# File 'lib/glom/terminal-table/row.rb', line 8

def cells
  @cells
end

#tableObject (readonly)

Returns the value of attribute table.



10
11
12
# File 'lib/glom/terminal-table/row.rb', line 10

def table
  @table
end

Instance Method Details

#[](index) ⇒ Object



30
31
32
# File 'lib/glom/terminal-table/row.rb', line 30

def [] index
  cells[index]
end

#add_cell(item) ⇒ Object Also known as: <<



22
23
24
25
26
27
# File 'lib/glom/terminal-table/row.rb', line 22

def add_cell item
  options = item.is_a?(Hash) ? item : {:value => item}
  cell = Cell.new(options.merge(:index => @cell_index, :table => @table))
  @cell_index += cell.colspan
  @cells << cell
end

#heightObject



34
35
36
# File 'lib/glom/terminal-table/row.rb', line 34

def height
  cells.map { |c| c.lines.count }.max
end

#renderObject



43
44
45
46
47
48
49
50
# File 'lib/glom/terminal-table/row.rb', line 43

def render
  y = @table.style.border_y
  (0...height).to_a.map do |line|
    y + cells.map do |cell|
      cell.render(line)
    end.join(y) + y
  end.join("\n")
end

#wrap_cell(index, width) ⇒ Object



38
39
40
41
# File 'lib/glom/terminal-table/row.rb', line 38

def wrap_cell(index, width) 
  return if @cells.nil? or @cells[index].nil?
  @cells[index].wrap(width)
end