1 package PVE
::CLIFormatter
;
7 use POSIX
qw(strftime);
8 use CPAN
::Meta
::YAML
; # comes with perl-modules
17 sub render_timestamp
{
20 # ISO 8601 date format
21 return strftime
("%F %H:%M:%S", localtime($epoch));
24 PVE
::JSONSchema
::register_renderer
('timestamp', \
&render_timestamp
);
26 sub render_timestamp_gmt
{
29 # ISO 8601 date format, standard Greenwich time zone
30 return strftime
("%F %H:%M:%S", gmtime($epoch));
33 PVE
::JSONSchema
::register_renderer
('timestamp_gmt', \
&render_timestamp_gmt
);
36 my ($duration_in_seconds) = @_;
39 my $rest = $duration_in_seconds;
42 my ($unit, $unitlength) = @_;
44 if ((my $v = int($rest/$unitlength)) > 0) {
45 $text .= " " if length($text);
46 $text .= "${v}${unit}";
47 $rest -= $v * $unitlength;
51 $step->('w', 7*24*3600);
52 $step->('d', 24*3600);
60 PVE
::JSONSchema
::register_renderer
('duration', \
&render_duration
);
62 sub render_fraction_as_percentage
{
65 return sprintf("%.2f%%", $fraction*100);
68 PVE
::JSONSchema
::register_renderer
(
69 'fraction_as_percentage', \
&render_fraction_as_percentage
);
74 my @units = qw(B KiB MiB GiB TiB PiB);
78 $max_unit = int(log($value)/log(1024));
79 $value /= 1024**($max_unit);
81 my $unit = $units[$max_unit];
82 return sprintf "%.2f $unit", $value;
85 PVE
::JSONSchema
::register_renderer
('bytes', \
&render_bytes
);
90 my $data = CPAN
::Meta
::YAML
::Dump
($value);
91 $data =~ s/^---[\n\s]//; # remove yaml marker
96 PVE
::JSONSchema
::register_renderer
('yaml', \
&render_yaml
);
98 sub query_terminal_options
{
104 ($options->{columns
}) = PVE
::PTY
::tcgetsize
(*STDOUT
);
107 $options->{encoding
} = I18N
::Langinfo
::langinfo
(I18N
::Langinfo
::CODESET
());
109 $options->{utf8
} = 1 if $options->{encoding
} eq 'UTF-8';
115 my ($data, $propdef, $options, $terminal_opts) = @_;
117 return '' if !defined($data);
119 $terminal_opts //= {};
121 my $human_readable = $options->{'human-readable'} // 1;
123 if ($human_readable && defined($propdef)) {
124 if (my $type = $propdef->{type
}) {
125 if ($type eq 'boolean') {
126 return $data ?
1 : 0;
129 if (!defined($data) && defined($propdef->{default})) {
130 return "($propdef->{default})";
132 if (defined(my $renderer = $propdef->{renderer
})) {
133 my $code = PVE
::JSONSchema
::get_renderer
($renderer);
134 die "internal error: unknown renderer '$renderer'" if !$code;
135 return $code->($data, $options, $terminal_opts);
139 if (my $class = ref($data)) {
140 # JSON::PP::Boolean requires allow_nonref
141 return to_json
($data, { allow_nonref
=> 1, canonical
=> 1 });
147 # prints a formatted table with a title row.
148 # $data - the data to print (array of objects)
149 # $returnprops -json schema property description
150 # $props_to_print - ordered list of properties to print
152 # - sort_key: can be used to sort after a specific column, if it isn't set we sort
153 # after the leftmost column. This can be turned off by passing 0 as sort_key
154 # - noborder: print without asciiart border
155 # - noheader: print without table header
156 # - columns: limit output width (if > 0)
157 # - utf8: use utf8 characters for table delimiters
159 sub print_text_table
{
160 my ($data, $returnprops, $props_to_print, $options, $terminal_opts) = @_;
162 $terminal_opts //= query_terminal_options
({});
164 my $sort_key = $options->{sort_key
};
165 my $border = !$options->{noborder
};
166 my $header = !$options->{noheader
};
168 my $columns = $terminal_opts->{columns
};
169 my $utf8 = $terminal_opts->{utf8
};
170 my $encoding = $terminal_opts->{encoding
} // 'UTF-8';
172 $sort_key //= $props_to_print->[0];
174 if (defined($sort_key) && $sort_key ne 0) {
175 my $type = $returnprops->{$sort_key}->{type
} // 'string';
177 if ($type eq 'integer' || $type eq 'number') {
178 $cmpfn = sub { $_[0] <=> $_[1] };
180 $cmpfn = sub { $_[0] cmp $_[1] };
183 PVE
::Tools
::safe_compare
($a->{$sort_key}, $b->{$sort_key}, $cmpfn)
189 my $borderstring_m = '';
190 my $borderstring_b = '';
191 my $borderstring_t = '';
192 my $borderstring_h = '';
193 my $formatstring = '';
195 my $column_count = scalar(@$props_to_print);
199 foreach my $entry (@$data) {
204 for (my $i = 0; $i < $column_count; $i++) {
205 my $prop = $props_to_print->[$i];
206 my $propinfo = $returnprops->{$prop} // {};
208 my $text = data_to_text
($entry->{$prop}, $propinfo, $options, $terminal_opts);
209 my $lines = [ split(/\n/, $text) ];
210 my $linecount = scalar(@$lines);
211 $height = $linecount if $linecount > $height;
214 foreach my $line (@$lines) {
215 my $len = length($line);
216 $width = $len if $len > $width;
219 $width = ($width =~ m/^(\d+)$/) ?
int($1) : 0; # untaint int
221 $rowdata->{$prop} = {
233 for (my $i = 0; $i < $column_count; $i++) {
234 my $prop = $props_to_print->[$i];
235 my $propinfo = $returnprops->{$prop} // {};
236 my $type = $propinfo->{type
} // 'string';
237 my $alignstr = ($type eq 'integer' || $type eq 'number') ?
'' : '-';
239 my $title = $propinfo->{title
} // $prop;
240 my $cutoff = $propinfo->{print_width
} // $propinfo->{maxLength
};
242 # calculate maximal print width and cutoff
243 my $titlelen = length($title);
245 my $longest = $titlelen;
246 foreach my $coldata (@$tabledata) {
247 my $rowdata = $coldata->{rowdata
}->{$prop};
248 $longest = $rowdata->{width
} if $rowdata->{width
} > $longest;
250 $cutoff = $longest if !defined($cutoff) || $cutoff > $longest;
252 $colopts->{$prop} = {
258 if ($i == 0 && ($column_count == 1)) {
260 $formatstring .= "│ %$alignstr${cutoff}s │";
261 $borderstring_t .= "┌─" . ('─' x
$cutoff) . "─┐";
262 $borderstring_h .= "╞═" . ('═' x
$cutoff) . '═╡';
263 $borderstring_m .= "├─" . ('─' x
$cutoff) . "─┤";
264 $borderstring_b .= "└─" . ('─' x
$cutoff) . "─┘";
266 $formatstring .= "| %$alignstr${cutoff}s |";
267 $borderstring_m .= "+-" . ('-' x
$cutoff) . "-+";
268 $borderstring_h .= "+=" . ('=' x
$cutoff) . '=';
272 $formatstring .= "│ %$alignstr${cutoff}s ";
273 $borderstring_t .= "┌─" . ('─' x
$cutoff) . '─';
274 $borderstring_h .= "╞═" . ('═' x
$cutoff) . '═';
275 $borderstring_m .= "├─" . ('─' x
$cutoff) . '─';
276 $borderstring_b .= "└─" . ('─' x
$cutoff) . '─';
278 $formatstring .= "| %$alignstr${cutoff}s ";
279 $borderstring_m .= "+-" . ('-' x
$cutoff) . '-';
280 $borderstring_h .= "+=" . ('=' x
$cutoff) . '=';
282 } elsif ($i == ($column_count - 1)) {
284 $formatstring .= "│ %$alignstr${cutoff}s │";
285 $borderstring_t .= "┬─" . ('─' x
$cutoff) . "─┐";
286 $borderstring_h .= "╪═" . ('═' x
$cutoff) . '═╡';
287 $borderstring_m .= "┼─" . ('─' x
$cutoff) . "─┤";
288 $borderstring_b .= "┴─" . ('─' x
$cutoff) . "─┘";
290 $formatstring .= "| %$alignstr${cutoff}s |";
291 $borderstring_m .= "+-" . ('-' x
$cutoff) . "-+";
292 $borderstring_h .= "+=" . ('=' x
$cutoff) . "=+";
296 $formatstring .= "│ %$alignstr${cutoff}s ";
297 $borderstring_t .= "┬─" . ('─' x
$cutoff) . '─';
298 $borderstring_h .= "╪═" . ('═' x
$cutoff) . '═';
299 $borderstring_m .= "┼─" . ('─' x
$cutoff) . '─';
300 $borderstring_b .= "┴─" . ('─' x
$cutoff) . '─';
302 $formatstring .= "| %$alignstr${cutoff}s ";
303 $borderstring_m .= "+-" . ('-' x
$cutoff) . '-';
304 $borderstring_h .= "+=" . ('=' x
$cutoff) . '=';
308 # skip alignment and cutoff on last column
309 $formatstring .= ($i == ($column_count - 1)) ?
"%s" : "%$alignstr${cutoff}s ";
313 $borderstring_t = $borderstring_m if !length($borderstring_t);
314 $borderstring_b = $borderstring_m if !length($borderstring_b);
320 print encode
($encoding, substr($text, 0, $columns) . "\n");
322 print encode
($encoding, $text) . "\n";
326 $writeln->($borderstring_t) if $border;
328 my $borderstring_sep;
330 my $text = sprintf $formatstring, map { $colopts->{$_}->{title
} } @$props_to_print;
332 $borderstring_sep = $borderstring_h;
334 $borderstring_sep = $borderstring_m;
337 for (my $i = 0; $i < scalar(@$tabledata); $i++) {
338 my $coldata = $tabledata->[$i];
340 if ($border && ($i != 0 || $header)) {
341 $writeln->($borderstring_sep);
342 $borderstring_sep = $borderstring_m;
345 for (my $i = 0; $i < $coldata->{height
}; $i++) {
347 my $text = sprintf $formatstring, map {
348 substr($coldata->{rowdata
}->{$_}->{lines
}->[$i] // '', 0, $colopts->{$_}->{cutoff
});
355 $writeln->($borderstring_b) if $border;
358 sub extract_properties_to_print
{
364 foreach my $key (keys %$propdef) {
365 my $prop = $propdef->{$key};
366 if ($prop->{optional
}) {
367 push @$optional, $key;
369 push @$required, $key;
373 return [ sort(@$required), sort(@$optional) ];
376 # prints the result of an API GET call returning an array as a table.
377 # takes formatting information from the results property of the call
378 # if $props_to_print is provided, prints only those columns. otherwise
379 # takes all fields of the results property, with a fallback
380 # to all fields occurring in items of $data.
382 my ($data, $result_schema, $props_to_print, $options, $terminal_opts) = @_;
384 die "can only print object lists\n"
385 if !($result_schema->{type
} eq 'array' && $result_schema->{items
}->{type
} eq 'object');
387 my $returnprops = $result_schema->{items
}->{properties
};
389 $props_to_print = extract_properties_to_print
($returnprops)
390 if !defined($props_to_print);
392 if (!scalar(@$props_to_print)) {
394 foreach my $obj (@$data) {
395 foreach my $key (keys %$obj) {
396 $all_props->{$key} = 1;
399 $props_to_print = [ sort keys %{$all_props} ];
402 die "unable to detect list properties\n" if !scalar(@$props_to_print);
404 print_text_table
($data, $returnprops, $props_to_print, $options, $terminal_opts);
407 my $guess_type = sub {
410 return 'null' if !defined($data);
412 my $class = ref($data);
413 return 'string' if !$class;
415 if ($class eq 'HASH') {
417 } elsif ($class eq 'ARRAY') {
420 return 'string'; # better than nothing
424 sub print_api_result
{
425 my ($data, $result_schema, $props_to_print, $options, $terminal_opts) = @_;
427 return if $options->{quiet
};
429 $terminal_opts //= query_terminal_options
({});
431 my $format = $options->{'output-format'} // 'text';
433 if ($result_schema && defined($result_schema->{type
})) {
434 return if $result_schema->{type
} eq 'null';
435 return if $result_schema->{optional
} && !defined($data);
437 my $type = $guess_type->($data);
438 $result_schema = { type
=> $type };
439 $result_schema->{items
} = { type
=> $guess_type->($data->[0]) } if $type eq 'array';
442 if ($format eq 'yaml') {
443 print encode
('UTF-8', CPAN
::Meta
::YAML
::Dump
($data));
444 } elsif ($format eq 'json') {
445 # Note: we always use utf8 encoding for json format
446 print to_json
($data, {utf8
=> 1, allow_nonref
=> 1, canonical
=> 1 }) . "\n";
447 } elsif ($format eq 'json-pretty') {
448 # Note: we always use utf8 encoding for json format
449 print to_json
($data, {utf8
=> 1, allow_nonref
=> 1, canonical
=> 1, pretty
=> 1 });
450 } elsif ($format eq 'text') {
451 my $encoding = $options->{encoding
} // 'UTF-8';
452 my $type = $result_schema->{type
};
453 if ($type eq 'object') {
454 $props_to_print = extract_properties_to_print
($result_schema->{properties
})
455 if !defined($props_to_print);
456 $props_to_print = [ sort keys %$data ] if !scalar(@$props_to_print);
458 foreach my $key (@$props_to_print) {
459 next if !defined($data->{$key});
460 push @$kvstore, { key
=> $key, value
=> data_to_text
($data->{$key}, $result_schema->{properties
}->{$key}, $options, $terminal_opts) };
462 my $schema = { type
=> 'array', items
=> { type
=> 'object' }};
463 print_api_list
($kvstore, $schema, ['key', 'value'], $options, $terminal_opts);
464 } elsif ($type eq 'array') {
465 return if !scalar(@$data);
466 my $item_type = $result_schema->{items
}->{type
};
467 if ($item_type eq 'object') {
468 print_api_list
($data, $result_schema, $props_to_print, $options, $terminal_opts);
471 foreach my $value (@$data) {
472 push @$kvstore, { value
=> $value };
474 my $schema = { type
=> 'array', items
=> { type
=> 'object', properties
=> { value
=> $result_schema->{items
} }}};
475 print_api_list
($kvstore, $schema, ['value'], { %$options, noheader
=> 1 }, $terminal_opts);
478 print encode
($encoding, "$data\n");
481 die "internal error: unknown output format"; # should not happen
485 sub print_api_result_plain
{
486 my ($data, $result_schema, $props_to_print, $options) = @_;
488 # avoid borders and header, ignore terminal width
489 $options = $options ?
{ %$options } : {}; # copy
491 $options->{noheader
} //= 1;
492 $options->{noborder
} //= 1;
494 print_api_result
($data, $result_schema, $props_to_print, $options, {});