Class: GithubCLI::Formatters::Table
- Inherits:
-
Object
- Object
- GithubCLI::Formatters::Table
show all
- Extended by:
- Forwardable
- Includes:
- Enumerable, Util
- Defined in:
- lib/github_cli/formatters/table.rb
Constant Summary
collapse
- OPTIONS =
{
:border => {
'top' => '━',
'top_mid' => '┳',
'top_left' => '┏',
'top_right' => '┓',
'bottom' => '━',
'bottom_mid' => '┻',
'bottom_left' => '┗',
'bottom_right' => '┛',
'left' => '┃',
'left_mid' => '┣',
'mid' => '━',
'mid_mid' => '╋',
'right' => '┃',
'right_mid' => '┫'
},
:truncate => '…',
:style => {
:padding_left => 1,
:padding_right => 1,
:align => :left,
:head => ['cyan'],
:compact => false
}
}
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Util
#convert_value, #convert_values, #flatten_hash, #hash_without!, #longest_common_prefix, #pad, #truncate
Constructor Details
#initialize(response, options = {}) ⇒ Table
Returns a new instance of Table.
47
48
49
50
51
52
|
# File 'lib/github_cli/formatters/table.rb', line 47
def initialize(response, options={})
@total_records = 1
@transform = determine_layout(options[:transform])
@response = response
@output_array = build_output
end
|
Instance Attribute Details
#output_array ⇒ Object
Returns the value of attribute output_array.
40
41
42
|
# File 'lib/github_cli/formatters/table.rb', line 40
def output_array
@output_array
end
|
#response ⇒ Object
Returns the value of attribute response.
40
41
42
|
# File 'lib/github_cli/formatters/table.rb', line 40
def response
@response
end
|
#total_records ⇒ Object
Returns the value of attribute total_records.
40
41
42
|
# File 'lib/github_cli/formatters/table.rb', line 40
def total_records
@total_records
end
|
Returns the value of attribute transform.
40
41
42
|
# File 'lib/github_cli/formatters/table.rb', line 40
def transform
@transform
end
|
Instance Method Details
#border ⇒ Object
62
63
64
|
# File 'lib/github_cli/formatters/table.rb', line 62
def border
OpenStruct.new OPTIONS[:border]
end
|
#build_output ⇒ Object
Builds output array from response hash
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
# File 'lib/github_cli/formatters/table.rb', line 72
def build_output
if response.respond_to?(:to_ary)
case transform
when :horizontal
array = [flatten_hash(response[0].to_hash).keys]
response.each do |item|
array << convert_values(flatten_hash(item.to_hash).values)
end
array
when :vertical
response.inject([]) do |array, item|
output = flatten_hash(item.to_hash)
rows = output.keys.zip(convert_values(output.values))
rows.each {|row| array << row }
@total_records = rows.size
array
end
end
elsif response.respond_to?(:to_hash)
output = flatten_hash(response)
case transform
when :horizontal
array = [output.keys]
array << convert_values(output.values)
when :vertical
output.keys.zip(convert_values(output.values))
end
else
[response]
end
end
|
#column_width(index) ⇒ Object
129
130
131
|
# File 'lib/github_cli/formatters/table.rb', line 129
def column_width(index)
width = column_widths[index] || 0
end
|
#column_widths ⇒ Object
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
# File 'lib/github_cli/formatters/table.rb', line 104
def column_widths
@column_widths ||= begin
array = case transform
when :horizontal
field_len = (
(Terminal.width - (output_array[0].size)).to_f / output_array[0].size.to_f
).round
Array.new(output_array[0].size, field_len)
when :vertical
start = 0
maximas = []
colcount = output_array.max{ |a,b| a.size <=> b.size }.size
start.upto(colcount - 1) do |index|
maxima = output_array.map do |row|
row[index] ? (style.padding_left+ row[index].to_s.size + style.padding_right) : 0
end.max
maximas << maxima
end
maximas
end
array
end
end
|
#columns ⇒ Object
133
134
135
|
# File 'lib/github_cli/formatters/table.rb', line 133
def columns
column_widths.size
end
|
#determine_layout(layout) ⇒ Object
54
55
56
|
# File 'lib/github_cli/formatters/table.rb', line 54
def determine_layout(layout)
( !layout.nil? && layout.index('h') ) ? :horizontal : :vertical
end
|
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
# File 'lib/github_cli/formatters/table.rb', line 141
def format
case response
when Array
render_top_line
output_array.each_with_index do |row, indx|
render_row row
if (output_array.size - 1 != indx) && ((indx + 1) % total_records == 0)
render_middle_line
end
end
render_bottom_line
when Hash
render_top_line
output_array.each_with_index do |row, indx|
render_row row
if transform == :horizontal && output_array.size - 1 != indx
render_middle_line
end
end
render_bottom_line
else
Terminal.line "#{response}\n"
end
end
|
#options ⇒ Object
58
59
60
|
# File 'lib/github_cli/formatters/table.rb', line 58
def options
OPTIONS
end
|
#render_bottom_line ⇒ Object
217
218
219
220
221
222
223
224
|
# File 'lib/github_cli/formatters/table.rb', line 217
def render_bottom_line
render_line(
border.bottom,
border.bottom_left || border.bottom,
border.bottom_right || border.top,
border.bottom_mid
)
end
|
#render_cell(field, indx) ⇒ Object
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
|
# File 'lib/github_cli/formatters/table.rb', line 226
def render_cell(field, indx)
width = column_widths[indx] -
(style.padding_left || 0) -
(style.padding_right || 0)
if (indx == (column_widths.size - 1))
width = width - 2 if width > 3
end
field = if field.to_s.length == width
field.to_s
elsif field.to_s.length < width
pad(field.to_s, width, :align => style.align)
else
truncate(field.to_s, width)
end
string = ''
string << ' ' * (style.padding_left || 0)
string << "#{field}"
string << ' ' * (style.padding_right || 0)
string << "#{border.right}"
string
end
|
#render_horizontal(item) ⇒ Object
174
175
176
177
178
179
180
|
# File 'lib/github_cli/formatters/table.rb', line 174
def render_horizontal(item)
output = GithubCLI::Util.flatten_hash(item.to_hash)
GithubCLI.ui.print_table(
[GithubCLI::Util.convert_values(output.values)],
:truncate => true
)
end
|
#render_line(line, left, right, intersection) ⇒ Object
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
# File 'lib/github_cli/formatters/table.rb', line 182
def render_line(line, left, right, intersection)
line = left + (line * (total_width - 2)) + right
width = 0
column_widths.each_with_index do |col_width, indx|
if indx != (column_widths.size - 1)
width += col_width + 1
line = line[0...width] + intersection + line[width+1..-1]
else
width += col_width - 1
line = line[0...width] + right
end
end
Terminal.line line + "\n"
end
|
#render_middle_line ⇒ Object
208
209
210
211
212
213
214
215
|
# File 'lib/github_cli/formatters/table.rb', line 208
def render_middle_line
render_line(
border.mid,
border.left_mid || border.mid,
border.right_mid || border.mid,
border.mid_mid
)
end
|
#render_row(fields, options = {}) ⇒ Object
251
252
253
254
255
256
257
|
# File 'lib/github_cli/formatters/table.rb', line 251
def render_row(fields, options={})
columns = []
fields.each_with_index do |field, indx|
columns << render_cell(field, indx)
end
Terminal.line border.left + columns.join + "\n"
end
|
#render_top_line ⇒ Object
199
200
201
202
203
204
205
206
|
# File 'lib/github_cli/formatters/table.rb', line 199
def render_top_line
render_line(
border.top,
border.top_left || border.top,
border.top_right || border.top,
border.top_mid
)
end
|
#render_vertical(item) ⇒ Object
166
167
168
169
170
171
172
|
# File 'lib/github_cli/formatters/table.rb', line 166
def render_vertical(item)
output = GithubCLI::Util.flatten_hash(item.to_hash)
GithubCLI.ui.print_table(
output.keys.zip(GithubCLI::Util.convert_values(output.values)),
:truncate => true
)
end
|
#style ⇒ Object
66
67
68
|
# File 'lib/github_cli/formatters/table.rb', line 66
def style
OpenStruct.new OPTIONS[:style]
end
|
#total_width ⇒ Object
137
138
139
|
# File 'lib/github_cli/formatters/table.rb', line 137
def total_width
column_widths.reduce(0, :+).round + column_widths.size + border.right.length
end
|