Class: TablePrint
Constant Summary
collapse
- DELIM =
'|'
Instance Method Summary
collapse
#config_dir, #config_file, #mkdir, #param, #user_file
Constructor Details
#initialize(*titles) ⇒ TablePrint
Returns a new instance of TablePrint.
18
19
20
21
22
23
24
25
26
27
28
|
# File 'lib/table_print.rb', line 18
def initialize(*titles)
@ncols = titles.length
@lines = [ ]
@width = nil
@lines << titles
separator = [ ]
titles.each { |title|
separator << char_repeat(title.length, '-')
}
@lines << separator
end
|
Instance Method Details
#append(*columns) ⇒ Object
30
31
32
|
# File 'lib/print.rb', line 30
def append(columns)
@lines << columns
end
|
#calc_column(line) ⇒ Object
48
49
50
|
# File 'lib/print.rb', line 48
def calc_column(line)
line.length / @width
end
|
#calc_max_widths ⇒ Object
78
79
80
81
82
83
84
85
86
|
# File 'lib/print.rb', line 78
def calc_max_widths
maxwidth = Array.new(@ncols, 0)
@lines.each { |line|
line.each_with_index { |value, index|
maxwidth[index] = [value.to_s.length, maxwidth[index]].max
}
}
maxwidth
end
|
#char_repeat(n, char) ⇒ Object
10
11
12
13
14
15
16
|
# File 'lib/print.rb', line 10
def char_repeat(n, char)
result = ""
(1..n).each { |i|
result = result + char
}
result
end
|
#fill_to_next_column(line) ⇒ Object
52
53
54
55
|
# File 'lib/print.rb', line 52
def fill_to_next_column(line)
tab = @width - (line.length % @width)
line = line + spaces(tab-1) + DELIM
end
|
#generate_separator_line(maxwidth) ⇒ Object
96
97
98
99
100
101
102
|
# File 'lib/print.rb', line 96
def generate_separator_line(maxwidth)
line = []
maxwidth.each { |width|
line << separator(width)
}
line
end
|
#indent(column) ⇒ Object
34
35
36
37
38
39
40
41
|
# File 'lib/print.rb', line 34
def indent(column)
if column == 0
""
else
segment = spaces(@width-1) + DELIM
segment * column
end
end
|
#next_unprinted_column(col_array, start = 0) ⇒ Object
71
72
73
74
75
76
|
# File 'lib/print.rb', line 71
def next_unprinted_column(col_array, start=0)
for i in (start...col_array.length)
return i if col_array[i]
end
nil
end
|
#print_line_normally(maxwidth, line) ⇒ Object
88
89
90
91
92
93
94
|
# File 'lib/print.rb', line 88
def print_line_normally(maxwidth, line)
output = ""
line.each_with_index { |value, index|
output = output + value.to_s + spaces(maxwidth[index] - value.to_s.length + 2)
}
puts output[0...-2]
end
|
#print_old_staggered(maxwidth, term_width) ⇒ Object
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
# File 'lib/print.rb', line 149
def print_old_staggered(maxwidth, term_width)
puts separator(term_width)
@lines.each_with_index { |line, index|
next if index == 1
output = ""
column = 0
line.each_with_index { |value, column|
value = value.to_s
output = indent(column) if output.length == 0
if value.length > (@width - 1)
output = output + value
println(output)
output = ""
else
output = output + value + spaces(@width - value.length - 1) + DELIM
end
column += 1
}
println(output) if output.length > 0
puts separator(term_width)
}
end
|
#print_staggered(maxwidth, term_width) ⇒ Object
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
# File 'lib/print.rb', line 117
def print_staggered(maxwidth, term_width)
minw = term_width
maxwidth.each_with_index { |w, index|
next if index == 0
width = (term_width - w) / index
minw = [width, minw].min
}
@width = minw
puts separator(term_width)
@lines.each_with_index { |line, lineno|
output = ""
cols_left = Array.new(@ncols, true)
while column = next_unprinted_column(cols_left)
output = indent(column) if output.length == 0
while column
value = line[column].to_s
cols_left[column] = false output = output + value
output = fill_to_next_column(output)
next_column = calc_column(output)
column = next_unprinted_column(cols_left, next_column)
if column && (column > next_column)
output = output + (spaces(@width-1) + DELIM) * (column - next_column)
end
end
println(output)
output = ""
end
puts separator(term_width)
}
end
|
#print_table ⇒ Object
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
# File 'lib/print.rb', line 172
def print_table
return if @lines.nil?
return if @ncols == 0
if @ncols == 1
@lines.each { |line| puts line }
return
end
maxwidth = calc_max_widths
width_needed = maxwidth.reduce(:+) + (@ncols-1) * 2
term_width, h = Util.detect_terminal_size
if param('mock-env') != ''
term_width = 1000000
end
if (width_needed <= term_width)
print_table_normally(maxwidth)
else
print_staggered(maxwidth, term_width)
end
end
|
#print_table_normally(maxwidth) ⇒ Object
104
105
106
107
108
109
110
111
|
# File 'lib/print.rb', line 104
def print_table_normally(maxwidth)
@lines.each_with_index { |line, lineno|
print_line_normally(maxwidth, line)
if lineno == 0
print_line_normally(maxwidth, generate_separator_line(maxwidth))
end
}
end
|
#println(line) ⇒ Object
prints a line with DELIMs following in proper columns
58
59
60
61
62
63
64
65
66
67
68
69
|
# File 'lib/print.rb', line 58
def println(line)
column = calc_column(line)
if (column < @ncols)
line = fill_to_next_column(line)
column = calc_column(line)
end
if (column < @ncols)
segment = spaces(@width-1) + DELIM
line = line + segment * (@ncols - column)
end
puts strip(line)
end
|
#separator(width) ⇒ Object
113
114
115
|
# File 'lib/print.rb', line 113
def separator(width)
char_repeat(width, @sep_char)
end
|
#set_titles(sep_char, titles) ⇒ Object
22
23
24
25
26
27
28
|
# File 'lib/print.rb', line 22
def set_titles(sep_char, titles)
@ncols = titles.length
@lines = [ ]
@width = nil
@sep_char = sep_char
@lines << titles
end
|
#spaces(n) ⇒ Object
18
19
20
|
# File 'lib/print.rb', line 18
def spaces(n)
char_repeat(n, ' ')
end
|
#strip(line) ⇒ Object
43
44
45
46
|
# File 'lib/print.rb', line 43
def strip(line)
regexp = Regexp.new(' *' + Regexp.escape(DELIM) + '*\Z')
line.sub(regexp, "")
end
|