X-Git-Url: https://git.proxmox.com/?p=pve-common.git;a=blobdiff_plain;f=src%2FPVE%2FCLIFormatter.pm;h=eff6da5832470ddfdca01e21173374a23760dd54;hp=824c1a26a1833549e4789077739b8387944d52d7;hb=5e287f60ae597dc19c0f66bcc0e1a0785ca6eecb;hpb=41d554d9cdd4ce9cca03d5ca0444af32894b9a2c diff --git a/src/PVE/CLIFormatter.pm b/src/PVE/CLIFormatter.pm index 824c1a2..eff6da5 100644 --- a/src/PVE/CLIFormatter.pm +++ b/src/PVE/CLIFormatter.pm @@ -3,6 +3,7 @@ package PVE::CLIFormatter; use strict; use warnings; use I18N::Langinfo; +use POSIX qw(strftime); use PVE::JSONSchema; use PVE::PTY; @@ -10,6 +11,67 @@ use JSON; use utf8; use Encode; +sub render_timestamp { + my ($epoch) = @_; + + # ISO 8601 date format + return strftime("%F %H:%M:%S", localtime($epoch)); +} + +PVE::JSONSchema::register_renderer('timestamp', \&render_timestamp); + +sub render_duration { + my ($duration_in_seconds) = @_; + + my $text = ''; + my $rest = $duration_in_seconds; + + my $step = sub { + my ($unit, $unitlength) = @_; + + if ((my $v = int($rest/$unitlength)) > 0) { + $text .= " " if length($text); + $text .= "${v}${unit}"; + $rest -= $v * $unitlength; + } + }; + + $step->('w', 7*24*3600); + $step->('d', 24*3600); + $step->('h', 3600); + $step->('m', 60); + $step->('s', 1); + + return $text; +} + +PVE::JSONSchema::register_renderer('duration', \&render_duration); + +sub render_fraction_as_percentage { + my ($fraction) = @_; + + return sprintf("%.2f%%", $fraction*100); +} + +PVE::JSONSchema::register_renderer( + 'fraction_as_percentage', \&render_fraction_as_percentage); + +sub render_bytes { + my ($value) = @_; + + my @units = qw(B KiB MiB GiB TiB PiB); + + my $max_unit = 0; + if ($value > 1023) { + $max_unit = int(log($value)/log(1024)); + $value /= 1024**($max_unit); + } + + return sprintf "%.2f $units[$max_unit]", $value; +} + +PVE::JSONSchema::register_renderer('bytes', \&render_bytes); + sub query_terminal_options { my ($options) = @_; @@ -75,9 +137,7 @@ sub print_text_table { my $utf8 = $options->{utf8}; my $encoding = $options->{encoding} // 'UTF-8'; - my $autosort = 1; - if (defined($sort_key) && $sort_key eq 0) { - $autosort = 0; + if (!defined($sort_key) || $sort_key eq 0) { $sort_key = $props_to_print->[0]; } @@ -236,6 +296,24 @@ sub print_text_table { $writeln->($borderstring_b) if $border; } +sub extract_properties_to_print { + my ($propdef) = @_; + + my $required = []; + my $optional = []; + + foreach my $key (keys %$propdef) { + my $prop = $propdef->{$key}; + if ($prop->{optional}) { + push @$optional, $key; + } else { + push @$required, $key; + } + } + + return [ sort(@$required), sort(@$optional) ]; +} + # prints the result of an API GET call returning an array as a table. # takes formatting information from the results property of the call # if $props_to_print is provided, prints only those columns. otherwise @@ -249,20 +327,21 @@ sub print_api_list { my $returnprops = $result_schema->{items}->{properties}; - if (!defined($props_to_print)) { - $props_to_print = [ sort keys %$returnprops ]; - if (!scalar(@$props_to_print)) { - my $all_props = {}; - foreach my $obj (@{$data}) { - foreach my $key (keys %{$obj}) { - $all_props->{ $key } = 1; - } + $props_to_print = extract_properties_to_print($returnprops) + if !defined($props_to_print); + + if (!scalar(@$props_to_print)) { + my $all_props = {}; + foreach my $obj (@$data) { + foreach my $key (keys %$obj) { + $all_props->{$key} = 1; } - $props_to_print = [ sort keys %{$all_props} ]; } - die "unable to detect list properties\n" if !scalar(@$props_to_print); + $props_to_print = [ sort keys %{$all_props} ]; } + die "unable to detect list properties\n" if !scalar(@$props_to_print); + print_text_table($data, $returnprops, $props_to_print, $options); } @@ -284,7 +363,9 @@ sub print_api_result { my $encoding = $options->{encoding} // 'UTF-8'; my $type = $result_schema->{type}; if ($type eq 'object') { - $props_to_print = [ sort keys %$data ] if !defined($props_to_print); + $props_to_print = extract_properties_to_print($result_schema->{properties}) + if !defined($props_to_print); + $props_to_print = [ sort keys %$data ] if !scalar(@$props_to_print); my $kvstore = []; foreach my $key (@$props_to_print) { push @$kvstore, { key => $key, value => data_to_text($data->{$key}, $result_schema->{properties}->{$key}) };