]> git.proxmox.com Git - pve-common.git/blob - src/PVE/CLIFormatter.pm
bump version to 8.2.1
[pve-common.git] / src / PVE / CLIFormatter.pm
1 package PVE::CLIFormatter;
2
3 use strict;
4 use warnings;
5
6 use I18N::Langinfo;
7 use YAML::XS; # supports Dumping JSON::PP::Boolean
8 $YAML::XS::Boolean = "JSON::PP";
9
10 use PVE::JSONSchema;
11 use PVE::PTY;
12 use PVE::Format;
13
14 use JSON;
15 use utf8;
16 use Encode;
17
18 PVE::JSONSchema::register_renderer('timestamp', \&PVE::Format::render_timestamp);
19 PVE::JSONSchema::register_renderer('timestamp_gmt', \&PVE::Format::render_timestamp_gmt);
20 PVE::JSONSchema::register_renderer('duration', \&PVE::Format::render_duration);
21 PVE::JSONSchema::register_renderer('fraction_as_percentage', \&PVE::Format::render_fraction_as_percentage);
22 PVE::JSONSchema::register_renderer('bytes', \&PVE::Format::render_bytes);
23
24 sub render_yaml {
25 my ($value) = @_;
26
27 my $data = YAML::XS::Dump($value);
28 $data =~ s/^---[\n\s]//; # remove yaml marker
29
30 return $data;
31 }
32
33 PVE::JSONSchema::register_renderer('yaml', \&render_yaml);
34
35 sub query_terminal_options {
36 my ($options) = @_;
37
38 $options //= {};
39
40 if (-t STDOUT) {
41 ($options->{columns}) = PVE::PTY::tcgetsize(*STDOUT);
42 }
43
44 $options->{encoding} = I18N::Langinfo::langinfo(I18N::Langinfo::CODESET());
45
46 $options->{utf8} = 1 if $options->{encoding} eq 'UTF-8';
47
48 return $options;
49 }
50
51 sub data_to_text {
52 my ($data, $propdef, $options, $terminal_opts) = @_;
53
54 return '' if !defined($data);
55
56 $terminal_opts //= {};
57
58 my $human_readable = $options->{'human-readable'} // 1;
59
60 if ($human_readable && defined($propdef)) {
61 if (my $type = $propdef->{type}) {
62 if ($type eq 'boolean') {
63 return $data ? 1 : 0;
64 }
65 }
66 if (!defined($data) && defined($propdef->{default})) {
67 return "($propdef->{default})";
68 }
69 if (defined(my $renderer = $propdef->{renderer})) {
70 my $code = PVE::JSONSchema::get_renderer($renderer);
71 die "internal error: unknown renderer '$renderer'" if !$code;
72 return $code->($data, $options, $terminal_opts);
73 }
74 }
75
76 if (my $class = ref($data)) {
77 # JSON::PP::Boolean requires allow_nonref
78 return to_json($data, { allow_nonref => 1, canonical => 1 });
79 } else {
80 return "$data";
81 }
82 }
83
84 # prints a formatted table with a title row.
85 # $data - the data to print (array of objects)
86 # $returnprops -json schema property description
87 # $props_to_print - ordered list of properties to print
88 # $options
89 # - sort_key: can be used to sort after a specific column, if it isn't set we sort
90 # after the leftmost column. This can be turned off by passing 0 as sort_key
91 # - noborder: print without asciiart border
92 # - noheader: print without table header
93 # - columns: limit output width (if > 0)
94 # - utf8: use utf8 characters for table delimiters
95
96 sub print_text_table {
97 my ($data, $returnprops, $props_to_print, $options, $terminal_opts) = @_;
98
99 $terminal_opts //= query_terminal_options({});
100
101 my $sort_key = $options->{sort_key};
102 my $show_border = !$options->{noborder};
103 my $show_header = !$options->{noheader};
104
105 my $columns = $terminal_opts->{columns};
106 my $utf8 = $terminal_opts->{utf8};
107 my $encoding = $terminal_opts->{encoding} // 'UTF-8';
108
109 $sort_key //= $props_to_print->[0];
110
111 if (defined($sort_key) && $sort_key ne 0) {
112 my $type = $returnprops->{$sort_key}->{type} // 'string';
113 my $cmpfn;
114 if ($type eq 'integer' || $type eq 'number') {
115 $cmpfn = sub { $_[0] <=> $_[1] };
116 } else {
117 $cmpfn = sub { $_[0] cmp $_[1] };
118 }
119 @$data = sort {
120 PVE::Tools::safe_compare($a->{$sort_key}, $b->{$sort_key}, $cmpfn)
121 } @$data;
122 }
123
124 my $colopts = {};
125
126 my $border = { m => '', b => '', t => '', h => '' };
127 my $formatstring = '';
128
129 my $column_count = scalar(@$props_to_print);
130
131 my $tabledata = [];
132
133 foreach my $entry (@$data) {
134
135 my $height = 1;
136 my $rowdata = {};
137
138 for (my $i = 0; $i < $column_count; $i++) {
139 my $prop = $props_to_print->[$i];
140 my $propinfo = $returnprops->{$prop} // {};
141
142 my $text = data_to_text($entry->{$prop}, $propinfo, $options, $terminal_opts);
143 my $lines = [ split(/\n/, $text) ];
144 my $linecount = scalar(@$lines);
145 $height = $linecount if $linecount > $height;
146
147 my $width = 0;
148 foreach my $line (@$lines) {
149 my $len = length($line);
150 $width = $len if $len > $width;
151 }
152
153 $width = ($width =~ m/^(\d+)$/) ? int($1) : 0; # untaint int
154
155 $rowdata->{$prop} = {
156 lines => $lines,
157 width => $width,
158 };
159 }
160
161 push @$tabledata, {
162 height => $height,
163 rowdata => $rowdata,
164 };
165 }
166
167 for (my $i = 0; $i < $column_count; $i++) {
168 my $prop = $props_to_print->[$i];
169 my $propinfo = $returnprops->{$prop} // {};
170 my $type = $propinfo->{type} // 'string';
171 my $alignstr = ($type eq 'integer' || $type eq 'number') ? '' : '-';
172
173 my $title = $propinfo->{title} // $prop;
174 my $cutoff = $propinfo->{print_width} // $propinfo->{maxLength};
175
176 # calculate maximal print width and cutoff
177 my $titlelen = length($title);
178
179 my $longest = $titlelen;
180 foreach my $coldata (@$tabledata) {
181 my $rowdata = $coldata->{rowdata}->{$prop};
182 $longest = $rowdata->{width} if $rowdata->{width} > $longest;
183 }
184 $cutoff = $longest if !defined($cutoff) || $cutoff > $longest;
185
186 $colopts->{$prop} = {
187 title => $title,
188 cutoff => $cutoff,
189 };
190
191 if ($show_border) {
192 if ($i == 0 && ($column_count == 1)) {
193 if ($utf8) {
194 $formatstring .= "│ %$alignstr${cutoff}s │";
195 $border->{t} .= "┌─" . ('─' x $cutoff) . "─┐";
196 $border->{h} .= "╞═" . ('═' x $cutoff) . '═╡';
197 $border->{m} .= "├─" . ('─' x $cutoff) . "─┤";
198 $border->{b} .= "└─" . ('─' x $cutoff) . "─┘";
199 } else {
200 $formatstring .= "| %$alignstr${cutoff}s |";
201 $border->{m} .= "+-" . ('-' x $cutoff) . "-+";
202 $border->{h} .= "+=" . ('=' x $cutoff) . '=';
203 }
204 } elsif ($i == 0) {
205 if ($utf8) {
206 $formatstring .= "│ %$alignstr${cutoff}s ";
207 $border->{t} .= "┌─" . ('─' x $cutoff) . '─';
208 $border->{h} .= "╞═" . ('═' x $cutoff) . '═';
209 $border->{m} .= "├─" . ('─' x $cutoff) . '─';
210 $border->{b} .= "└─" . ('─' x $cutoff) . '─';
211 } else {
212 $formatstring .= "| %$alignstr${cutoff}s ";
213 $border->{m} .= "+-" . ('-' x $cutoff) . '-';
214 $border->{h} .= "+=" . ('=' x $cutoff) . '=';
215 }
216 } elsif ($i == ($column_count - 1)) {
217 if ($utf8) {
218 $formatstring .= "│ %$alignstr${cutoff}s │";
219 $border->{t} .= "┬─" . ('─' x $cutoff) . "─┐";
220 $border->{h} .= "╪═" . ('═' x $cutoff) . '═╡';
221 $border->{m} .= "┼─" . ('─' x $cutoff) . "─┤";
222 $border->{b} .= "┴─" . ('─' x $cutoff) . "─┘";
223 } else {
224 $formatstring .= "| %$alignstr${cutoff}s |";
225 $border->{m} .= "+-" . ('-' x $cutoff) . "-+";
226 $border->{h} .= "+=" . ('=' x $cutoff) . "=+";
227 }
228 } else {
229 if ($utf8) {
230 $formatstring .= "│ %$alignstr${cutoff}s ";
231 $border->{t} .= "┬─" . ('─' x $cutoff) . '─';
232 $border->{h} .= "╪═" . ('═' x $cutoff) . '═';
233 $border->{m} .= "┼─" . ('─' x $cutoff) . '─';
234 $border->{b} .= "┴─" . ('─' x $cutoff) . '─';
235 } else {
236 $formatstring .= "| %$alignstr${cutoff}s ";
237 $border->{m} .= "+-" . ('-' x $cutoff) . '-';
238 $border->{h} .= "+=" . ('=' x $cutoff) . '=';
239 }
240 }
241 } else {
242 # skip alignment and cutoff on last column
243 $formatstring .= ($i == ($column_count - 1)) ? "%s" : "%$alignstr${cutoff}s ";
244 }
245 }
246
247 $border->{t} = $border->{m} if !length($border->{t});
248 $border->{b} = $border->{m} if !length($border->{b});
249
250 my $writeln = sub {
251 my ($text) = @_;
252
253 if ($columns) {
254 print encode($encoding, substr($text, 0, $columns) . "\n");
255 } else {
256 print encode($encoding, $text) . "\n";
257 }
258 };
259
260 $writeln->($border->{t}) if $show_border;
261
262 if ($show_header) {
263 my $text = sprintf $formatstring, map { $colopts->{$_}->{title} } @$props_to_print;
264 $writeln->($text);
265 $border->{sep} = $border->{h};
266 } else {
267 $border->{sep} = $border->{m};
268 }
269
270 for (my $i = 0; $i < scalar(@$tabledata); $i++) {
271 my $coldata = $tabledata->[$i];
272
273 if ($show_border && ($i != 0 || $show_header)) {
274 $writeln->($border->{sep});
275 $border->{sep} = $border->{m};
276 }
277
278 for (my $i = 0; $i < $coldata->{height}; $i++) {
279 my $text = sprintf $formatstring, map {
280 substr($coldata->{rowdata}->{$_}->{lines}->[$i] // '', 0, $colopts->{$_}->{cutoff});
281 } @$props_to_print;
282
283 $writeln->($text);
284 }
285 }
286
287 $writeln->($border->{b}) if $show_border;
288 }
289
290 sub extract_properties_to_print {
291 my ($propdef) = @_;
292
293 my $required = [];
294 my $optional = [];
295
296 foreach my $key (keys %$propdef) {
297 my $prop = $propdef->{$key};
298 if ($prop->{optional}) {
299 push @$optional, $key;
300 } else {
301 push @$required, $key;
302 }
303 }
304
305 return [ sort(@$required), sort(@$optional) ];
306 }
307
308 # prints the result of an API GET call returning an array as a table.
309 # takes formatting information from the results property of the call
310 # if $props_to_print is provided, prints only those columns. otherwise
311 # takes all fields of the results property, with a fallback
312 # to all fields occurring in items of $data.
313 sub print_api_list {
314 my ($data, $result_schema, $props_to_print, $options, $terminal_opts) = @_;
315
316 die "can only print object lists\n"
317 if !($result_schema->{type} eq 'array' && $result_schema->{items}->{type} eq 'object');
318
319 my $returnprops = $result_schema->{items}->{properties};
320
321 $props_to_print = extract_properties_to_print($returnprops)
322 if !defined($props_to_print);
323
324 if (!scalar(@$props_to_print)) {
325 my $all_props = {};
326 foreach my $obj (@$data) {
327 foreach my $key (keys %$obj) {
328 $all_props->{$key} = 1;
329 }
330 }
331 $props_to_print = [ sort keys %{$all_props} ];
332 }
333
334 die "unable to detect list properties\n" if !scalar(@$props_to_print);
335
336 print_text_table($data, $returnprops, $props_to_print, $options, $terminal_opts);
337 }
338
339 my $guess_type = sub {
340 my $data = shift;
341
342 return 'null' if !defined($data);
343
344 my $class = ref($data);
345 return 'string' if !$class;
346
347 if ($class eq 'HASH') {
348 return 'object';
349 } elsif ($class eq 'ARRAY') {
350 return 'array';
351 } else {
352 return 'string'; # better than nothing
353 }
354 };
355
356 sub print_api_result {
357 my ($data, $result_schema, $props_to_print, $options, $terminal_opts) = @_;
358
359 return if $options->{quiet};
360
361 $terminal_opts //= query_terminal_options({});
362
363 my $format = $options->{'output-format'} // 'text';
364
365 if ($result_schema && defined($result_schema->{type})) {
366 return if $result_schema->{type} eq 'null';
367 return if $result_schema->{optional} && !defined($data);
368 } else {
369 my $type = $guess_type->($data);
370 $result_schema = { type => $type };
371 $result_schema->{items} = { type => $guess_type->($data->[0]) } if $type eq 'array';
372 }
373
374 if ($format eq 'yaml') {
375 print encode('UTF-8', YAML::XS::Dump($data));
376 } elsif ($format eq 'json') {
377 # Note: we always use utf8 encoding for json format
378 print to_json($data, {utf8 => 1, allow_nonref => 1, canonical => 1 }) . "\n";
379 } elsif ($format eq 'json-pretty') {
380 # Note: we always use utf8 encoding for json format
381 print to_json($data, {utf8 => 1, allow_nonref => 1, canonical => 1, pretty => 1 });
382 } elsif ($format eq 'text') {
383 my $encoding = $options->{encoding} // 'UTF-8';
384 my $type = $result_schema->{type};
385 if ($type eq 'object') {
386 $props_to_print = extract_properties_to_print($result_schema->{properties})
387 if !defined($props_to_print);
388 $props_to_print = [ sort keys %$data ] if !scalar(@$props_to_print);
389 my $kvstore = [];
390 foreach my $key (@$props_to_print) {
391 next if !defined($data->{$key});
392 push @$kvstore, { key => $key, value => data_to_text($data->{$key}, $result_schema->{properties}->{$key}, $options, $terminal_opts) };
393 }
394 my $schema = { type => 'array', items => { type => 'object' }};
395 print_api_list($kvstore, $schema, ['key', 'value'], $options, $terminal_opts);
396 } elsif ($type eq 'array') {
397 if (ref($data) eq 'ARRAY') {
398 return if !scalar(@$data);
399 } elsif (ref($data) eq 'HASH') {
400 return if !scalar($data->%*);
401 die "got hash object, but result schema specified array!\n"
402 }
403 my $item_type = $result_schema->{items}->{type};
404 if ($item_type eq 'object') {
405 print_api_list($data, $result_schema, $props_to_print, $options, $terminal_opts);
406 } else {
407 my $kvstore = [];
408 foreach my $value (@$data) {
409 push @$kvstore, { value => $value };
410 }
411 my $schema = { type => 'array', items => { type => 'object', properties => { value => $result_schema->{items} }}};
412 print_api_list($kvstore, $schema, ['value'], { %$options, noheader => 1 }, $terminal_opts);
413 }
414 } else {
415 print encode($encoding, "$data\n");
416 }
417 } else {
418 die "internal error: unknown output format"; # should not happen
419 }
420 }
421
422 sub print_api_result_plain {
423 my ($data, $result_schema, $props_to_print, $options) = @_;
424
425 # avoid borders and header, ignore terminal width
426 $options = $options ? { %$options } : {}; # copy
427
428 $options->{noheader} //= 1;
429 $options->{noborder} //= 1;
430
431 print_api_result($data, $result_schema, $props_to_print, $options, {});
432 }
433
434 1;