]> git.proxmox.com Git - pve-common.git/commitdiff
print_text_table: align numbers to the right side
authorDietmar Maurer <dietmar@proxmox.com>
Fri, 27 Jul 2018 12:00:28 +0000 (14:00 +0200)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Mon, 30 Jul 2018 13:51:59 +0000 (15:51 +0200)
Signed-off-by: Dietmar Maurer <dietmar@proxmox.com>
src/PVE/CLIFormatter.pm

index 119bc063cdc0785044e745767f9274137840bd43..d964b51cda6343d3a75454651fba6a04eea198fe 100644 (file)
@@ -226,6 +226,8 @@ sub print_text_table {
     for (my $i = 0; $i < $column_count; $i++) {
        my $prop = $props_to_print->[$i];
        my $propinfo = $returnprops->{$prop} // {};
     for (my $i = 0; $i < $column_count; $i++) {
        my $prop = $props_to_print->[$i];
        my $propinfo = $returnprops->{$prop} // {};
+       my $type = $propinfo->{type} // 'string';
+       my $alignstr = ($type eq 'integer' || $type eq 'number') ? '' : '-';
 
        my $title = $propinfo->{title} // $prop;
        my $cutoff = $propinfo->{print_width} // $propinfo->{maxLength};
 
        my $title = $propinfo->{title} // $prop;
        my $cutoff = $propinfo->{print_width} // $propinfo->{maxLength};
@@ -248,48 +250,48 @@ sub print_text_table {
        if ($border) {
            if ($i == 0 && ($column_count == 1)) {
                if ($utf8) {
        if ($border) {
            if ($i == 0 && ($column_count == 1)) {
                if ($utf8) {
-                   $formatstring .= "│ %-${cutoff}s │";
+                   $formatstring .= "│ %$alignstr${cutoff}s │";
                    $borderstring_t .= "┌─" . ('─' x $cutoff) . "─┐";
                    $borderstring_m .= "├─" . ('─' x $cutoff) . "─┤";
                    $borderstring_b .= "└─" . ('─' x $cutoff) . "─┘";
                } else {
                    $borderstring_t .= "┌─" . ('─' x $cutoff) . "─┐";
                    $borderstring_m .= "├─" . ('─' x $cutoff) . "─┤";
                    $borderstring_b .= "└─" . ('─' x $cutoff) . "─┘";
                } else {
-                   $formatstring .= "| %-${cutoff}s |";
+                   $formatstring .= "| %$alignstr${cutoff}s |";
                    $borderstring_m .= "+-" . ('-' x $cutoff) . "-+";
                }
            } elsif ($i == 0) {
                if ($utf8) {
                    $borderstring_m .= "+-" . ('-' x $cutoff) . "-+";
                }
            } elsif ($i == 0) {
                if ($utf8) {
-                   $formatstring .= "│ %-${cutoff}s ";
+                   $formatstring .= "│ %$alignstr${cutoff}s ";
                    $borderstring_t .= "┌─" . ('─' x $cutoff) . '─';
                    $borderstring_m .= "├─" . ('─' x $cutoff) . '─';
                    $borderstring_b .= "└─" . ('─' x $cutoff) . '─';
                } else {
                    $borderstring_t .= "┌─" . ('─' x $cutoff) . '─';
                    $borderstring_m .= "├─" . ('─' x $cutoff) . '─';
                    $borderstring_b .= "└─" . ('─' x $cutoff) . '─';
                } else {
-                   $formatstring .= "| %-${cutoff}s ";
+                   $formatstring .= "| %$alignstr${cutoff}s ";
                    $borderstring_m .= "+-" . ('-' x $cutoff) . '-';
                }
            } elsif ($i == ($column_count - 1)) {
                if ($utf8) {
                    $borderstring_m .= "+-" . ('-' x $cutoff) . '-';
                }
            } elsif ($i == ($column_count - 1)) {
                if ($utf8) {
-                   $formatstring .= "│ %-${cutoff}s │";
+                   $formatstring .= "│ %$alignstr${cutoff}s │";
                    $borderstring_t .= "┬─" . ('─' x $cutoff) . "─┐";
                    $borderstring_m .= "┼─" . ('─' x $cutoff) . "─┤";
                    $borderstring_b .= "┴─" . ('─' x $cutoff) . "─┘";
                } else {
                    $borderstring_t .= "┬─" . ('─' x $cutoff) . "─┐";
                    $borderstring_m .= "┼─" . ('─' x $cutoff) . "─┤";
                    $borderstring_b .= "┴─" . ('─' x $cutoff) . "─┘";
                } else {
-                   $formatstring .= "| %-${cutoff}s |";
+                   $formatstring .= "| %$alignstr${cutoff}s |";
                    $borderstring_m .= "+-" . ('-' x $cutoff) . "-+";
                }
            } else {
                if ($utf8) {
                    $borderstring_m .= "+-" . ('-' x $cutoff) . "-+";
                }
            } else {
                if ($utf8) {
-                   $formatstring .= "│ %-${cutoff}s ";
+                   $formatstring .= "│ %$alignstr${cutoff}s ";
                    $borderstring_t .= "┬─" . ('─' x $cutoff) . '─';
                    $borderstring_m .= "┼─" . ('─' x $cutoff) . '─';
                    $borderstring_b .= "┴─" . ('─' x $cutoff) . '─';
                } else {
                    $borderstring_t .= "┬─" . ('─' x $cutoff) . '─';
                    $borderstring_m .= "┼─" . ('─' x $cutoff) . '─';
                    $borderstring_b .= "┴─" . ('─' x $cutoff) . '─';
                } else {
-                   $formatstring .= "| %-${cutoff}s ";
+                   $formatstring .= "| %$alignstr${cutoff}s ";
                    $borderstring_m .= "+-" . ('-' x $cutoff) . '-';
                }
            }
        } else {
            # skip alignment and cutoff on last column
                    $borderstring_m .= "+-" . ('-' x $cutoff) . '-';
                }
            }
        } else {
            # skip alignment and cutoff on last column
-           $formatstring .= ($i == ($column_count - 1)) ? "%s" : "%-${cutoff}s ";
+           $formatstring .= ($i == ($column_count - 1)) ? "%s" : "%$alignstr${cutoff}s ";
        }
     }
 
        }
     }