-
Notifications
You must be signed in to change notification settings - Fork 121
/
row.rb
73 lines (61 loc) · 1.5 KB
/
row.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
module Terminal
class Table
class Row
include Enumerable
##
# Row cells
attr_reader :cells
attr_reader :table
##
# Initialize with _width_ and _options_.
def initialize table, array
@table = table
@cells = if array == :separator
array
else
index = 0
array.map do |item|
options = item.is_a?(Hash) ? item : {:value => item, :index => index}
cell = Cell.new(options.merge(:index => index, :table => @table))
index += cell.colspan
cell
end
end
end
def [] index
cells[index] unless separator?
end
def []= index, value
cells[index] = value
end
def each &block
cells.each &block unless separator?
end
def height
cells.map { |c| c.lines.count }.max
end
def method_missing m, *args, &block
if cells.respond_to?(m)
cells.__send__(m, *args, &block)
else
super
end
end
def render
y = @table.style.border_y
if separator?
@table.separator
else
(0...height).to_a.map do |line|
y + cells.map do |cell|
cell.render(line)
end.join(y) + y
end.join("\n")
end
end
def separator?
cells == :separator
end
end
end
end