File Coverage

blib/lib/App/JobLog/Command/resume.pm
Criterion Covered Total %
statement 12 68 17.6
branch 0 32 0.0
condition 0 3 0.0
subroutine 4 10 40.0
pod 3 5 60.0
total 19 118 16.1


line stmt bran cond sub pod time code
1             package App::JobLog::Command::resume;
2             $App::JobLog::Command::resume::VERSION = '1.039';
3             # ABSTRACT: resume last closed task
4              
5 2     2   1593 use App::JobLog -command;
  2         4  
  2         14  
6 2     2   740 use Modern::Perl;
  2         4  
  2         12  
7 2     2   208 use Class::Autouse 'App::JobLog::Log';
  2         4  
  2         10  
8 2     2   94 use autouse 'App::JobLog::Time' => qw(now);
  2         4  
  2         10  
9              
10             sub execute {
11 0     0 1   my ( $self, $opt, $args ) = @_;
12              
13             # construct event test
14 0 0         my %must = map { $_ => 1 } @{ $opt->tag || [] };
  0            
  0            
15 0 0         my %mustnt = map { $_ => 1 } @{ $opt->without || [] };
  0            
  0            
16             my $test = sub {
17 0     0     my $event = shift;
18 0           my @tags = @{ $event->tags };
  0            
19 0           my %tags = map { $_ => 1 } @tags;
  0            
20 0           my $good = 1;
21 0 0         if (%must) {
22 0 0         if ( $opt->any ) {
23 0           $good = 0;
24 0           for my $tag (@tags) {
25 0 0         if ( $must{$tag} ) {
26 0           $good = 1;
27 0           last;
28             }
29             }
30             }
31             else {
32 0           for my $tag ( keys %must ) {
33 0 0         unless ( $tags{$tag} ) {
34 0           $good = 0;
35 0           last;
36             }
37             }
38             }
39             }
40 0 0 0       if ( $good && %mustnt ) {
41 0 0         if ( $opt->some ) {
42 0           $good = 0;
43 0           for my $tag ( keys %mustnt ) {
44 0 0         unless ( $tags{$tag} ) {
45 0           $good = 1;
46 0           last;
47             }
48             }
49             }
50             else {
51 0           for my $tag (@tags) {
52 0 0         if ( $mustnt{$tag} ) {
53 0           $good = 0;
54 0           last;
55             }
56             }
57             }
58             }
59 0           return $good;
60 0           };
61              
62             # find event
63 0           my $log = App::JobLog::Log->new;
64 0           my ( $i, $count, $e ) = ( $log->reverse_iterator, 0 );
65 0           while ( $e = $i->() ) {
66 0           $count++;
67 0 0         next unless $e->isa('App::JobLog::Log::Event');
68 0 0         last if $test->($e);
69             }
70              
71 0 0         $self->usage_error('empty log') unless $count;
72 0 0         $self->usage_error('no matching event') unless $e;
73 0 0         $self->usage_error('event ongoing') unless $e->is_closed;
74              
75 0           my $ll = $e->data->clone;
76 0           $ll->time = now;
77 0           $log->append_event($ll);
78 0           print "resuming " . $e->describe;
79 0 0         print ' (tags: ', join( ', ', sort $e->tag_list ), ')' if $e->tagged;
80 0           print "\n";
81             }
82              
83 0     0 1   sub usage_desc { '%c ' . __PACKAGE__->name . ' %o' }
84              
85 0     0 1   sub abstract { 'resume last task' }
86              
87             sub full_description {
88             <
89             Starts a new task with an identical description and tags to the last
90             task in the log. If some restriction by tag is specified, it is the last
91             task with the given tags.
92             END
93 0     0 0   }
94              
95             sub options {
96             return (
97             [
98 0     0 0   'tag|t=s@',
99             'resume the last event with all of these tags; '
100             . 'multiple tags may be specified'
101             ],
102             [ 'any|a', 'require only that one of the --tag tags be present' ],
103             [
104             'without|w=s@',
105             'resume the last event which does not have any of these tags; '
106             . 'multiple tags may be specified'
107             ],
108             [
109             'some|s', 'require only that some one of the --without tags be absent'
110             ],
111             );
112             }
113              
114             1;
115              
116             __END__