File Coverage

blib/lib/App/JobLog/Command/last.pm
Criterion Covered Total %
statement 59 60 98.3
branch 28 30 93.3
condition 3 3 100.0
subroutine 7 8 87.5
pod 3 4 75.0
total 100 105 95.2


line stmt bran cond sub pod time code
1             package App::JobLog::Command::last;
2             $App::JobLog::Command::last::VERSION = '1.040';
3             # ABSTRACT: show details of last recorded event
4              
5 2     2   2189 use Modern::Perl;
  2         4  
  2         12  
6 2     2   232 use App::JobLog -command;
  2         5  
  2         14  
7 2         12 use Class::Autouse qw(
8             App::JobLog::Log
9             App::JobLog::Command::summary
10 2     2   756 );
  2         5  
11              
12             sub execute {
13 12     12 1 72 my ( $self, $opt, $args ) = @_;
14              
15             # construct event test
16 12 100       26 my %must = map { $_ => 1 } @{ $opt->tag || [] };
  8         62  
  12         48  
17 12 100       84 my %mustnt = map { $_ => 1 } @{ $opt->without || [] };
  8         68  
  12         53  
18             my $test = sub {
19 20     20   38 my $event = shift;
20 20 50       84 return if $event->is_note;
21 20         33 my @tags = @{ $event->tags };
  20         75  
22 20         38 my %tags = map { $_ => 1 } @tags;
  22         70  
23 20         42 my $good = 1;
24 20 100       61 if (%must) {
25 10 100       43 if ( $opt->any ) {
26 4         28 $good = 0;
27 4         15 for my $tag (@tags) {
28 4 100       23 if ( $must{$tag} ) {
29 3         10 $good = 1;
30 3         13 last;
31             }
32             }
33             }
34             else {
35 6         38 for my $tag ( keys %must ) {
36 8 100       23 unless ( $tags{$tag} ) {
37 3         9 $good = 0;
38 3         6 last;
39             }
40             }
41             }
42             }
43 20 100 100     136 if ( $good && %mustnt ) {
44 10 100       48 if ( $opt->some ) {
45 4         32 $good = 0;
46 4         15 for my $tag ( keys %mustnt ) {
47 5 100       29 unless ( $tags{$tag} ) {
48 3         11 $good = 1;
49 3         10 last;
50             }
51             }
52             }
53             else {
54 6         34 for my $tag (@tags) {
55 6 100       22 if ( $mustnt{$tag} ) {
56 4         16 $good = 0;
57 4         13 last;
58             }
59             }
60             }
61             }
62 20         109 return $good;
63 12         121 };
64              
65             # find event
66 12         117 my ( $i, $count, $e ) = ( App::JobLog::Log->new->reverse_iterator, 0 );
67 12         180 while ( $e = $i->() ) {
68 20         65 $count++;
69 20 100       54 last if $test->($e);
70             }
71              
72 12 100       48 if ($e) {
73 11         36 my $start = $e->start->strftime('%F at %H:%M:%S %p');
74 11 100       1301 my $end = $e->is_open ? 'now' : $e->end->strftime('%F at %H:%M:%S %p');
75 11         423 $opt->{merge} = 'no_merge';
76 11         140 App::JobLog::Command::summary->execute( $opt, ["$start - $end"] );
77             }
78             else {
79 1 50       14 say $count ? 'no matching event' : 'empty log';
80             }
81             }
82              
83 12     12 1 27775 sub usage_desc { '%c ' . __PACKAGE__->name }
84              
85 0     0 1 0 sub abstract { 'describe the last task recorded' }
86              
87             sub options {
88             return (
89             [
90 12     12 0 132 'tag|t=s@',
91             'find the last event with all of these tags; '
92             . 'multiple tags may be specified'
93             ],
94             [ 'any|a', 'require only that one of the --tag tags be present' ],
95             [
96             'without|w=s@',
97             'find the last event which does not have any of these tags; '
98             . 'multiple tags may be specified'
99             ],
100             [
101             'some|s', 'require only that some one of the --without tags be absent'
102             ],
103             );
104             }
105              
106             1;
107              
108             __END__