btrbk: print_formatted: add skip-row-if-empty functionality for table_formats

If the row key is prefixed with "-", and is all <undef>, omit output
of this row.
pull/274/head
Axel Burri 2019-04-01 03:24:00 +02:00
parent 805d7f4a0d
commit 716d420a40
1 changed files with 30 additions and 11 deletions

41
btrbk
View File

@ -4243,33 +4243,39 @@ sub print_formatted(@)
my %args = @_; my %args = @_;
my $title = $args{title}; my $title = $args{title};
my $format = $args{output_format} || $output_format || $default_format; my $format = $args{output_format} || $output_format || $default_format;
my $keys = $table_formats{$format_key}->{$format}; my $key_defs = $table_formats{$format_key}->{$format};
my $ralign = $table_formats{$format_key}->{RALIGN} // {}; my $ralign = $table_formats{$format_key}->{RALIGN} // {};
my $fh = $args{outfile} // *STDOUT; my $fh = $args{outfile} // *STDOUT;
my $table_spacing = 2; my $table_spacing = 2;
unless($keys) { unless($key_defs) {
WARN "Unsupported output format \"$format\", defaulting to \"$default_format\" format."; WARN "Unsupported output format \"$format\", defaulting to \"$default_format\" format.";
$keys = $table_formats{$format_key}->{$default_format} || die; $key_defs = $table_formats{$format_key}->{$default_format} || die;
$format = $default_format; $format = $default_format;
} }
my @keys;
my %print_row;
foreach (@$key_defs) {
$print_row{$_} = 1 unless(s/^-//); # strip leading "-"
push @keys, $_;
}
if($format eq "raw") if($format eq "raw")
{ {
# output: key0="value0" key1="value1" ... # output: key0="value0" key1="value1" ...
foreach my $row (@$data) { foreach my $row (@$data) {
print $fh "format=\"$format_key\" "; print $fh "format=\"$format_key\" ";
print $fh join(' ', map { "$_=\"" . ($row->{$_} // "") . "\""; } @$keys) . "\n"; print $fh join(' ', map { "$_=\"" . ($row->{$_} // "") . "\""; } @keys) . "\n";
} }
} }
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($args{no_header}) {
print $fh join(' ', @$keys) . "\n"; print $fh join(' ', @keys) . "\n";
} }
foreach my $row (@$data) { foreach my $row (@$data) {
my $line = join(' ', map { ((defined($row->{$_}) && ($_ eq "message")) ? '# ' : '') . ($row->{$_} // "-") } @$keys); my $line = join(' ', map { ((defined($row->{$_}) && ($_ eq "message")) ? '# ' : '') . ($row->{$_} // "-") } @keys);
if($format eq "syslog") { # dirty hack, ignore outfile on syslog format if($format eq "syslog") { # dirty hack, ignore outfile on syslog format
syslog($line); syslog($line);
} else { } else {
@ -4283,15 +4289,16 @@ sub print_formatted(@)
# NOTE: this is destructive on data! # NOTE: this is destructive on data!
my %maxlen; my %maxlen;
my @sane_data; my @sane_data;
foreach my $key (@$keys) { foreach my $key (@keys) {
$maxlen{$key} = length($key); # initialize with size of key $maxlen{$key} = length($key); # initialize with size of key
} }
foreach my $row (@$data) { foreach my $row (@$data) {
foreach my $key (@$keys) { foreach my $key (@keys) {
my $val = $row->{$key}; my $val = $row->{$key};
if(ref $val eq "ARRAY") { if(ref $val eq "ARRAY") {
$val = join(',', @{$val}); $val = join(',', @{$val});
} }
$print_row{$key} = 1 if(defined($val));
$val //= "-"; $val //= "-";
$val = "-" if($val eq ""); $val = "-" if($val eq "");
$row->{$key} = $val; # write back the sanitized value $row->{$key} = $val; # write back the sanitized value
@ -4307,7 +4314,8 @@ sub print_formatted(@)
# print keys (headings) # print keys (headings)
unless($args{no_header}) { unless($args{no_header}) {
my $fill = 0; my $fill = 0;
foreach (@$keys) { foreach (@keys) {
next unless($print_row{$_});
print $fh ' ' x $fill; print $fh ' ' x $fill;
$fill = $maxlen{$_} - length($_); $fill = $maxlen{$_} - length($_);
if($ralign->{$_}) { if($ralign->{$_}) {
@ -4318,13 +4326,24 @@ sub print_formatted(@)
$fill += $table_spacing; $fill += $table_spacing;
} }
print $fh "\n"; print $fh "\n";
print $fh join(' ' x $table_spacing, map { '-' x ($maxlen{$_}) } @$keys) . "\n";
$fill = 0;
foreach (@keys) {
next unless($print_row{$_});
print $fh ' ' x $fill;
print $fh '-' x $maxlen{$_};
$fill = $table_spacing;
}
# alternatively (all above in one line ;)
#print $fh join(' ' x $table_spacing, map { '-' x ($maxlen{$_}) } @keys) . "\n";
print $fh "\n";
} }
# print values # print values
foreach my $row (@$data) { foreach my $row (@$data) {
my $fill = 0; my $fill = 0;
foreach (@$keys) { foreach (@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);