feat: autodetect column size

This commit is contained in:
Stéphane Lesimple 2022-01-03 12:51:29 +01:00
parent 5c3540a0b5
commit 7e66d9b270

View File

@ -823,36 +823,47 @@ foreach my $fuuid (sort keys %filesystems) {
push @orderedAll, @ordered; push @orderedAll, @ordered;
} }
# find the longest path (including leading spaces) # this sub returns the length of the longest item of a column or @sortedAll
my $longestpath = 0; # and pushes the header name to @headers
foreach (@orderedAll) { my @header;
my $len = ($_->{depth} || 0) * 3;
$len += length($_->{path}); sub longest {
$len > $longestpath and $longestpath = $len; my $headerName = shift;
} my $useDepth = shift; # whether 'depth' should be taken into account
my $pretty_print_size = ($opt_raw ? 16 : 7); my $key = shift;
# ensure the header name always fits
my $longest = ($opt_no_header ? 1 : length($headerName));
push @header, $headerName;
# loop through all the items
foreach my $item (@orderedAll) {
my $len = ($useDepth ? (($item->{depth} || 0) * 3) : 0);
$len += length($item->{$key} || '');
$longest = $len if $len > $longest;
}
return $longest;
}
# find the longest path (including leading spaces)
# note that longest() also pushes the header to @headers
my $format = "%-" . longest('NAME', 1, 'path') . "s ";
my $format = "%-${longestpath}s ";
my @header = qw{ NAME };
if ($opt_show_id) { if ($opt_show_id) {
$format .= "%6s "; $format .= "%" . longest('ID', 0, 'id') . "s ";
push @header, qw{ ID };
} }
if ($opt_show_parent) { if ($opt_show_parent) {
$format .= "%6s "; $format .= "%" . longest('PARENT', 0, 'parent') . "s ";
push @header, qw{ PARENT };
} }
if ($opt_show_toplevel) { if ($opt_show_toplevel) {
$format .= "%6s "; $format .= "%" . longest('TOPLVL', 0, 'top') . "s ";
push @header, qw{ TOPLVL };
} }
if ($opt_show_gen) { if ($opt_show_gen) {
$format .= "%8s "; $format .= "%" . longest('GEN', 0, 'gen') . "s ";
push @header, qw{ GEN };
} }
if ($opt_show_cgen) { if ($opt_show_cgen) {
$format .= "%8s "; $format .= "%" . longest('CGEN', 0, 'cgen') . "s ";
push @header, qw{ CGEN };
} }
my $uuid_len = ($opt_wide ? 36 : 10); my $uuid_len = ($opt_wide ? 36 : 10);
if ($opt_show_uuid) { if ($opt_show_uuid) {
@ -871,10 +882,10 @@ if ($opt_show_otime) {
$format .= "%20s "; $format .= "%20s ";
push @header, qw{ OTIME }; push @header, qw{ OTIME };
} }
$format .= "%8s "; $format .= "%" . longest('TYPE', 0, 'type') . "s ";
push @header, qw{ TYPE };
my $noquota = $vol{$fuuid}{df}{noquota} || $opt_free_space; my $pretty_print_size = ($opt_raw ? 16 : 7);
my $noquota = $vol{$fuuid}{df}{noquota} || $opt_free_space;
if (!$noquota) { if (!$noquota) {
$format .= "%s%${pretty_print_size}s%s%1s%s "; $format .= "%s%${pretty_print_size}s%s%1s%s ";
push @header, '', 'REFE', 'R', '', ''; push @header, '', 'REFE', 'R', '', '';