btrbk: tidy print_formatted

pull/409/head
Axel Burri 2021-03-28 17:02:15 +02:00
parent 37ef87ddaf
commit bd68b15ebc
1 changed files with 33 additions and 39 deletions

72
btrbk
View File

@ -5055,32 +5055,31 @@ sub print_formatted(@)
my $table_format = ref($format_key) ? $format_key : $table_formats{$format_key}; my $table_format = ref($format_key) ? $format_key : $table_formats{$format_key};
my $format = $args{output_format} || $output_format || $default_format; my $format = $args{output_format} || $output_format || $default_format;
my $pretty = $args{pretty} // $output_pretty; my $pretty = $args{pretty} // $output_pretty;
my $key_defs = $table_format->{$format}; my $no_header = $args{no_header};
my $ralign = $table_format->{RALIGN} // {};
my $fh = $args{outfile} // *STDOUT; my $fh = $args{outfile} // *STDOUT;
my $table_spacing = 2; my $table_spacing = 2;
my $empty_cell_char = $args{empty_cell_char} // "-"; my $empty_cell_char = $args{empty_cell_char} // "-";
my @keys;
my %ralign;
my %hide_column;
if($format =~ s/^col:\s*(h:)?\s*//) { if($format =~ s/^col:\s*(h:)?\s*//) {
$args{no_header} = 1 if($1); $no_header = 1 if($1);
$key_defs = []; $ralign = {};
foreach (split(/\s*,\s*/, $format)) { foreach (split(/\s*,\s*/, $format)) {
$ralign->{$_} = 1 if s/:R(ALIGN)?$//i; $ralign{$_} = 1 if s/:R(ALIGN)?$//i;
push @$key_defs, lc($_); push @keys, lc($_);
} }
} }
unless($key_defs) { else {
WARN "Unsupported output format \"$format\", defaulting to \"$default_format\" format."; unless(exists($table_format->{$format})) {
$key_defs = $table_format->{$default_format} || die; WARN "Unsupported output format \"$format\", defaulting to \"$default_format\" format.";
$format = $default_format; $format = $default_format;
} }
my @keys; @keys = @{$table_format->{$format}};
my %print_row; %ralign = %{$table_format->{RALIGN} // {}};
foreach (@$key_defs) {
my $kd = $_;
$print_row{$kd} = 1 unless($kd =~ s/^-//); # strip leading "-"
push @keys, $kd;
} }
# strips leading "-" from @keys
%hide_column = map { $_ => 1 } grep { s/^-// } @keys;
if($format eq "raw") if($format eq "raw")
{ {
@ -5093,7 +5092,7 @@ sub print_formatted(@)
elsif(($format eq "tlog") || ($format eq "syslog")) elsif(($format eq "tlog") || ($format eq "syslog"))
{ {
# output: value0 value1, ... # output: value0 value1, ...
unless($args{no_header}) { unless($no_header) {
print $fh join(' ', map uc($_), @keys) . "\n"; # unaligned upper case headings print $fh join(' ', map uc($_), @keys) . "\n"; # unaligned upper case headings
} }
foreach my $row (@$data) { foreach my $row (@$data) {
@ -5108,24 +5107,22 @@ sub print_formatted(@)
else else
{ {
# sanitize and calculate maxlen for each column # sanitize and calculate maxlen for each column
# NOTE: this is destructive on data! my %maxlen = map { $_ => $no_header ? 0 : length($_) } @keys;
my %maxlen; my @formatted_data;
my @sane_data;
foreach my $key (@keys) {
$maxlen{$key} = $args{no_header} ? 0 : length($key); # initialize with size of key
}
foreach my $row (@$data) { foreach my $row (@$data) {
my %formatted_row;
foreach my $key (@keys) { foreach my $key (@keys) {
my $val = $row->{$key}; my $val = $row->{$key};
if(ref $val eq "ARRAY") { $val = join(',', @$val) if(ref $val eq "ARRAY");
$val = join(',', @{$val});
} $hide_column{$key} = 0 if(defined($val));
$print_row{$key} = 1 if(defined($val));
$val = $empty_cell_char if(!defined($val) || ($val eq "")); $val = $empty_cell_char if(!defined($val) || ($val eq ""));
$row->{$key} = $val; # write back the sanitized value $formatted_row{$key} = $val;
$maxlen{$key} = length($val) if($maxlen{$key} < length($val)); $maxlen{$key} = length($val) if($maxlen{$key} < length($val));
} }
push @formatted_data, \%formatted_row;
} }
my @visible_keys = grep !$hide_column{$_}, @keys;
# print title # print title
if($title) { if($title) {
@ -5134,15 +5131,14 @@ sub print_formatted(@)
} }
# print keys (headings) # print keys (headings)
unless($args{no_header}) { unless($no_header) {
my $fill = 0; my $fill = 0;
foreach (@keys) { foreach (@visible_keys) {
next unless($print_row{$_});
print $fh ' ' x $fill; print $fh ' ' x $fill;
$fill = $maxlen{$_} - length($_); $fill = $maxlen{$_} - length($_);
if($pretty) { if($pretty) {
# use aligned lower case headings (with separator line below) # use aligned lower case headings (with separator line below)
if($ralign->{$_}) { if($ralign{$_}) {
print $fh ' ' x $fill; print $fh ' ' x $fill;
$fill = 0; $fill = 0;
} }
@ -5156,8 +5152,7 @@ sub print_formatted(@)
$fill = 0; $fill = 0;
if($pretty) { # separator line after header if($pretty) { # separator line after header
foreach (@keys) { foreach (@visible_keys) {
next unless($print_row{$_});
print $fh ' ' x $fill; print $fh ' ' x $fill;
print $fh '-' x $maxlen{$_}; print $fh '-' x $maxlen{$_};
$fill = $table_spacing; $fill = $table_spacing;
@ -5169,14 +5164,13 @@ sub print_formatted(@)
} }
# print values # print values
foreach my $row (@$data) { foreach my $row (@formatted_data) {
my $fill = 0; my $fill = 0;
foreach (@keys) { foreach (@visible_keys) {
next unless($print_row{$_});
my $val = $row->{$_}; my $val = $row->{$_};
print $fh ' ' x $fill; print $fh ' ' x $fill;
$fill = $maxlen{$_} - length($val); $fill = $maxlen{$_} - length($val);
if($ralign->{$_}) { if($ralign{$_}) {
print $fh ' ' x $fill; print $fh ' ' x $fill;
$fill = 0; $fill = 0;
} }