From 7b0de7036e0d017841b518c0cd7afe356b5ef913 Mon Sep 17 00:00:00 2001 From: Ben Charlton Date: Mon, 18 Jul 2011 23:03:15 +0100 Subject: [PATCH] It helps to add the source, too... --- oj | 102 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100755 oj diff --git a/oj b/oj new file mode 100755 index 0000000..bf537e0 --- /dev/null +++ b/oj @@ -0,0 +1,102 @@ +#!/usr/bin/perl + +# OddJob - a simple tool for tracking time + +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 = (); + 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') { + + my $args = join (' ', @ARGV); + + open F, $data; + my @lines = (); + 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