]> git.proxmox.com Git - pve-common.git/blobdiff - src/PVE/CLIFormatter.pm
cli: data_to_text: pass property info
[pve-common.git] / src / PVE / CLIFormatter.pm
index 77b018a9806ead824097f56eb5bd4cf301fdcc03..38d4eecd3f63b61a09805330a3db3be17b225003 100644 (file)
@@ -2,15 +2,31 @@ package PVE::CLIFormatter;
 
 use strict;
 use warnings;
+use PVE::JSONSchema;
 use JSON;
 
 sub data_to_text {
-    my ($data) = @_;
+    my ($data, $propdef) = @_;
 
+    if (defined($propdef)) {
+       if (my $type = $propdef->{type}) {
+           if ($type eq 'boolean') {
+               return $data ? 1 : 0;
+           }
+       }
+       if (!defined($data) && defined($propdef->{default})) {
+           return "($propdef->{default})";
+       }
+       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 '' if !defined($data);
 
     if (my $class = ref($data)) {
-       return to_json($data, { utf8 => 1, canonical => 1 });
+       return to_json($data, { canonical => 1 });
     } else {
        return "$data";
     }
@@ -23,8 +39,9 @@ sub data_to_text {
 # $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
 sub print_text_table {
-    my ($data, $returnprops, $props_to_print, $sort_key) = @_;
+    my ($data, $returnprops, $props_to_print, $sort_key, $border) = @_;
 
     my $autosort = 1;
     if (defined($sort_key) && $sort_key eq 0) {
@@ -33,6 +50,8 @@ sub print_text_table {
     }
 
     my $colopts = {};
+
+    my $borderstring = '';
     my $formatstring = '';
 
     my $column_count = scalar(@$props_to_print);
@@ -50,7 +69,7 @@ sub print_text_table {
        my $longest = $titlelen;
        my $sortable = $autosort;
        foreach my $entry (@$data) {
-           my $len = length(data_to_text($entry->{$prop})) // 0;
+           my $len = length(data_to_text($entry->{$prop}, $propinfo)) // 0;
            $longest = $len if $len > $longest;
            $sortable = 0 if !defined($entry->{$prop});
        }
@@ -63,12 +82,20 @@ sub print_text_table {
            cutoff => $cutoff,
        };
 
-       # skip alignment and cutoff on last column
-       $formatstring .= ($i == ($column_count - 1)) ? "%s\n" : "%-${cutoff}s ";
+       if ($border) {
+           if ($i == ($column_count - 1)) {
+               $formatstring .= "| %-${cutoff}s |\n";
+               $borderstring .= "+-" . ('-' x $cutoff) . "-+\n";
+           } else {
+               $formatstring .= "| %-${cutoff}s ";
+               $borderstring .= "+-" . ('-' x $cutoff) . '-';
+           }
+       } else {
+           # skip alignment and cutoff on last column
+           $formatstring .= ($i == ($column_count - 1)) ? "%s\n" : "%-${cutoff}s ";
+       }
     }
 
-    printf $formatstring, map { $colopts->{$_}->{title} } @$props_to_print;
-
     if (defined($sort_key)) {
        my $type = $returnprops->{$sort_key}->{type} // 'string';
        if ($type eq 'integer' || $type eq 'number') {
@@ -78,12 +105,17 @@ sub print_text_table {
        }
     }
 
+    print $borderstring if $border;
+    printf $formatstring, map { $colopts->{$_}->{title} } @$props_to_print;
+
     foreach my $entry (@$data) {
+       print $borderstring if $border;
         printf $formatstring, map {
-           substr(data_to_text($entry->{$_}) // $colopts->{$_}->{default},
+           substr(data_to_text($entry->{$_}, $returnprops->{$_}) // $colopts->{$_}->{default},
                   0, $colopts->{$_}->{cutoff});
        } @$props_to_print;
     }
+    print $borderstring if $border;
 }
 
 # prints the result of an API GET call returning an array as a table.
@@ -92,7 +124,7 @@ sub print_text_table {
 # takes all fields of the results property, with a fallback
 # to all fields occuring in items of $data.
 sub print_api_list {
-    my ($data, $result_schema, $props_to_print, $sort_key) = @_;
+    my ($data, $result_schema, $props_to_print, $sort_key, $border) = @_;
 
     die "can only print object lists\n"
        if !($result_schema->{type} eq 'array' && $result_schema->{items}->{type} eq 'object');
@@ -113,7 +145,7 @@ sub print_api_list {
        die "unable to detect list properties\n" if !scalar(@$props_to_print);
     }
 
-    print_text_table($data, $returnprops, $props_to_print, $sort_key);
+    print_text_table($data, $returnprops, $props_to_print, $sort_key, $border);
 }
 
 sub print_api_result {
@@ -123,18 +155,21 @@ sub print_api_result {
 
     if ($format eq 'json') {
        print to_json($data, {utf8 => 1, allow_nonref => 1, canonical => 1, pretty => 1 });
-    } elsif ($format eq 'text') {
+    } elsif ($format eq 'text' || $format eq 'plain') {
        my $type = $result_schema->{type};
        if ($type eq 'object') {
            $props_to_print = [ sort keys %$data ] if !defined($props_to_print);
+           my $kvstore = [];
            foreach my $key (@$props_to_print) {
-               print $key . ": " .  data_to_text($data->{$key}) . "\n";
+               push @$kvstore, { key => $key, value => data_to_text($data->{$key}, $result_schema->{properties}->{$key}) };
            }
+           my $schema = { type => 'array', items => { type => 'object' }};
+           print_api_list($kvstore, $schema, ['key', 'value'], 0, $format eq 'text');
        } elsif ($type eq 'array') {
            return if !scalar(@$data);
            my $item_type = $result_schema->{items}->{type};
            if ($item_type eq 'object') {
-               print_api_list($data, $result_schema, $props_to_print, $sort_key);
+               print_api_list($data, $result_schema, $props_to_print, $sort_key, $format eq 'text');
            } else {
                foreach my $entry (@$data) {
                    print data_to_text($entry) . "\n";