-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclock
executable file
·359 lines (276 loc) · 9.9 KB
/
clock
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#!/usr/bin/perl
use Getopt::Long;
use IO::All;
use Date::Format;
use Date::Parse;
use TryCatch;
use feature ':5.10';
use Pod::Usage;
# general syntax
# todo.sh clock in <num> -- start tracking time on item <num>
# todo.sh clock out -- stop tracking time (clock out of item <num>)
# todo.sh clock what -- print the current 'active' task, shortcut 'wh'
# todo.sh report [--groupingss=groupingss] [filters...] -- report time with optionally specified groupings
my $verbose = 0;
my @groupings = ( 'indate', 'project' ); # default
my $res = GetOptions(
'verbose|v' => sub { $verbose++ },
'groupings=s' => sub { @groupings = split (/,/, $_[1] ) }
);
say "GROUPINGS ARE ".join(", ",@groupings) if $verbose;
my $tododir = $ENV{'TODO_DIR'} || die "TODOTXT_DIR must be specified on in the environment";
my $cmdname = shift @ARGV || die 'Invalid program state: cmdname not specified';
# make sure we're called correctly
my @aliases = ( 'clock', 'cl', 'do' );
(grep { $cmdname eq $_ } @aliases) || die "Invalid program state: command:'$cmdname' is not one of (".join(", ", @aliases).")";
# get all the clock data
my $clockf = io("$tododir/clock.dat");
my @clock = slurp_data(io($clockf));
my $active = get_active_task(@clock);
# if the alias is do, we want to override so we can clock out and then pass it on
if ( $cmdname eq 'do' ) {
# allow us to do multiple tasks
foreach my $dotask ( @ARGV ) {
# find matching task in @todo
my $doline = get_todo_line( $dotask );
if ( ( defined( $doline ) && defined( $active ) )
&& $active->{detail} eq $doline->{detail} ) {
clockout( $active );
}
}
# call the original
my $cmd = "$ENV{'TODO_SH'} command do ".join(' ',map { "'$_'" } @ARGV);
say $cmd if $verbose;
system( $cmd );
exit( 0 );
}
my $cmd = shift @ARGV || pod2usage(1);
if ( $cmd eq 'in' ) {
# todo.sh clock in <linenum>
my $linenum = shift @ARGV || pod2usage(1);
my $line;
try {
$line = get_todo_line($linenum);
if ( not $active ) {
# if we're not active, simply append a new entry
clockin( $line );
} else {
# we're active
if ( $active->{todotxtline} eq $line->{todotxtline} ) {
# already doing requested
warn "You are already clocked in to $line->{todotxtline}";
} else {
# clock out of active and clock in to $line
clockout( $active );
clockin( $line );
}
}
} catch ( $err ) {
say "Caught exception punching in line $linenum:\n\t$err";
exit(1);
}
} elsif ( $cmd eq 'out' ) {
# todo.sh clock out
# see if there's anything amiss
if ( @ARGV ) {
say "Gibberish at the end of the command: '".join(' ',@ARGV)."'";
say "Stopping processing...";
pod2usage(1);
}
try {
clockout( $active );
} catch ( $err ) {
say "Caught exception punching out:\n\t$err";
exit(1);
}
} elsif ( $cmd =~ /(what|wh)/ ) {
if ( $active ) {
say "The active task for ".spent_time($active)." minutes is:\n\t $active->{todotxtline}";
} else {
say "No current task is active";
}
} elsif ( $cmd =~ /(report|rep)/ ) {
# generate a report, optionally using groupings
# apply the cany filters from the command line
my @filters = @ARGV;
my @subclock =
grep { my $d = $_->{todotxtline};
if ( not @filters ) { 1; }
else {
grep { $d =~ /$_/i } @filters;
}
} @clock;
# loop over @clock, grouping first by date, then by activity
my $groups = {};
foreach my $c ( @clock ) {
my $targ = $groups;
foreach my $grp ( @groupings ) {
if ( $grp eq 'indate' ) {
my ($date) = $c->{clockin} =~ /^(\d{8})T\d{6}$/;
die "Couldn't identify clock-in date for '$c->{todotxtline}'" if not $date;
$targ->{$date} = {} if ( not defined $targ->{$date} );
$targ = $targ->{$date};
} elsif ( $grp eq 'project' ) {
my ($prj) = grep { $_ =~ /(\+p\/[^\s]+)/i } @{$c->{proj}};
$prj = '<unknown>' if ( not defined $prj );
$prj = lc($prj);
$targ->{$prj} = {} if ( not defined $targ->{$prj} );
$targ = $targ->{$prj};
}
}
push @{$targ->{$c->{todotxtline}}}, $c;
}
say_group_sums( $groups );
1;
}
sub get_active_task(@) {
# return undef if not @clock; # nothing in the clock file
my @clock = @_;
return undef if not @clock;
return $clock[-1] if not defined( $clock[-1]->{clockout} );
return undef; # not doing anything
}
sub slurp_data {
my ( $file, @filters ) = @_;
die "You must pass an io::file to slurp" if not defined $file;
# read all lines, removing blanks, and return as a list
my $cnt = 0;
my @data =
map {
my $line = $_;
say "PARSING TASK LINE: $line" if $verbose>1;
parse_task( $line, ++$cnt );
}
grep { my $d = $_;
if ( not @filters ) { 1; }
else {
grep { $d =~ /$_/i } @filters;
}
} grep { not /^\s*$/ }
$file->chomp->slurp;
return @data;
}
sub parse_task() {
my ( $task, $id ) = @_;
die "Must specify task (string) to parse" if not defined( $task );
my ( $todotxtline,
$oDone, $oDonetag, $done, $oEnd, $end,
$oPri, $pri,
$oStart, $start,
$detail,
$oClockin, $clockin,
$oClockout, $clockout
) =
$task =~ m/^
(
(((x)\s)\s*((\d{4}-\d{2}-\d{2})\s)?)? # opt done, with or without date
(\(([A-Z])\)\s)? # opt priority
((\d{4}-\d{2}-\d{2})\s)? # opt start date
(.*?) # detail
)
(\t(\d{4}\d{2}\d{2}T\d{2}\d{2}\d{2}))? # opt clock in
(\s(\d{4}\d{2}\d{2}T\d{2}\d{2}\d{2}))? # opt clock out
$/x;
my $tasko = {
todotxtline => $todotxtline,
line => $task,
detail => $detail };
$tasko->{id} = $id if $id;
$tasko->{done} = $done if $done;
$tasko->{start} = $start if $start;
$tasko->{end} = $end if $end;
$tasko->{pri} = $pri if $pri;
$tasko->{clockin} = $clockin if $clockin;
$tasko->{clockout} = $clockout if $clockout;
push @{$tasko->{proj}}, $detail =~ /(\+[^\s]+)/g;
push @{$tasko->{context}}, $detail =~ /(\@[^\s]+)/g;
return $tasko;
}
my $todo = undef;
sub get_todo_line {
my ( $linenum ) = @_;
# verify conditions
die "No line number passed to get_todo_line"
if ( not defined $linenum );
if ( not defined $todo ) {
my @t = slurp_data(io("$tododir/todo.txt"));
$todo = \@t;
}
my $lineidx = $linenum - 1;
die "Specified line number $linenum is out of range [1..".scalar(@{$todo})."]"
if ( $lineidx < 0 || $lineidx >= scalar( @{$todo} ) );
return $todo->[$lineidx];
}
sub clockout($) {
my ( $activeline ) = shift;
die "Not currently clocked into any task" if not $activeline;
say "Clocking out of '$active->{todotxtline}'";
$clockf << "\t".time2str("%Y%m%dT%H%M%S", time);
}
sub clockin {
my ( $line ) = shift;
say "Clocking into '$line->{todotxtline}'";
"\n$line->{line}\t".time2str("%Y%m%dT%H%M%S", time) >> $clockf;
}
sub spent_time( $ ) {
my ( $task ) = shift;
die "Can't compute spent time without a clock in" if not $task->{clockin};
my $clockint = str2time( $task->{clockin} );
my $clockoutt = str2time( $task->{clockout} || time2str( "%c", time ) );
return int(($clockoutt - $clockint)/60); # minutes
}
sub say_group_sums {
my ( $o, $level ) = ( shift, shift || 0 );
#my $thisindent = $indent . ' ';
my $indent;
foreach my $l ( 1..$level ) { $indent .= ' ' };
my $thisindent = $indent . ' ';
die "Groups to summarize not stored as hash" if ref $o ne 'HASH';
my $sumtotal = 0;
my $total = 0;
foreach my $gk ( sort { $a <=> $b } keys %{$o} ) {
my $oo = $o->{$gk};
# print STDERR "PROCESSING KEY $gk\n";
if ( ref $oo eq 'ARRAY' ) {
# A list of matching tasks for this group...
$total = 0;
foreach my $t ( @{$oo} ) {
$total += spent_time( $t );
#say sprintf "$thisindent%5d: $t->{todotxtline}", $total;
}
say sprintf "$thisindent%3d:%2.2d - $gk", $total/60,$total%60;
$sumtotal += $total;
} elsif ( ref $oo eq 'HASH' ) {
# we have a subgrouping
say "\n$indent$gk:";
my $bigtotal = say_group_sums($oo, $level + 1 );
say sprintf "$thisindent -----";
say sprintf "$thisindent %3d:%2.2d - total $gk", $bigtotal/60,$bigtotal%60;
}
}
return $sumtotal;
}
1;
=head1 NAME
todo.sh clock - Using the clock add-on for todo.sh
=head1 SYNOPSIS
todo.sh clock [command [command options] [command arguments]]
Global Options:
-v verbose
Commands:
in <linenum> Clock in to the task associated with
<linenum>. Clocking out of the active
task if necessary.
out Clock out of the current task (if any)
what|wh Show what the current task is (if any)
report|rep [--groupings=[indate],[project]]
Report time recorded for each task in the clock.dat file
=head1 GLOBAL OPTIONS
=over 8
=item B<-v>
Show what is being done.
=back
=head1 DESCRIPTION
The B<clock> add-on for B<todo.sh> will keep track of the time spent in
performing each task.
=cut