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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
# File 'lib/vop/shell/shell_formatter.rb', line 33
def format(request, response, display_type)
data = response.result
command = request.command
show_options = command.show_options
case display_type
when :table
columns_to_display =
if show_options[:columns]
show_options[:columns]
else
first_row = data.first
first_row.keys
end
column_headers = [ '#' ] + columns_to_display
rearranged = []
data.each do |row|
values = [ ]
columns_to_display.each do |key|
potential_value = row[key.to_s] || row[key.to_sym]
values << potential_value
end unless row.nil?
rearranged << values
end
unless show_options.include?(:sort) && show_options[:sort] == false
rearranged.sort_by! { |row| row.first || "" }
end
rearranged.each_with_index do |row, index|
row.unshift index
end
Terminal::Table.new(
rows: rearranged,
headings: column_headers
)
when :list
data.join("\n")
when :hash
data.map do |k,v|
"#{k} : #{v}"
end.join("\n")
when :entity_list
sorted = data.sort_by { |e| e.id }
columns = if show_options[:columns]
show_options[:columns]
else
blacklisted_keys = %w|name params plugin_name|
all_keys = sorted.map { |x| x.data.keys }.flatten.uniq
all_keys.delete_if { |x| blacklisted_keys.include? x }
end
= [ sorted.first.key ] + columns
rows = sorted.map do |entity|
row = [ entity.id ]
columns.each do |column|
value = entity.data[column] || ""
row << (value.respond_to?(to_s) ? value.to_s[0..49] : value)
end
row
end
Terminal::Table.new(
rows: rows,
headings:
)
when :entity
entity = data
"[#{entity.type}] #{entity.id}"
when :raw
data
when :data
data.pretty_inspect
else
raise "unknown display type #{display_type}"
end
end
|