From: Andrew Chilton Date: Sun, 31 Jul 2011 02:59:22 +0000 (+1200) Subject: Issue:6 - Allow ability to --order on various fields (also --reverse has been added... X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=e3e767288777a0b15619724a908d38177a92dd31;p=cil.git Issue:6 - Allow ability to --order on various fields (also --reverse has been added too) --- diff --git a/bin/cil b/bin/cil index 5c210d8..2d5afe6 100755 --- a/bin/cil +++ b/bin/cil @@ -51,14 +51,17 @@ my @IN_OPTS = ( 'created-by>c', # for 'summary', 'list' 'a=s', # a = assigned_to 'assigned-to>a',# for 'summary', 'list' + 'o=s', # o = order + 'order>o', # for 'summary', 'list' 'r=s', # r = revision - 'revision>s', # for all query commands + 'revision>r', # for all query commands # booleans 'bare', # for 'init' 'is-open', # for 'summary', 'list' 'is-closed', # for 'summary', 'list' 'is-mine', # for 'summary', 'list' + 'reverse', # for 'summary', 'list' 'mine', # for 'add' 'add', # for 'add', 'comment' 'commit', # for 'add', 'comment' @@ -75,6 +78,7 @@ my %BOOLEAN_ARGS = ( 'is-open' => 1, 'is-closed' => 1, 'is-mine' => 1, + 'reverse' => 1, 'mine' => 1, 'add' => 1, 'commit' => 1, @@ -177,7 +181,9 @@ cil - the command-line issue list $ cil list $ cil list --status=New $ cil list --label=Release-v0.1 - $ cil list --is-open + $ cil list --is-open --order=Updated + $ cil list --order=Updated --reverse + $ cil list --order=Inserted $ cil add ... added issue 'cafebabe' ... @@ -341,6 +347,34 @@ C. =back +=head1 ORDER + +When doing a 'summary' or a 'list', as well as filtering the issues to only +show what you want, you can also order them to show them in whichever order you +like too. The fields you can order by are: + + --order=Summary + --order=Inserted + --order=Updated + --order=AssignedTo + --order=CreatedBy + --order=DueDate + --order=Status + +In the case of Summary, Inserted, Updated, CreatedBy and Status the field is +always defined and hence the ordering will take every issue into +account. However, some issues may not have been assigned or have a due date, so +AssignedTo and DueDate may be blank. In this case, any issues with these fields +defined are ordered and any issues which do not have this field are placed +towards the bottom of the listing. + +=head2 REVERSE + +Every single one of these orderings can also be reversed using the C<--reverse> +flag. If an ordering is reversed it works as expected but again for issues +where that field is undefined they are placed at the bottom (rather than having +the undefined values float to the top). + =head1 .cil The C<.cil> file is used to configure bits and pieces within cil for this @@ -455,7 +489,7 @@ Andrew Chilton =head1 COPYRIGHT -Copyright (C) 2008 by Andrew Chilton +Copyright (C) 2008-2011 by Andrew Chilton Cil is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, diff --git a/lib/CIL/Command/List.pm b/lib/CIL/Command/List.pm index 98fbad4..6b67ff9 100644 --- a/lib/CIL/Command/List.pm +++ b/lib/CIL/Command/List.pm @@ -37,7 +37,8 @@ sub run { my $issues = $cil->get_issues(); $issues = CIL::Utils->filter_issues( $cil, $issues, $args ); if ( @$issues ) { - foreach my $issue ( sort { $a->Inserted cmp $b->Inserted } @$issues ) { + $issues = CIL::Utils->order_issues( $cil, $issues, $args ); + foreach my $issue ( @$issues ) { CIL::Utils->separator(); CIL::Utils->display_issue_headers($issue); } diff --git a/lib/CIL/Command/Summary.pm b/lib/CIL/Command/Summary.pm index 2a5e325..c22b8ea 100644 --- a/lib/CIL/Command/Summary.pm +++ b/lib/CIL/Command/Summary.pm @@ -38,6 +38,7 @@ sub run { $issues = CIL::Utils->filter_issues( $cil, $issues, $args ); if ( @$issues ) { CIL::Utils->separator(); + $issues = CIL::Utils->order_issues( $cil, $issues, $args ); foreach my $issue ( @$issues ) { CIL::Utils->display_issue_summary($issue); } diff --git a/lib/CIL/Utils.pm b/lib/CIL/Utils.pm index 6ca3cf4..a76f513 100644 --- a/lib/CIL/Utils.pm +++ b/lib/CIL/Utils.pm @@ -473,6 +473,48 @@ sub filter_issues { return \@new_issues; } +my $valid_order = { + Summary => 1, + Inserted => 1, + Updated => 1, + AssignedTo => 1, + CreatedBy => 1, + DueDate => 1, + Status => 1, +}; + +sub order_issues { + my ($class, $cil, $issues, $args) = @_; + + # don't order if we haven't been given anything + return $issues unless defined $args; + return $issues unless %$args; + return $issues unless defined $args->{o}; + + my $order_by = $args->{o}; + + # make sure that the order is allowed + unless ( exists $valid_order->{$order_by} ) { + $class->fatal("the --order must be one of: " . join(', ', sort keys %$valid_order)); + } + + # Split off the ones with this field defined/not defined. + my @defined = grep { $_->{data}{$order_by} } @$issues; + my @undefined = grep { ! $_->{data}{$order_by} } @$issues; + + @$issues = sort { $a->{data}{$order_by} cmp $b->{data}{$order_by} } @defined; + + # reverse the order if specified + if ( $args->{reverse} ) { + @$issues = reverse @$issues; + } + + # now put the undefined ones back (so they're at the bottom independent of --order and --reverse) + push @$issues, @undefined; + + return $issues; +} + sub separator { my ($class) = @_; $class->msg('=' x 79);