JobLog/Command/info.pm untested

filter: Statementstmt 0.0% 0 / 89 Branchbran 0.0% 0 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 129 CC 3 SCAR 9.9
CC
3
Cov
0%
execute
6.0
_unambiguous_prefixes
6.0
usage_desc
2.0
CRAP 2.7
1 package App::JobLog::Command::info;
2 $App::JobLog::Command::info::VERSION = '1.042';
3 # ABSTRACT: provides general App::JobLog information
4  
5 use App::JobLog -command;
6 use autouse 'File::Temp' => qw(tempfile);
7 use autouse 'Pod::Usage' => qw(pod2usage);
8 use autouse 'Carp' => qw(carp);
9 use autouse 'App::JobLog::Config' => qw(log DIRECTORY);
10 use Class::Autouse qw(Config File::Spec);
11  
12 use Modern::Perl;
13 no if $] >= 5.018, warnings => "experimental::smartmatch";
14  
15 # using quasi-pod -- == instead of = -- to make this work with Pod::Weaver
16  
17 sub execute {
18 my ( $self, $opt, $args ) = @_;
19 $self->simple_command_check($args);
20 my ( $fh, $fn ) = tempfile( UNLINK => 1 );
21 my ($executable) = reverse File::Spec->splitpath($0);
22 my $text;
23 my @options = ( -verbose => 2, -exitval => 0, -input => $fn );
24 for ( $opt->verbosity ) {
25 when ('man') {
26 $text =
27 $self->_header($executable)
28 . $self->_body($executable)
29 . $self->_footer($executable);
30 my $perldoc =
31 File::Spec->catfile( $Config::Config{scriptdir}, 'perldoc' );
32 unless ( -e $perldoc ) {
33 carp 'Cannot find perldoc. Text will not be paged.';
34 push @options, -noperldoc => 1;
35 }
36 }
37 when ('verbose') {
38 $text =
39 $self->_header($executable)
40 . $self->_basic_usage($executable)
41 . $self->_footer($executable);
42 push @options, -noperldoc => 1;
43 }
44 default {
45 $text =
46 $self->_header($executable) . <<END . $self->_footer($executable);
47  
48 ==head1 For More Information
49  
50 $executable info --help
51 END
52 push @options, -noperldoc => 1;
53 }
54 }
55  
56 $text = <<END;
57 $text
58 ==cut
59 END
60 $text =~ s/^==(\w)/=$1/gm;
61 print $fh $text;
62 $fh->close;
63 pod2usage(@options);
64 }
65  
66 sub usage_desc { '%c ' . __PACKAGE__->name }
67  
68 sub abstract { 'describe job log' }
69  
70 sub full_description {
71 <<END
72 Describes application and provides usage information.
73 END
74 }
75  
76 sub options {
77 return (
78 [
79 "verbosity" => hidden => {
80 one_of => [
81 [ 'verbose|v' => 'longer documentation' ],
82 [ 'man' => 'extensive documentation in pager' ],
83 ],
84 }
85 ]
86 );
87 }
88  
89 # obtain all the
90 sub _unambiguous_prefixes {
91 my ( $self, $command ) = @_;
92  
93 # borrowing this from App::Cmd::Command::commands
94 my @commands =
95 map { ( $_->command_names )[0] } $self->app->command_plugins;
96 my %counts;
97 for my $cmd (@commands) {
98 for my $prefix ( _prefixes($cmd) ) {
99 $counts{$prefix}++;
100 }
101 }
102 my @prefixes;
103 for my $prefix ( _prefixes($command) ) {
104 push @prefixes, $prefix if $counts{$prefix} == 1;
105 }
106 return @prefixes;
107 }
108  
109 # obtain all the prefixes of a word
110 sub _prefixes {
111 my $cmd = shift;
112 my @prefixes;
113 for ( my ( $i, $lim ) = ( 0, length $cmd ) ; $i < $lim ; ++$i ) {
114 push @prefixes, substr $cmd, 0, $lim - $i;
115 }
116 return @prefixes;
117 }
118  
119 sub _header {
120 my ( $self, $executable ) = (@_);
121 return <<END;
122 ==head1 Job Log
123  
124 work log management
125  
126 version @${[ $App::JobLog::Command::info::VERSION // 'DEVELOPMENT' ]}
127  
128 This application allows one to keep a simple, human readable log
129 of one's activities. B<Job Log> also facilitates searching, summarizing,
130 and extracting information from this log as needed.
131 END
132 }
133  
134 sub _body {
135 my ( $self, $executable ) = (@_);
136 return $self->_basic_usage($executable) . $self->_advanced_usage();
137 }
138  
139 sub _basic_usage {
140 my ( $self, $executable ) = (@_);
141 return <<END;
142  
143 ==head1 Usage
144  
145 B<Job Log> keeps a log of events and notes. If you begin a new task you type
146  
147 $executable @{[App::JobLog::Command::add->name]} what I am doing now
148  
149 and it appends the following, modulo changes in time, to @{[log]}:
150  
151 2011 2 1 15 19 12::what I am doing now
152  
153 The portion before the first colon is a timestamp in year month day hour minute second format.
154 The portion after the second colon is your description of the event.
155  
156 If you wish to take a note, you type
157  
158 $executable @{[App::JobLog::Command::note->name]} something I should remember
159  
160 and it appends the following to @{[log]}:
161  
162 2011 2 1 15 19 12<NOTE>:something I should remember
163  
164 Again, the portion before the first colon is a timestamp. The portion after the E<lt>NOTEE<gt> is
165 the body of the note.
166  
167 The text between the two colons, or between the first colon and the E<lt>NOTEE<gt> tag, which is blank in these
168 examples, is a list of space-delimited tags one can use to categorize things. For
169 instance, if you were performing this task for Acme Widgets you might have typed
170  
171 $executable @{[App::JobLog::Command::add->name]} -t "Acme Widgets" what I am doing now
172  
173 producing
174  
175 2011 2 1 15 19 12:Acme\\ Widgets:what I am doing now
176  
177 Note the I<\\> character. This is the escape character which neutralizes any special value of
178 the character after it -- I<\\>, I<:>, or a whitespace character.
179  
180 You may tag an event multiple times. E.g.,
181  
182 $executable @{[App::JobLog::Command::add->name]} -t "Acme Widgets" -t foo -t bar what I am doing now
183  
184 producing
185  
186 2011 2 1 15 19 12:Acme\\ Widgets foo bar:what I am doing now
187  
188 For readability it is probably best to avoid spaces in tags.
189  
190 Since one usually works on a particular project for an extended period of time, if you specify no tags
191 the event or note is given the same tags as the preceding event/note. For example,
192  
193 $executable @{[App::JobLog::Command::add->name]} -t foo what I am doing now
194 $executable @{[App::JobLog::Command::add->name]} now something else
195  
196 would produce something like
197  
198 2011 2 1 15 19 12:foo:what I am doing now
199 2011 2 1 16 19 12:foo:now something else
200  
201 When you are done with the last task of the day, or your stop to take a break, you type
202  
203 $executable @{[App::JobLog::Command::done->name]}
204  
205 which adds something like
206  
207 2011 2 1 16 19 12:DONE
208  
209 to the log. Note the single colon. In this case I<DONE> is not a tag. Tags are always sandwiched between
210 two delimiters. I<DONE> here just marks the line as the end of a task.
211  
212 When you come back to work you can type
213  
214 $executable @{[App::JobLog::Command::resume->name]}
215  
216 to add a new line to the log with the same description and tags as the last task you began.
217  
218 ==head2 Summary Commands
219  
220 The log is of little use if you cannot extract useful reports of what it contains. For this there are a
221 variety of commands.
222  
223 ==over 8
224  
225 ==item B<@{[App::JobLog::Command::summary->name]}>
226  
227 The most extensive and featureful log report command. Example:
228  
229 \$ job summary yesterday
230 Monday, 14 March
231 9:46 - 10:11 am 0.41 widgets modifying name normalization code to use dates
232 10:17 - 10:55 am 0.62 widgets modifying name normalization code to use dates
233 1:49 - 2:08 pm 0.32 widgets testing PGA file to see whether Felix Frankfurter is still there
234  
235 TOTAL HOURS 1.35
236 widgets 1.35
237  
238 ==item B<@{[App::JobLog::Command::last->name]}>
239  
240 The last event recorded. Example:
241  
242 \$ job last
243 Tuesday, 15 March
244 5:07 pm - ongoing 0.00 foo muttering
245  
246 TOTAL HOURS 0.00
247 foo 0.00
248  
249 ==item B<@{[App::JobLog::Command::today->name]}>
250  
251 Everything you've done today. Example:
252  
253 \$ job today
254 Tuesday, 15 March
255 11:33 - 11:35 am 0.04 widgets checking up on Lem's issue with pipeline
256 11:38 - 11:46 am 0.12 widgets checking up on Lem's issue with pipeline; figuring out null pointer in multi-threaded code
257 12:40 - 1:11 pm 0.52 widgets debugging null pointers
258  
259 TOTAL HOURS 0.68
260 widgets 0.68
261  
262 ==back
263  
264 ==head2 Obtaining Further Information
265  
266 If you wish further information there are severals routes:
267  
268 ==over 8
269  
270 ==item B<$executable>
271  
272 If you invoke B<Job Log> without any arguments you will receive a list of its commands.
273  
274 ==item B<$executable commands>
275  
276 Another way to obtain a list of commands.
277  
278 ==item B<--help>
279  
280 Every command has a C<--help> option which will provide minimal help text and a complete list of the options the command
281 understands.
282  
283 ==item B<$executable help <command>>
284  
285 The C<help> command will provide a command's full usage text.
286  
287 ==item B<$executable @{[__PACKAGE__->name]} --man>
288  
289 This command's C<--man> option provides still more extensive help text.
290  
291 ==item B<perldoc>
292  
293 The Perl modules of which this application is composed each have their own documentation. For example, try
294  
295 perldoc App::JobLog
296  
297 ==back
298  
299 B<TIP:> any unambigous prefix of a command will do. All the following are equivalent:
300  
301 @{[join "\n", map {" $executable $_ doing something"} $self->_unambiguous_prefixes(App::JobLog::Command::add->name)]}
302  
303 This means that for almost all commands you need only use the first letter of the command name.
304 END
305 }
306  
307 sub _advanced_usage {
308 my ( $self, $executable ) = (@_);
309 return <<END;
310  
311 ==head1 Environment Variables
312  
313 B<Job Log> is sensitive to a single environment variable:
314  
315 ==head2 @{[DIRECTORY()]}
316  
317 By default B<Job Log> keeps the log and all other files in a hidden directory called F<.joblog> in your home
318 directory. If @{[DIRECTORY()]} is set, however, it will keep its files here. This is mostly useful for
319 testing, though if you find F<.joblog> already is in use by some other application you can use this variable
320 to prevent collisions. Collisions will only occur if the files F<log> or F<config.ini> exist in this
321 directory, and B<Job Log> will only alter these files if you append an event to the log or modify some
322 configuration parameters.
323  
324 All other configuration is done through the B<@{[App::JobLog::Command::configure->name]}> command.
325  
326 ==head1 Time Expressions
327  
328 B<Job Log> goes to considerable trouble to interpret whatever time expressions you might throw at it.
329 For example, it understands all of the following:
330  
331 1
332 11/24 to today
333 17 dec, 2024
334 1 april, 2022 to 1-23-2002
335 2023.6.5 - 10.26.2020
336 2-22 till yesterday
337 24 apr
338 27 november, 1995 through 10
339 3-4-2004
340 3-9 - today
341 4.23- 16 november, 1992
342 8/1/1997 through yesterday
343 june 14
344 last month - 6.14
345 pay period
346 2010
347 June 2010
348 2010/6
349 Feb 1 - 14
350 ever
351  
352 Every expression represents an interval of time. It either names an interval or defines it as the span from
353 the beginning of one interval to the end of another.
354  
355 ==head2 Time Grammar
356  
357 Here is a complete BNF-style grammar of the time expressions understood by B<Job Log>. In this set of rules
358 C<s> represents some amount of whitespace, C<d> represents a digit, and C<\\x>, where C<x> is a number,
359 represents a back reference to the corresponding matched group in the same rule. After the first three
360 rules the remainder are alphabetized to facilitate finding them in the list. All expressions must match the
361 first rule.
362  
363 If you find this system of rules opaque or unwieldy, you can use the B<@{[App::JobLog::Command::parse->name]}>
364 command to test an expression and see what time interval it is interpreted as.
365  
366 @{[_bnf()]}
367 END
368 }
369  
370 sub _footer {
371 my ( $self, $executable ) = (@_);
372 return <<END;
373  
374 ==head1 License etc.
375  
376 Author David Houghton
377 dfhoughton\@gmail.com
378 Copyright (c) 2011
379 License Perl_5
380 END
381 }
382  
383 # the complete bnf diagram for time grammar, also maintained
384 # in App::JobLog::TimeGrammar for lack of introspection in pod
385 sub _bnf {
386 return <<END;
387 <expression> = s* ( <ever> | <span> ) s*
388 <ever> = "all" | "always" | "ever" | [ [ "the" s ] ( "entire" | "whole" ) s ] "log"
389 <span> = <date> [ <span_divider> <date> ]
390  
391 <at> = "at" | "@"
392 <at_time> = [ ( s | s* <at> s* ) <time> ]
393 <at_time_on> = [ <at> s ] <time> s "on" s
394 <beginning> = "beg" [ "in" [ "ning" ] ]
395 <date> = <numeric> | <verbal>
396 <day_first> = d{1,2} s <month>
397 <divider> = "-" | "/" | "."
398 <dm_full> = d{1,2} s <month> [ "," ] s d{4}
399 <dom> = d{1,2}
400 <full> = <at_time_on> <full_no_time> | <full_no_time> <at_time>
401 <full_month> = "january" | "february" | "march" | "april" | "may" | "june" | "july" | "august" | "september" | "october" | "november" | "december"
402 <full_no_time> = <dm_full> | <md_full>
403 <full_weekday> = "sunday" | "monday" | "tuesday" | "wednesday" | "thursday" | "friday" | "saturday"
404 <iso> = d{4} ( <divider> ) d{1,2} \\1 d{1,2}
405 <md> = d{1,2} <divider> d{1,2}
406 <md_full> = <month> s d{1,2} "," s d{4}
407 <modifiable_day> = <at_time_on> <modifiable_day_no_time> | <modifiable_day_no_time> <at_time>
408 <modifiable_day_no_time> = [ <modifier> s ] <weekday>
409 <modifiable_month> = [ <month_modifier> s ] <month>
410 <modifiable_period> = [ <period_modifier> s ] <period>
411 <modifier> = "last" | "this" | "next"
412 <month> = <full_month> | <short_month>
413 <month_day> = <at_time_on> <month_day_no_time> | <month_day_no_time> <at_time>
414 <month_day_no_time> = <month_first> | <day_first>
415 <month_first> = <month> s d{1,2}
416 <month_modifier> = <modifier> | <termini> [ s "of" ]
417 <my> = <month> [","] s <year>
418 <named_period> = <modifiable_day> | <modifiable_month> | <modifiable_period>
419 <now> = "now"
420 <numeric> = <year> | <ym> |<at_time_on> <numeric_no_time> | <numeric_no_time> <at_time>
421 <numeric_no_time> = <us> | <iso> | <md> | <dom>
422 <pay> = "pay" | "pp" | "pay" s* "period"
423 <period> = "week" | "month" | "year" | <pay>
424 <period_modifier> = <modifier> | <termini> [ s "of" [ s "the" ] ]
425 <relative_period> = [ <at> s* ] <time> s <relative_period_no_time> | <relative_period_no_time> <at_time> | <now>
426 <relative_period_no_time> = "yesterday" | "today" | "tomorrow"
427 <short_month> = "jan" | "feb" | "mar" | "apr" | "may" | "jun" | "jul" | "aug" | "sep" | "oct" | "nov" | "dec"
428 <short_weekday> = "sun" | "mon" | "tue" | "wed" | "thu" | "fri" | "sat"
429 <span_divider> = s* ( "-"+ | ( "through" | "thru" | "to" | "til" [ "l" ] | "until" ) ) s*
430 <termini> = [ "the" s ] ( <beginning> | "end" )
431 <time> = d{1,2} [ ":" d{2} [ ":" d{2} ] ] [ s* <time_suffix> ]
432 <time_suffix> = ( "a" | "p" ) ( "m" | ".m." )
433 <us> = d{1,2} ( <divider> ) d{1,2} \\1 d{4}
434 <verbal> = <my> | <named_period> | <relative_period> | <month_day> | <full>
435 <weekday> = <full_weekday> | <short_weekday>
436 <year> = d{4}
437 <ym> = <year> <divider> d{1,2}
438 END
439 }
440  
441 1;
442  
443 __END__