[pve-devel] applied: [pve-common] print_text_table: align numbers to the right side

Thomas Lamprecht t.lamprecht at proxmox.com
Mon Jul 30 16:04:02 CEST 2018


Am 07/27/2018 um 02:00 PM schrieb Dietmar Maurer:
> Signed-off-by: Dietmar Maurer <dietmar at proxmox.com>
> ---
>   src/PVE/CLIFormatter.pm | 20 +++++++++++---------
>   1 file changed, 11 insertions(+), 9 deletions(-)
> 
> diff --git a/src/PVE/CLIFormatter.pm b/src/PVE/CLIFormatter.pm
> index f6ad459..ff0e264 100644
> --- a/src/PVE/CLIFormatter.pm
> +++ b/src/PVE/CLIFormatter.pm
> @@ -224,6 +224,8 @@ sub print_text_table {
>       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};
> @@ -246,48 +248,48 @@ sub print_text_table {
>   	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 {
> -		    $formatstring .= "| %-${cutoff}s |";
> +		    $formatstring .= "| %$alignstr${cutoff}s |";
>   		    $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 {
> -		    $formatstring .= "| %-${cutoff}s ";
> +		    $formatstring .= "| %$alignstr${cutoff}s ";
>   		    $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 {
> -		    $formatstring .= "| %-${cutoff}s |";
> +		    $formatstring .= "| %$alignstr${cutoff}s |";
>   		    $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 {
> -		    $formatstring .= "| %-${cutoff}s ";
> +		    $formatstring .= "| %$alignstr${cutoff}s ";
>   		    $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 ";
>   	}
>       }
>   
> 
applied




More information about the pve-devel mailing list