cli format: refactoring and code bloat reduction

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
This commit is contained in:
Thomas Lamprecht 2021-10-22 17:48:02 +02:00
parent d37a718672
commit 507310df34

View File

@ -104,8 +104,8 @@ sub print_text_table {
$terminal_opts //= query_terminal_options({}); $terminal_opts //= query_terminal_options({});
my $sort_key = $options->{sort_key}; my $sort_key = $options->{sort_key};
my $border = !$options->{noborder}; my $show_border = !$options->{noborder};
my $header = !$options->{noheader}; my $show_header = !$options->{noheader};
my $columns = $terminal_opts->{columns}; my $columns = $terminal_opts->{columns};
my $utf8 = $terminal_opts->{utf8}; my $utf8 = $terminal_opts->{utf8};
@ -128,10 +128,7 @@ sub print_text_table {
my $colopts = {}; my $colopts = {};
my $borderstring_m = ''; my $border = { m => '', b => '', t => '', h => '' };
my $borderstring_b = '';
my $borderstring_t = '';
my $borderstring_h = '';
my $formatstring = ''; my $formatstring = '';
my $column_count = scalar(@$props_to_print); my $column_count = scalar(@$props_to_print);
@ -196,54 +193,54 @@ sub print_text_table {
cutoff => $cutoff, cutoff => $cutoff,
}; };
if ($border) { if ($show_border) {
if ($i == 0 && ($column_count == 1)) { if ($i == 0 && ($column_count == 1)) {
if ($utf8) { if ($utf8) {
$formatstring .= "│ %$alignstr${cutoff}s │"; $formatstring .= "│ %$alignstr${cutoff}s │";
$borderstring_t .= "┌─" . ('─' x $cutoff) . "─┐"; $border->{t} .= "┌─" . ('─' x $cutoff) . "─┐";
$borderstring_h .= "╞═" . ('═' x $cutoff) . '═╡'; $border->{h} .= "╞═" . ('═' x $cutoff) . '═╡';
$borderstring_m .= "├─" . ('─' x $cutoff) . "─┤"; $border->{m} .= "├─" . ('─' x $cutoff) . "─┤";
$borderstring_b .= "└─" . ('─' x $cutoff) . "─┘"; $border->{b} .= "└─" . ('─' x $cutoff) . "─┘";
} else { } else {
$formatstring .= "| %$alignstr${cutoff}s |"; $formatstring .= "| %$alignstr${cutoff}s |";
$borderstring_m .= "+-" . ('-' x $cutoff) . "-+"; $border->{m} .= "+-" . ('-' x $cutoff) . "-+";
$borderstring_h .= "+=" . ('=' x $cutoff) . '='; $border->{h} .= "+=" . ('=' x $cutoff) . '=';
} }
} elsif ($i == 0) { } elsif ($i == 0) {
if ($utf8) { if ($utf8) {
$formatstring .= "│ %$alignstr${cutoff}s "; $formatstring .= "│ %$alignstr${cutoff}s ";
$borderstring_t .= "┌─" . ('─' x $cutoff) . '─'; $border->{t} .= "┌─" . ('─' x $cutoff) . '─';
$borderstring_h .= "╞═" . ('═' x $cutoff) . '═'; $border->{h} .= "╞═" . ('═' x $cutoff) . '═';
$borderstring_m .= "├─" . ('─' x $cutoff) . '─'; $border->{m} .= "├─" . ('─' x $cutoff) . '─';
$borderstring_b .= "└─" . ('─' x $cutoff) . '─'; $border->{b} .= "└─" . ('─' x $cutoff) . '─';
} else { } else {
$formatstring .= "| %$alignstr${cutoff}s "; $formatstring .= "| %$alignstr${cutoff}s ";
$borderstring_m .= "+-" . ('-' x $cutoff) . '-'; $border->{m} .= "+-" . ('-' x $cutoff) . '-';
$borderstring_h .= "+=" . ('=' x $cutoff) . '='; $border->{h} .= "+=" . ('=' x $cutoff) . '=';
} }
} elsif ($i == ($column_count - 1)) { } elsif ($i == ($column_count - 1)) {
if ($utf8) { if ($utf8) {
$formatstring .= "│ %$alignstr${cutoff}s │"; $formatstring .= "│ %$alignstr${cutoff}s │";
$borderstring_t .= "┬─" . ('─' x $cutoff) . "─┐"; $border->{t} .= "┬─" . ('─' x $cutoff) . "─┐";
$borderstring_h .= "╪═" . ('═' x $cutoff) . '═╡'; $border->{h} .= "╪═" . ('═' x $cutoff) . '═╡';
$borderstring_m .= "┼─" . ('─' x $cutoff) . "─┤"; $border->{m} .= "┼─" . ('─' x $cutoff) . "─┤";
$borderstring_b .= "┴─" . ('─' x $cutoff) . "─┘"; $border->{b} .= "┴─" . ('─' x $cutoff) . "─┘";
} else { } else {
$formatstring .= "| %$alignstr${cutoff}s |"; $formatstring .= "| %$alignstr${cutoff}s |";
$borderstring_m .= "+-" . ('-' x $cutoff) . "-+"; $border->{m} .= "+-" . ('-' x $cutoff) . "-+";
$borderstring_h .= "+=" . ('=' x $cutoff) . "=+"; $border->{h} .= "+=" . ('=' x $cutoff) . "=+";
} }
} else { } else {
if ($utf8) { if ($utf8) {
$formatstring .= "│ %$alignstr${cutoff}s "; $formatstring .= "│ %$alignstr${cutoff}s ";
$borderstring_t .= "┬─" . ('─' x $cutoff) . '─'; $border->{t} .= "┬─" . ('─' x $cutoff) . '─';
$borderstring_h .= "╪═" . ('═' x $cutoff) . '═'; $border->{h} .= "╪═" . ('═' x $cutoff) . '═';
$borderstring_m .= "┼─" . ('─' x $cutoff) . '─'; $border->{m} .= "┼─" . ('─' x $cutoff) . '─';
$borderstring_b .= "┴─" . ('─' x $cutoff) . '─'; $border->{b} .= "┴─" . ('─' x $cutoff) . '─';
} else { } else {
$formatstring .= "| %$alignstr${cutoff}s "; $formatstring .= "| %$alignstr${cutoff}s ";
$borderstring_m .= "+-" . ('-' x $cutoff) . '-'; $border->{m} .= "+-" . ('-' x $cutoff) . '-';
$borderstring_h .= "+=" . ('=' x $cutoff) . '='; $border->{h} .= "+=" . ('=' x $cutoff) . '=';
} }
} }
} else { } else {
@ -252,8 +249,8 @@ sub print_text_table {
} }
} }
$borderstring_t = $borderstring_m if !length($borderstring_t); $border->{t} = $border->{m} if !length($border->{t});
$borderstring_b = $borderstring_m if !length($borderstring_b); $border->{b} = $border->{m} if !length($border->{b});
my $writeln = sub { my $writeln = sub {
my ($text) = @_; my ($text) = @_;
@ -265,27 +262,25 @@ sub print_text_table {
} }
}; };
$writeln->($borderstring_t) if $border; $writeln->($border->{t}) if $show_border;
my $borderstring_sep; if ($show_header) {
if ($header) {
my $text = sprintf $formatstring, map { $colopts->{$_}->{title} } @$props_to_print; my $text = sprintf $formatstring, map { $colopts->{$_}->{title} } @$props_to_print;
$writeln->($text); $writeln->($text);
$borderstring_sep = $borderstring_h; $border->{sep} = $border->{h};
} else { } else {
$borderstring_sep = $borderstring_m; $border->{sep} = $border->{m};
} }
for (my $i = 0; $i < scalar(@$tabledata); $i++) { for (my $i = 0; $i < scalar(@$tabledata); $i++) {
my $coldata = $tabledata->[$i]; my $coldata = $tabledata->[$i];
if ($border && ($i != 0 || $header)) { if ($show_border && ($i != 0 || $show_header)) {
$writeln->($borderstring_sep); $writeln->($border->{sep});
$borderstring_sep = $borderstring_m; $border->{sep} = $border->{m};
} }
for (my $i = 0; $i < $coldata->{height}; $i++) { for (my $i = 0; $i < $coldata->{height}; $i++) {
my $text = sprintf $formatstring, map { my $text = sprintf $formatstring, map {
substr($coldata->{rowdata}->{$_}->{lines}->[$i] // '', 0, $colopts->{$_}->{cutoff}); substr($coldata->{rowdata}->{$_}->{lines}->[$i] // '', 0, $colopts->{$_}->{cutoff});
} @$props_to_print; } @$props_to_print;
@ -294,7 +289,7 @@ sub print_text_table {
} }
} }
$writeln->($borderstring_b) if $border; $writeln->($border->{b}) if $show_border;
} }
sub extract_properties_to_print { sub extract_properties_to_print {