]> git.proxmox.com Git - pve-common.git/blobdiff - src/PVE/CLIHandler.pm
cli: print_text_table: another fix for poperties without schema
[pve-common.git] / src / PVE / CLIHandler.pm
index 9cf79886acc009953dd35e130c2329bddf1553f3..a0d5b0e4a1ca5bd02ec73c78bb825e52b8d9e0e8 100644 (file)
@@ -464,8 +464,7 @@ sub print_text_table {
 
     for (my $i = 0; $i < $column_count; $i++) {
        my $prop = $props_to_print->[$i];
-       my $propinfo = $returnprops->{$prop};
-       die "undefined property '$prop'" if !$propinfo;
+       my $propinfo = $returnprops->{$prop} // {};
 
        my $title = $propinfo->{title} // $prop;
        my $cutoff = $propinfo->{print_width} // $propinfo->{maxLength};
@@ -480,7 +479,7 @@ sub print_text_table {
            $longest = $len if $len > $longest;
            $sortable = 0 if !defined($entry->{$prop});
        }
-       $cutoff = (defined($cutoff) && $cutoff < $longest) ? $cutoff : $longest;
+       $cutoff = $longest if !defined($cutoff) || $cutoff > $longest;
        $sort_key //= $prop if $sortable;
 
        $colopts->{$prop} = {
@@ -489,14 +488,15 @@ sub print_text_table {
            cutoff => $cutoff,
        };
 
+       # 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)) {
-       if ($returnprops->{$sort_key}->{type} eq 'integer' ||
-           $returnprops->{$sort_key}->{type} eq 'number') {
+       my $type = $returnprops->{$sort_key}->{type} // 'string';
+       if ($type eq 'integer' || $type eq 'number') {
            @$data = sort { $a->{$sort_key} <=> $b->{$sort_key} } @$data;
        } else {
            @$data = sort { $a->{$sort_key} cmp $b->{$sort_key} } @$data;