]>
git.defcon.no Git - trk/blob - trk
3 use Digest
::MD5
qw(md5_hex);
8 my $trk_dir = "$ENV{HOME}/.trk";
20 printf("It seems you require assistance\n");
24 printf("How to start\n") if $code == START
;
25 printf("How to time\n") if $code == TIMEFORMAT
;
33 my $id_length = shift;
34 $id_length = 32 if not defined $id_length;
38 for(my $i=0 ; $i<128;)
40 my $tc = chr(int(rand(127)));
41 if($tc =~ /[a-zA-Z0-9]/)
49 while ( length($w) < $id_length )
51 $w .= gen_puuid
( $id_length - length( $w ) );
54 $w = substr( $w, 0, $id_length );
58 # Input to parse_time is:
59 # * date -> date-string in the form YYYY-MM-DD
60 # * time -> time-string in the form HH:MM
61 # Return value is a unix timestamp, as returned by time()
64 my ( $Y, $M, $D ) = split ("-", shift );
65 my ( $h, $m ) = split(":", shift );
66 return timelocal
(0, $m, $h, $D, ($M-1), $Y);
73 return 0 if not $i =~ m/(\d\d\d\d-\d\d-\d\d) (\d\d:\d\d)/;
74 return parse_time
($1, $2);
80 return strftime
("%Y-%m-%d %H:%M", localtime($t));
83 sub parse_arguments
($)
88 my $start_time = time;
91 if (( $#ARGV >= 1) && ( $ARGV[1] eq "at" ))
93 # Start and Activity require a title to be present.
94 # All other (stop, main...) do not ^^.
95 if ( ($step == START
) || ($step == TASK
) )
97 # TODO: Allow no title!
98 # If no title is given, read ID of previously used track in stead :)
99 help
($step) unless $#ARGV > 3;
100 $title = join(" ", @ARGV[4..$#ARGV]);
102 help
(TIMEFORMAT
) unless ( $ARGV[2] =~ m/\d\d\d\d-\d\d-\d\d/ && $ARGV[3] =~ m/\d\d:\d\d/);
104 $start_time = parse_time
( $ARGV[2], $ARGV[3] );
106 elsif ( ($step == START
) || ($step == TASK
) || ($step == EDIT
))
109 $title = join(" ", @ARGV);
112 if ( not defined $title )
118 return ( $start_time, $title );
125 my $wrk_dir = $trk_dir;
126 $wrk_dir = $trk_dir . "/" . $trk_id if $trk_id;
128 return undef if ( ! -f
$wrk_dir . "/last" );
129 open ( CUR
, "<" . $wrk_dir . "/last" ) or die ("Unable to read last track file");
136 sub get_current_id
(;$)
139 my $wrk_dir = $trk_dir;
140 $wrk_dir = $trk_dir . "/" . $trk_id if $trk_id;
142 return undef if ( ! -f
$wrk_dir . "/current" );
143 open ( CUR
, "<" . $wrk_dir . "/current" ) or die ("Unable to read current track file");
150 sub set_current_id
($;$)
154 my $wrk_dir = $trk_dir;
155 $wrk_dir = $trk_dir . "/" . $trk_id if $trk_id;
157 return undef if ( -f
$wrk_dir . "/current" );
158 open ( CUR
, ">" . $wrk_dir . "/current" ) or die ("Unable to write current track file");
159 printf(CUR
"%s\n", $id );
162 open ( LAST
, ">" . $wrk_dir . "/last" ) or die ("Unable to write last track file");
163 printf(LAST
"%s\n", $id );
170 my $wrk_dir = $trk_dir;
171 $wrk_dir = $trk_dir . "/" . $trk_id if $trk_id;
175 foreach my $d ( <$wrk_dir/*> )
178 next if not -f
$d . "/info";
180 my $id = basename
($d);
181 my $title = get_track_name
( $id );
183 $tracks{$id} = $title unless not defined $title;
190 sub get_track_id
($;$)
195 # Get hash of track-id's and -names from get_tracks
196 my $tracks = get_tracks
($trk_id);
198 # Look up name in list
199 foreach my $id ( keys $tracks )
202 return $id if ( $tracks->{$id} eq $title )
205 # If no match, return undef.
209 sub get_track_name
($;$)
213 my $wrk_dir = $trk_dir;
214 $wrk_dir = $trk_dir . "/" . $trk_id if $trk_id;
216 open(PRO
, "<" . $wrk_dir . "/" . $id . "/info" ) or die ("Unable to read track medatata file!");
220 next if not $_ =~ /^title:(.*)/;
227 sub create_track
($;$)
231 my $wrk_dir = $trk_dir;
232 $wrk_dir = $trk_dir . "/" . $trk_id if $trk_id;
239 } while ( -d
$wrk_dir . "/" . $id );
240 mkdir ( $wrk_dir . "/" . $id );
242 open(PRO
, ">" . $wrk_dir . "/" . $id . "/info" ) or die ("Unable to create track medatata file!");
243 printf(PRO
"title:%s\n", $title);
249 sub start_track
($$;$)
251 my $start_time = shift;
255 my $wrk_dir = $trk_dir;
256 $wrk_dir = $trk_dir . "/" . $trk_id if $trk_id;
260 my $current = get_current_id
($trk_id);
265 $current = get_last_id
( $trk_id );
269 $current = get_track_id
( $title, $trk_id );
272 printf("No track by that name! Creating a new one.\n");
273 $current = create_track
($title, $trk_id);
277 # Break off here if we haven't gotten an ID yet.
278 return undef if not $current;
280 set_current_id
($current, $trk_id);
282 # First iteration is VERY naive: simply add the start time to the bottom of the tracking file
283 # Will have to do more logic: if the start point is before one of the times already in the track,
284 # the file will have to be manipulated to get coherent tracking!
285 open (TRACK
, ">>" . $wrk_dir . "/" . $current . "/tracking" ) or die ("Unable to open file, $!");
286 printf(TRACK
"[%s]", time2str
($start_time));
295 sub close_track
($;$)
298 my $stop_time = shift;
300 my $wrk_dir = $trk_dir;
301 $wrk_dir = $trk_dir . "/" . $trk_id if $trk_id;
303 my $current = get_current_id
( $trk_id );
305 die ("Project exists, but tracking file does not!") if ( not -f
$wrk_dir . "/" . $current . "/tracking" );
307 # First iteration is VERY naive: simply add the stop time to the bottom line of the tracking file
308 # Will have to do more logic: if the start point is before one of the times already in the track,
309 # the file will have to be manipulated to get coherent tracking!
310 # In addtion to this: actually do some file sanity checking!
311 open (TRACK
, ">>" . $wrk_dir . "/" . $current . "/tracking" ) or die ("Unable to open file, $!");
312 printf(TRACK
" to [%s]\n", time2str
($stop_time));
315 unlink ( $trk_dir . "/current" );
318 ############################################################
322 mkdir $trk_dir or die("Unable to create data directory");
330 my $command = $ARGV[0];
332 if ( ( $command eq "start") || ($command eq "on" ) )
339 my ( $start_time, $title ) = parse_arguments
(START
);
341 my $current = get_current_id
();
344 $current = start_track
( $start_time, $title );
348 printf("Something weird happened.\n");
354 printf("A project is being tracked: %s\n", get_track_name
( $current ) );
355 close_track
($start_time);
356 $current = start_track
( $start_time, $title );
359 printf("Started tracking of '%s' at %s\n\n", $title, scalar localtime $start_time);
361 elsif ( ( $command eq "stop") || ($command eq "off" ) )
369 my $stop_time = parse_arguments
(STOP
);
371 my $current = get_current_id
();
374 printf("No project is currently tracked. To stop, please start first\n");
377 my $title = get_track_name
( $current );
379 close_track
($stop_time);
381 printf("Stopped tracking of '%s' at %s\n\n", $title, scalar localtime $stop_time);
383 elsif ( ( $command eq "projects" ) || ( $command eq "list" ) )
385 # Todo/future extensions:
386 # TODO: Sort list of names alphabetically
387 # TODO: Get total-hours for projects
389 my $tracks = get_tracks
();
390 printf("Currently tracked project names:\n\n");
391 my $current = get_current_id
();
393 foreach my $id ( keys $tracks )
395 printf(" %s %s\n", ($id eq $current ?
">" : " " ),$tracks->{$id} );
399 elsif ( $command eq "edit" )
402 my ( undef, $title ) = parse_arguments
(EDIT
);
403 my $id = get_last_id
();
407 $id = get_track_id
($title);
410 printf("No project by that name. Try 'list'\n");
415 system( "/usr/bin/editor " . $trk_dir . "/" . $id . "/tracking" );