#!/usr/bin/perl # OddJob: a simple tool for tracking time # Copyright (C) 2011 Ben Charlton # # 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 = (); 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 = (); 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