-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoj
executable file
·118 lines (94 loc) · 3.06 KB
/
oj
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
#!/usr/bin/perl
# OddJob: a simple tool for tracking time
# Copyright (C) 2011 Ben Charlton <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; see the file COPYING. If not, write to the Free
# Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301 USA, or see http://www.gnu.org
use strict;
my $command = shift;
my $data = $ENV{'HOME'} . "/.oddjob";
my $ymd = sub{sprintf '%04d-%02d-%02d', $_[5]+1900, $_[4]+1, $_[3]}->(localtime);
my $elapsed_regex = '^\d+[hmsd]$';
my %fields = ('date' => 0, 'time' => 1, 'cat' => 2, 'desc' => 3);
if ($command eq 'add') {
my $elapsed = shift;
my $category = shift;
my $description = join(' ', @ARGV);
if ($elapsed !~ /$elapsed_regex/) {
&usage;
}
open F, ">>$data";
print F "$ymd\t$elapsed\t$category\t$description\n";
close F;
}
elsif ($command eq 'edit') {
my $id = shift;
my $args = join(' ', @ARGV);
if ($id !~ /^\d+$/) {
&usage;
}
open F, $data;
my @lines = (<F>);
close F;
die ("Invalid ID\n") if ($id > $#lines);
chomp($lines[$id]) ;
my @edits = split(/\t+/, $lines[$id]);
my $f = join('|', keys %fields);
if (my ($key, $value) = ($args =~ m/^($f)=(.*)/)) {
if (($1 eq 'date') && ($2 !~ /^\d{4}-\d{2}-\d{2}$/)) {
die "Invalid date format, must be YYYY-MM-DD\n";
}
if (($1 eq 'time') && ($2 !~ /$elapsed_regex/)) {
die "Invalid time format, must match $elapsed_regex\n";
}
$edits[$fields{$key}] = $value;
$lines[$id] = "" . join("\t", @edits) . "\n";
open F, ">$data";
print F join('', @lines);
close F;
} else {
&usage;
}
}
elsif ($command eq 'list' or $command eq 'ls') {
my $args = join (' ', @ARGV);
open F, $data;
my @lines = (<F>);
close F;
if ($args =~ m/\-(\d+)/) {
# head
my $from = $#lines - $1 +1;
$from = 0 if ($from < 0);
for (my $i = $from; $i <= $#lines; $i++) {
print "$i\t" . $lines[$i];
}
} else {
# grep
for (my $i = 0; $i <= $#lines; $i++) {
print "$i\t$lines[$i]" if ($lines[$i] =~ /$args/);
}
}
} else {
&usage;
}
sub usage (){
print "OddJob, a simple tool for tracking time.\n";
print "Usage:\n";
print " oj add <time> <category> <description>\n";
print " oj list|ls [string]\n";
print " oj list|ls [-lines]\n";
print " oj edit <id> (date|time|cat|desc)=<new value>\n";
exit;
}