-
Notifications
You must be signed in to change notification settings - Fork 1
/
ctimer.pl
executable file
·444 lines (404 loc) · 13.1 KB
/
ctimer.pl
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
#!/usr/bin/perl
use strict;
use warnings;
use IO::Select;
use POSIX qw(strftime);
use Getopt::Long;
use DateTime;
use Scalar::Util qw(looks_like_number);
use Switch;
use Time::ParseDate;
#===============================================================================
# Support Functions
#===============================================================================
sub usage(){
print "\n";
print "Usage: ./ctimer.pl [options] minutes\n\n";
print "\t-f pomodoro_count_file (default: running_pomo_count.txt)\n";
print "\t-b specify that this timer is a break\n";
print "\t-r reset pomo counter.\n";
print "\n";
exit;
}
sub mail_notification(){
my $now_string = shift(@_);
print "Mailing notification of completion\n\n";
my %mail = ( To => '[email protected]',
From => '[email protected]',
Message => "Pomodoro Completed!",
Subject => "Pomodoro Completed at $now_string"
);
#sendmail(%mail) or die $Mail::Sendmail::error;
#print "OK. Log says:\n", $Mail::Sendmail::log;
}
sub notify_completion()
{
my $now_string = strftime "%a %b %e %H:%M:%S %Y", localtime;
#&mail_notification($now_string);
#system("DISPLAY=:2.0 ./naughty-notify.sh");
#system("DISPLAY=:1.0 ./naughty-notify.sh");
#foreach my $i (1 .. 190000){print("KABOOM!!!!");}
#system('./asciiquarium');
if(1)
{
if (`uname` ne "Linux"){
system("osascript -e 'display notification \"Pomo Complete!\" with title \"Pomo Complete!\"'")
} else {
#&mail_notification($now_string);
system("DISPLAY=:2.0 ./naughty-notify.sh");
system("DISPLAY=:1.0 ./naughty-notify.sh");
#PostNotification("MyPerlApp", $notificationName, $notificationTitle,
#$notificationDescription[, $sticky, $priority, $image_path]);
#system('say "You are done! Successful completion of Pomodoro!"');
}
}
}
sub printStats()
{
my $dayofweek = DateTime->now()->day_name();
my ($day) = @_;
if($day =~ /s/)
{
system("./pomo_point_count.pl -f ../today.txt -s");
}
elsif($day =~ /t/)
{
system("./pomo_point_count.pl -f ../today.txt -day $dayofweek");
}
elsif($day =~ /i/)
{
system("./pomo_point_count.pl -f ../today.txt -s -i");
}
}
sub printplan()
{
my $dayofweek = DateTime->now()->day_name();
my ($day) = @_;
if($day =~ /s/)
{
system("./pomo_point_count.pl -f ../today.txt -s");
}
else
{
system("./pomo_point_count.pl -f ../today.txt -day $dayofweek");
}
}
# Subtract from the running count of pomodoros
sub delete_pomodoros()
{
my ($running_fn, $pomos) = @_;
my $count = 0;
my $now = strftime "%a %b %e %H:%M:%S %Y", localtime;
my $trash = 0;
if (-e $running_fn) {
open(my $FH, '<', $running_fn);
($count,$trash) = split('\n',<$FH>);
#while(<$FH>){ ($count,$trash) = split('\n',<FH>); }
#close(FH);
}
open(FH, ">", "$running_fn")
or die "cannot open > $running_fn: $!";
$count -= $pomos; print FH $count;
print FH "\n--deleted "; print FH $pomos; print FH " pomos @ "; print FH $now;
close(FH);
print "Deleting $pomos from count\n";
print "\n\n**** You have completed $count pomodoros ****\n\n";
}
# Add to the running count of pomodoros
sub count_pomodoro()
{
my ($running_fn, $pomos) = @_;
my $count = 0;
my $trash = 0;
my $now = strftime "%a %b %e %H:%M:%S %Y", localtime;
if (-e $running_fn) {
open(FH, $running_fn); ($count,$trash) = split('\n',<FH>); close(FH);
}
open(FH, ">", "$running_fn")
or die "cannot open > $running_fn: $!";
$count += $pomos;
print FH $count;
print FH "\n Added "; print FH $count; print FH " pomos @ "; print FH $now;
close(FH);
print "\n\n**** You have completed $count pomodoros ****\n\n";
}
# Take a time range and add pomodoros to the log and start a new one
sub count_pomodoro_from_time()
{
my ($pomo_count_file, $beginTime, $endTime) = @_;
my $t1 = parsedate($beginTime);
my $t2 = parsedate($endTime);
my $tdiff = $t2 - $t1; # time duration in seconds
my $full_pomos = int(($tdiff / 60) / 25);
my $partial_pomos = ($tdiff / 60) % 25;
print "\n\tAdding $full_pomos to the logfile $pomo_count_file.\n";
print "\n\t$partial_pomos minutes extra worked\n";
&count_pomodoro($pomo_count_file, $full_pomos);
return 25 - $partial_pomos;
}
# Execute a timed pomodoro
sub do_pomodoro(){
my ($s, $minutes) = @_;
my $duration = $minutes * 60; # duration in seconds means input in minutes
my $now_string = strftime "%a %b %e %H:%M:%S %Y", localtime;
my $beg_time = time;
my $end_time = $beg_time + $duration;
my $input = '';
print "\nStarting timer for $minutes minutes on $now_string\n\n";
for (;;) {
$|++;
my $time = time;
last if ($time >= $end_time);
printf("\r\t[ %02d:%02d:%02d ] -- command [p,f,v,r,q,s,t,i]? %s",
($end_time - $time) / (60*60),
($end_time - $time) / ( 60) % 60,
($end_time - $time) % 60,
$input
);
if($s->can_read(.1)){
chomp(my $input = <STDIN>);
#print "Got '$foo' from STDIN\n";
#chomp($foo = <STDIN>);
if($input eq 'f')
{
print "\nExiting and counting the pomodoro\n\n";
return 1;
}
if($input eq 'p'){
$now_string = strftime "%H:%M:%S", localtime;
print "\tPaused: $now_string ... Press enter to continue.";
my $begin_pause = time;
chomp($input = <STDIN>);
$now_string = strftime "%H:%M:%S", localtime;
print "\tResuming... $now_string\n";
$end_time += time - $begin_pause;
}
if($input eq 'r')
{
print "Would you really like to restart the pomodoro? [y/N]: ";
chomp(my $input = <STDIN>);
if($input eq 'y' || $input eq 'Y')
{
my $now_string = strftime "%H:%M:%S", localtime;
print "Restarting Pomodoro for $minutes minutes at $now_string\n";
my $begin_pause = time;
$end_time = time + $duration;
}
}
if($input eq 'v'){
print "\nExiting without counting the pomodoro\n\n";
return 0;
}
if($input eq 's'){
&printStats('s');
print "\n";
}
if($input eq 't'){
&printStats('t');
print "\n";
}
if($input eq 'i'){
&printStats('i');
print "\n";
}
if($input eq 'q'){
print "\nExiting without counting the pomodoro\n\n";
exit;
}
}
}
return 1;
}
sub ask_for_action_and_get_choice()
{
print "\nAction (h for help): ";
chomp(my $action = <STDIN>);
while($action !~ /^[abcdhlmnrstiq]$/)
{
print "\nThe option $action does not exist. Please try again" .
"[a,b,c,d,i,l,m,n,q,r,s,t]: ";
chomp($action = <STDIN>);
}
return $action;
}
#===============================================================================
# Main script
#===============================================================================
#### OPTIONS ####
my $pomo_count_file = 'running_pomo_count.txt';
my $help = '';
my $minutes = 0;
my $break = 0;
my $reset = 0;
my $test = 0;
my $end_time = 0;
GetOptions (
'help' => \$help,
'file=s' => \$pomo_count_file,
'break' => \$break,
'test' => \$test,
'reset' => \$reset
);
&usage if($help);
#&usage if($help || !$ARGV[0]);
#$minutes = $ARGV[0];
#-----------------------------------------------------------------------------
# If the value is not a number die.
#-----------------------------------------------------------------------------
die "Value is not a number!\n" if ( !(&looks_like_number($minutes)) );
if($reset) {
print "Would you really like to reset the pomodoros? [y/N] ";
if(getc(STDIN) eq 'y') {
system("rm -rf $pomo_count_file");
}
}
my $s = IO::Select->new();
$s->add(\*STDIN);
while (1)
{
my $action = &ask_for_action_and_get_choice();
$break = 0;
switch($action)
{
case 'a'
{
print "\n\tMinutes to add: ";
chomp(my $tdiff_mins = <STDIN>);
my $full_pomos = int($tdiff_mins / 25);
my $partial_pomos = $tdiff_mins % 25;
&count_pomodoro($pomo_count_file, $full_pomos);
$minutes = 25 - $partial_pomos;
print "\n<<< You have $partial_pomos minutes extra worked\n";
}
case 'b'
{
$minutes = 5;
$break = 1;
}
case 'l'
{
my $count; my $trash;
if (-e $pomo_count_file) {
open(FH, $pomo_count_file);
($count,$trash) = split('\n',<FH>);
close(FH);
} else {
$count = 0;
}
print "\nYou have completed $count pomodoros\n";
}
case 'c'
{
if($end_time == 0)
{
print "Cannot continue from nothing... :)\n";
next;
}
my $tdiff = time - $end_time;
my $full_pomos = int(($tdiff / 60) / 25);
my $partial_pomos = ($tdiff / 60) % 25;
&count_pomodoro($pomo_count_file, $full_pomos);
$minutes = 25 - $partial_pomos;
}
case 'd'
{
print "Enter the number pomos to delete: ";
chomp(my $num_pomos = <STDIN>);
if($num_pomos =~ /^\d+$/){
&delete_pomodoros($pomo_count_file,$num_pomos);
}
else {
print "\n\tERROR: $num_pomos is not a positive integer\n";
}
}
case 'h'
{
print "\n\t- [a] add minutes" .
"\n\t- [b] take a break" .
"\n\t- [c] continue from the completion of the last pomodoro" .
"\n\t- [d] delete count pomodoros" .
"\n\t- [i] print stats with project allocation" .
"\n\t- [l] list pomodoro count" .
"\n\t- [m] capture pomodoros from duration and start new" .
"\n\t- [n] start a new pomodoro now".
"\n\t- [r] reset pomodoro count" .
"\n\t- [s] print weekly stats from today.txt" .
"\n\t- [t] print today's stats from today.txt" .
"\n\t- [q] quit? \n";
}
case 'i'
{
&printStats('i');
}
# TODO: Add a continuous mode here to allow for free flow
# case 'f'
# {
# start a timer counting up with the time since start of the
# continuous mode. Have an option to end the mode and ... but what if
# we've gone continous and haven't stopped when we should? How do we
# handle that?
#
# Can have a pause button, so effectively this works as a forward
# counter. It is effectively a stop watch timer approach. The main
# issue as already mentioned is handling that special case... I
# supose we could void it but .... maybe I should think of how I
# would handle it in real life.
# }
case 'n'
{
print "Duration: ";
chomp(my $duration = <STDIN>);
if($duration eq ""){
$minutes = 25;
} else {
$minutes = $duration;
}
}
case 'm'
{
print "\n\tThe start time: ";
chomp(my $beginTime = <STDIN>);
print "\tThe end time: ";
chomp(my $endTime = <STDIN>);
$minutes = &count_pomodoro_from_time($pomo_count_file, $beginTime, $endTime);
}
case 'r'
{
print "Would you really like to reset the pomodoros? [y/N] ";
chomp(my $answer = <STDIN>);
if($answer eq 'y') {
system("rm -rf $pomo_count_file");
}
}
case 's'
{
&printStats('s');
}
case 't'
{
&printStats('t');
}
case 'q'
{
exit();
}
default:
{
die "That option does not exist";
}
}
next if ($action =~ /[rladhsti]/);
my $result = &do_pomodoro($s, $minutes);
$end_time = time;
my $now_string = strftime "%a %b %e %H:%M:%S %Y", localtime;
# The \a is a system bell so it sounds even when I'm logged remotely
print "\nFinished at $now_string.\a\n";
if( $result == 1){
&count_pomodoro($pomo_count_file, 1) if(!$break);
¬ify_completion()
}
else
{
next;
}
}