| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package App::JobLog::Command::add; | 
| 2 |  |  |  |  |  |  | $App::JobLog::Command::add::VERSION = '1.039'; | 
| 3 |  |  |  |  |  |  | # ABSTRACT: log an event | 
| 4 |  |  |  |  |  |  |  | 
| 5 | 2 |  |  | 2 |  | 20708 | use App::JobLog -command; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 22 |  | 
| 6 | 2 |  |  | 2 |  | 903 | use Modern::Perl; | 
|  | 2 |  |  |  |  | 5 |  | 
|  | 2 |  |  |  |  | 15 |  | 
| 7 | 2 |  |  | 2 |  | 323 | use autouse 'Getopt::Long::Descriptive' => qw(prog_name); | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 14 |  | 
| 8 | 2 |  |  | 2 |  | 125 | use autouse 'App::JobLog::Time'         => qw(now); | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 10 |  | 
| 9 | 2 |  |  | 2 |  | 329 | use Class::Autouse qw(App::JobLog::Log); | 
|  | 2 |  |  |  |  | 5 |  | 
|  | 2 |  |  |  |  | 17 |  | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | sub execute { | 
| 12 | 0 |  |  | 0 | 1 |  | my ( $self, $opt, $args ) = @_; | 
| 13 | 0 |  |  |  |  |  | my $tags = $opt->tag; | 
| 14 | 0 | 0 |  |  |  |  | unless ($tags) { | 
| 15 | 0 | 0 |  |  |  |  | $tags = [] if $opt->clear_tags; | 
| 16 |  |  |  |  |  |  | } | 
| 17 | 0 |  |  |  |  |  | my $log        = App::JobLog::Log->new; | 
| 18 | 0 |  |  |  |  |  | my ($last)     = $log->last_event; | 
| 19 | 0 |  | 0 |  |  |  | my $is_ongoing = $last && $last->is_open; | 
| 20 | 0 | 0 |  |  |  |  | $log->append_event( | 
| 21 |  |  |  |  |  |  | $tags ? ( tags => $tags ) : (), | 
| 22 |  |  |  |  |  |  | description => [ join ' ', @$args ], | 
| 23 |  |  |  |  |  |  | time        => now | 
| 24 |  |  |  |  |  |  | ); | 
| 25 | 0 | 0 | 0 |  |  |  | if ( $is_ongoing && _different_day( $last->start, now ) ) { | 
| 26 | 0 |  |  |  |  |  | say 'Event spans midnight. Perhaps you failed to close the last event.'; | 
| 27 |  |  |  |  |  |  | } | 
| 28 |  |  |  |  |  |  | } | 
| 29 |  |  |  |  |  |  |  | 
| 30 |  |  |  |  |  |  | sub _different_day { | 
| 31 | 0 |  |  | 0 |  |  | my ( $d1, $d2 ) = @_; | 
| 32 | 0 |  | 0 |  |  |  | return !( $d1->year == $d2->year | 
| 33 |  |  |  |  |  |  | && $d1->month == $d2->month | 
| 34 |  |  |  |  |  |  | && $d1->day == $d2->day ); | 
| 35 |  |  |  |  |  |  | } | 
| 36 |  |  |  |  |  |  |  | 
| 37 | 0 |  |  | 0 | 1 |  | sub usage_desc { '%c ' . __PACKAGE__->name . ' ' } | 
| 38 |  |  |  |  |  |  |  | 
| 39 | 0 |  |  | 0 | 1 |  | sub abstract { 'log an event' } | 
| 40 |  |  |  |  |  |  |  | 
| 41 |  |  |  |  |  |  | sub full_description { | 
| 42 | 0 |  |  | 0 | 0 |  | < | 
| 43 |  |  |  |  |  |  | Log an event. E.g., | 
| 44 |  |  |  |  |  |  |  | 
| 45 | 0 |  |  |  |  |  | @{[prog_name($0)]} @{[__PACKAGE__->name]} munging the widget | 
|  | 0 |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  |  | 
| 47 |  |  |  |  |  |  | All arguments that are not parameter values are concatenated as a description | 
| 48 |  |  |  |  |  |  | of the event. Logging an event simultaneously marks the end of the previous | 
| 49 |  |  |  |  |  |  | event. Events may be tagged to mark such things as client, grant, or | 
| 50 |  |  |  |  |  |  | project. | 
| 51 |  |  |  |  |  |  | END | 
| 52 |  |  |  |  |  |  | } | 
| 53 |  |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  | sub options { | 
| 55 |  |  |  |  |  |  | return ( | 
| 56 |  |  |  |  |  |  | [ | 
| 57 | 0 |  |  | 0 | 0 |  | 'tag|t=s@', | 
| 58 |  |  |  |  |  |  | 'tag the event; multiple tags are acceptable; e.g., -t foo -t bar -t quux', | 
| 59 |  |  |  |  |  |  | ], | 
| 60 |  |  |  |  |  |  | [ | 
| 61 |  |  |  |  |  |  | 'clear-tags|T', | 
| 62 |  |  |  |  |  |  | 'inherit no tags from preceding event; ' | 
| 63 |  |  |  |  |  |  | . 'this is equivalent to -t ""; ' | 
| 64 |  |  |  |  |  |  | . 'this option has no effect if any tag is specified', | 
| 65 |  |  |  |  |  |  | ], | 
| 66 |  |  |  |  |  |  |  | 
| 67 |  |  |  |  |  |  | ); | 
| 68 |  |  |  |  |  |  | } | 
| 69 |  |  |  |  |  |  |  | 
| 70 |  |  |  |  |  |  | sub validate { | 
| 71 | 0 |  |  | 0 | 0 |  | my ( $self, $opt, $args ) = @_; | 
| 72 |  |  |  |  |  |  |  | 
| 73 | 0 | 0 |  |  |  |  | $self->usage_error('no description provided') unless @$args; | 
| 74 |  |  |  |  |  |  | } | 
| 75 |  |  |  |  |  |  |  | 
| 76 |  |  |  |  |  |  | 1; | 
| 77 |  |  |  |  |  |  |  | 
| 78 |  |  |  |  |  |  | __END__ |