| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package App::JobLog::Command::resume; | 
| 2 |  |  |  |  |  |  | $App::JobLog::Command::resume::VERSION = '1.040'; | 
| 3 |  |  |  |  |  |  | # ABSTRACT: resume last closed task | 
| 4 |  |  |  |  |  |  |  | 
| 5 | 2 |  |  | 2 |  | 1853 | use App::JobLog -command; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 15 |  | 
| 6 | 2 |  |  | 2 |  | 724 | use Modern::Perl; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 11 |  | 
| 7 | 2 |  |  | 2 |  | 215 | use Class::Autouse 'App::JobLog::Log'; | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 13 |  | 
| 8 | 2 |  |  | 2 |  | 94 | use autouse 'App::JobLog::Time' => qw(now); | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 11 |  | 
| 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 | 0 |  |  |  |  | return if $event->is_note; | 
| 19 | 0 |  |  |  |  |  | my @tags  = @{ $event->tags }; | 
|  | 0 |  |  |  |  |  |  | 
| 20 | 0 |  |  |  |  |  | my %tags  = map { $_ => 1 } @tags; | 
|  | 0 |  |  |  |  |  |  | 
| 21 | 0 |  |  |  |  |  | my $good  = 1; | 
| 22 | 0 | 0 |  |  |  |  | if (%must) { | 
| 23 | 0 | 0 |  |  |  |  | if ( $opt->any ) { | 
| 24 | 0 |  |  |  |  |  | $good = 0; | 
| 25 | 0 |  |  |  |  |  | for my $tag (@tags) { | 
| 26 | 0 | 0 |  |  |  |  | if ( $must{$tag} ) { | 
| 27 | 0 |  |  |  |  |  | $good = 1; | 
| 28 | 0 |  |  |  |  |  | last; | 
| 29 |  |  |  |  |  |  | } | 
| 30 |  |  |  |  |  |  | } | 
| 31 |  |  |  |  |  |  | } | 
| 32 |  |  |  |  |  |  | else { | 
| 33 | 0 |  |  |  |  |  | for my $tag ( keys %must ) { | 
| 34 | 0 | 0 |  |  |  |  | unless ( $tags{$tag} ) { | 
| 35 | 0 |  |  |  |  |  | $good = 0; | 
| 36 | 0 |  |  |  |  |  | last; | 
| 37 |  |  |  |  |  |  | } | 
| 38 |  |  |  |  |  |  | } | 
| 39 |  |  |  |  |  |  | } | 
| 40 |  |  |  |  |  |  | } | 
| 41 | 0 | 0 | 0 |  |  |  | if ( $good && %mustnt ) { | 
| 42 | 0 | 0 |  |  |  |  | if ( $opt->some ) { | 
| 43 | 0 |  |  |  |  |  | $good = 0; | 
| 44 | 0 |  |  |  |  |  | for my $tag ( keys %mustnt ) { | 
| 45 | 0 | 0 |  |  |  |  | unless ( $tags{$tag} ) { | 
| 46 | 0 |  |  |  |  |  | $good = 1; | 
| 47 | 0 |  |  |  |  |  | last; | 
| 48 |  |  |  |  |  |  | } | 
| 49 |  |  |  |  |  |  | } | 
| 50 |  |  |  |  |  |  | } | 
| 51 |  |  |  |  |  |  | else { | 
| 52 | 0 |  |  |  |  |  | for my $tag (@tags) { | 
| 53 | 0 | 0 |  |  |  |  | if ( $mustnt{$tag} ) { | 
| 54 | 0 |  |  |  |  |  | $good = 0; | 
| 55 | 0 |  |  |  |  |  | last; | 
| 56 |  |  |  |  |  |  | } | 
| 57 |  |  |  |  |  |  | } | 
| 58 |  |  |  |  |  |  | } | 
| 59 |  |  |  |  |  |  | } | 
| 60 | 0 |  |  |  |  |  | return $good; | 
| 61 | 0 |  |  |  |  |  | }; | 
| 62 |  |  |  |  |  |  |  | 
| 63 |  |  |  |  |  |  | # find event | 
| 64 | 0 |  |  |  |  |  | my $log = App::JobLog::Log->new; | 
| 65 | 0 |  |  |  |  |  | my ( $i, $count, $e ) = ( $log->reverse_iterator, 0 ); | 
| 66 | 0 |  |  |  |  |  | while ( $e = $i->() ) { | 
| 67 | 0 |  |  |  |  |  | $count++; | 
| 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__ |