use strict;
use warnings;
use I18N::Langinfo;
+use POSIX qw(strftime);
use PVE::JSONSchema;
use PVE::PTY;
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_timestamp_gmt {
+ my ($epoch) = @_;
+
+ # ISO 8601 date format, standard Greenwich time zone
+ return strftime("%F %H:%M:%S", gmtime($epoch));
+}
+
+PVE::JSONSchema::register_renderer('timestamp_gmt', \&render_timestamp_gmt);
+
sub render_duration {
my ($duration_in_seconds) = @_;
}
sub data_to_text {
- my ($data, $propdef) = @_;
+ my ($data, $propdef, $options) = @_;
return '' if !defined($data);
- if (defined($propdef)) {
+ my $human_readable = $options->{'human-readable'} // 1;
+
+ if ($human_readable && defined($propdef)) {
if (my $type = $propdef->{type}) {
if ($type eq 'boolean') {
return $data ? 1 : 0;
if (defined(my $renderer = $propdef->{renderer})) {
my $code = PVE::JSONSchema::get_renderer($renderer);
die "internal error: unknown renderer '$renderer'" if !$code;
- return $code->($data);
+ return $code->($data, $options);
}
}
# - sort_key: can be used to sort after a column, if it isn't set we sort
# after the leftmost column (with no undef value in $data) this can be
# turned off by passing 0 as sort_key
-# - border: print with/without table header and asciiart border
+# - noborder: print without asciiart border
+# - noheader: print without table header
# - columns: limit output width (if > 0)
# - utf8: use utf8 characters for table delimiters
my ($data, $returnprops, $props_to_print, $options) = @_;
my $sort_key = $options->{sort_key};
- my $border = $options->{border};
+ my $border = !$options->{noborder};
+ my $header = !$options->{noheader};
my $columns = $options->{columns};
my $utf8 = $options->{utf8};
my $encoding = $options->{encoding} // 'UTF-8';
my $prop = $props_to_print->[$i];
my $propinfo = $returnprops->{$prop} // {};
- my $text = data_to_text($entry->{$prop}, $propinfo);
+ my $text = data_to_text($entry->{$prop}, $propinfo, $options);
my $lines = [ split(/\n/, $text) ];
my $linecount = scalar(@$lines);
$height = $linecount if $linecount > $height;
};
$writeln->($borderstring_t) if $border;
- my $text = sprintf $formatstring, map { $colopts->{$_}->{title} } @$props_to_print;
- $writeln->($text);
- foreach my $coldata (@$tabledata) {
- $writeln->($borderstring_m) if $border;
+ if ($header) {
+ my $text = sprintf $formatstring, map { $colopts->{$_}->{title} } @$props_to_print;
+ $writeln->($text);
+ }
+
+ for (my $i = 0; $i < scalar(@$tabledata); $i++) {
+ my $coldata = $tabledata->[$i];
+
+ $writeln->($borderstring_m) if $border && ($i != 0 || $header);
for (my $i = 0; $i < $coldata->{height}; $i++) {
- $text = sprintf $formatstring, map {
+ my $text = sprintf $formatstring, map {
substr($coldata->{rowdata}->{$_}->{lines}->[$i] // '', 0, $colopts->{$_}->{cutoff});
} @$props_to_print;
}
sub print_api_result {
- my ($format, $data, $result_schema, $props_to_print, $options) = @_;
+ my ($data, $result_schema, $props_to_print, $options) = @_;
+
+ return if $options->{quiet};
if (!defined($options)) {
$options = query_terminal_options({});
$options = { %$options }; # copy
}
+ my $format = $options->{format} // 'text';
+
return if $result_schema->{type} eq 'null';
if ($format eq 'json') {
$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}) };
+ push @$kvstore, { key => $key, value => data_to_text($data->{$key}, $result_schema->{properties}->{$key}, $options) };
}
my $schema = { type => 'array', items => { type => 'object' }};
- $options->{border} = $format eq 'text';
print_api_list($kvstore, $schema, ['key', 'value'], $options);
} elsif ($type eq 'array') {
return if !scalar(@$data);
my $item_type = $result_schema->{items}->{type};
if ($item_type eq 'object') {
- $options->{border} = $format eq 'text';
print_api_list($data, $result_schema, $props_to_print, $options);
} else {
- foreach my $entry (@$data) {
- print encode($encoding, data_to_text($entry) . "\n");
+ my $kvstore = [];
+ foreach my $value (@$data) {
+ push @$kvstore, { value => $value };
}
+ my $schema = { type => 'array', items => { type => 'object', properties => { value => $result_schema->{items} }}};
+ $options->{noheader} = 1;
+ print_api_list($kvstore, $schema, ['value'], $options);
}
} else {
print encode($encoding, "$data\n");