[pve-devel] [PATCH common 1/3] PVE::CLIHandler::print_text_table - fix sorting
Stoiko Ivanov
s.ivanov at proxmox.com
Fri Jun 22 20:21:06 CEST 2018
Sort only if at least one column contains no undefined values.
Sort according to the left-most such column.
Signed-off-by: Stoiko Ivanov <s.ivanov at proxmox.com>
---
src/PVE/CLIHandler.pm | 11 +++++++++--
1 file changed, 9 insertions(+), 2 deletions(-)
diff --git a/src/PVE/CLIHandler.pm b/src/PVE/CLIHandler.pm
index 5f3927d..b04326f 100644
--- a/src/PVE/CLIHandler.pm
+++ b/src/PVE/CLIHandler.pm
@@ -449,10 +449,11 @@ sub data_to_text {
# the last column will never be cutoff
# 'default' - optional default value for the column
# formatopts element order defines column order (left to right)
+# sorts the output according to the leftmost column not containing any undef
sub print_text_table {
my ($formatopts, $data) = @_;
- my ($formatstring, @keys, @titles, %cutoffs, %defaults);
+ my ($formatstring, @keys, @titles, %cutoffs, %defaults, $sort_key);
my $last_col = $formatopts->[$#{$formatopts}];
foreach my $col ( @$formatopts ) {
@@ -467,11 +468,14 @@ sub print_text_table {
my $titlelen = length($title);
my $longest = $titlelen;
+ my $sortable = 1;
foreach my $entry (@$data) {
my $len = length(data_to_text($entry->{$key})) // 0;
$longest = $len if $len > $longest;
+ $sortable = 0 if !defined($entry->{$key});
}
+ $sort_key //= $key if $sortable;
$cutoff = (defined($cutoff) && $cutoff < $longest) ? $cutoff : $longest;
$cutoffs{$key} = $cutoff;
@@ -485,7 +489,10 @@ sub print_text_table {
printf $formatstring, @titles;
- foreach my $entry (sort { $a->{$keys[0]} cmp $b->{$keys[0]} } @$data) {
+ if (defined($sort_key)){
+ @$data = sort { $a->{$sort_key} cmp $b->{$sort_key} } @$data;
+ }
+ foreach my $entry (@$data) {
printf $formatstring, map { substr((data_to_text($entry->{$_}) // $defaults{$_}), 0 , $cutoffs{$_}) } @keys;
}
}
--
2.11.0
More information about the pve-devel
mailing list