/
row.rb
72 lines (63 loc) · 2.16 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
module CommandLineReporter
class Row
include OptionsValidator
VALID_OPTIONS = [:header, :color, :bold]
attr_accessor :columns, :border, *VALID_OPTIONS
def initialize(options = {})
self.validate_options(options, *VALID_OPTIONS)
self.columns = []
self.border = false
self.header = options[:header] || false
self.color = options[:color]
self.bold = options[:bold] || false
end
def add(column)
if column.color.nil? && self.color
column.color = self.color
end
if self.bold || self.header
column.bold = true
end
self.columns << column
end
def output
screen_count.times do |sr|
border_char = ("\u2501" == "u2501") ? '|' : "\u2503"
line = (self.border) ? "#{border_char} " : ''
self.columns.size.times do |mc|
col = self.columns[mc]
# Account for the fact that some columns will have more screen rows than their
# counterparts in the row. An example being:
# c1 = Column.new('x' * 50, :width => 10)
# c2 = Column.new('x' * 20, :width => 10)
#
# c1.screen_rows.size == 5
# c2.screen_rows.size == 2
#
# So when we don't have a screen row for c2 we need to fill the screen with the
# proper number of blanks so the layout looks like (parenthesis on the right just
# indicate screen row index)
#
# +-------------+------------+
# | xxxxxxxxxxx | xxxxxxxxxx | (0)
# | xxxxxxxxxxx | xxxxxxxxxx | (1)
# | xxxxxxxxxxx | | (2)
# | xxxxxxxxxxx | | (3)
# | xxxxxxxxxxx | | (4)
# +-------------+------------+
if col.screen_rows[sr].nil?
line << ' ' * col.width
else
line << self.columns[mc].screen_rows[sr]
end
line << ' ' + ((self.border) ? "#{border_char} " : '')
end
puts line
end
end
private
def screen_count
@sc ||= self.columns.inject(0) {|max,column| column.screen_rows.size > max ? column.screen_rows.size : max}
end
end
end