]> git.proxmox.com Git - pve-common.git/blobdiff - src/PVE/CLIHandler.pm
cli: print_text_table: followup code cleanup
[pve-common.git] / src / PVE / CLIHandler.pm
index 9cf79886acc009953dd35e130c2329bddf1553f3..b939a22affed43746ea38744f22b6717d6c56447 100644 (file)
@@ -480,7 +480,7 @@ sub print_text_table {
            $longest = $len if $len > $longest;
            $sortable = 0 if !defined($entry->{$prop});
        }
            $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} = {
        $sort_key //= $prop if $sortable;
 
        $colopts->{$prop} = {
@@ -489,14 +489,15 @@ sub print_text_table {
            cutoff => $cutoff,
        };
 
            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)) {
        $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};
+       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;
            @$data = sort { $a->{$sort_key} <=> $b->{$sort_key} } @$data;
        } else {
            @$data = sort { $a->{$sort_key} cmp $b->{$sort_key} } @$data;