]>
git.defcon.no Git - trk/blob - trk
3 use Digest
::MD5
qw(md5_hex);
8 my $trk_dir = "$ENV{HOME}/.trk";
21 printf("It seems you require assistance\n");
25 printf("How to start\n") if $code == START
;
26 printf("How to time\n") if $code == TIMEFORMAT
;
34 my $id_length = shift;
35 $id_length = 32 if not defined $id_length;
39 for(my $i=0 ; $i<128;)
41 my $tc = chr(int(rand(127)));
42 if($tc =~ /[a-zA-Z0-9]/)
50 while ( length($w) < $id_length )
52 $w .= gen_puuid
( $id_length - length( $w ) );
55 $w = substr( $w, 0, $id_length );
59 # Input to parse_time is:
60 # * date -> date-string in the form YYYY-MM-DD
61 # * time -> time-string in the form HH:MM
62 # Return value is a unix timestamp, as returned by time()
65 my ( $Y, $M, $D ) = split ("-", shift );
66 my ( $h, $m ) = split(":", shift );
67 return timelocal
(0, $m, $h, $D, ($M-1), $Y);
74 return 0 if not $i =~ m/(\d\d\d\d-\d\d-\d\d) (\d\d:\d\d)/;
75 return parse_time
($1, $2);
81 return strftime
("%Y-%m-%d %H:%M", localtime($t));
84 sub parse_arguments
($)
89 my $start_time = time;
92 if (( $#ARGV >= 1) && ( $ARGV[1] eq "at" ))
94 # Start and Activity require a title to be present.
95 # All other (stop, main...) do not ^^.
96 if ( ($step == START
) || ($step == TASK
) )
98 # TODO: Allow no title!
99 # If no title is given, read ID of previously used track in stead :)
100 help
($step) unless $#ARGV > 3;
101 $title = join(" ", @ARGV[4..$#ARGV]);
103 help
(TIMEFORMAT
) unless ( $ARGV[2] =~ m/\d\d\d\d-\d\d-\d\d/ && $ARGV[3] =~ m/\d\d:\d\d/);
105 $start_time = parse_time
( $ARGV[2], $ARGV[3] );
107 elsif ( ($step == START
) || ($step == TASK
) || ($step == EDIT
))
110 $title = join(" ", @ARGV);
113 if ( not defined $title )
119 return ( $start_time, $title );
126 my $wrk_dir = $trk_dir;
127 $wrk_dir = $trk_dir . "/" . $trk_id if $trk_id;
129 return undef if ( ! -f
$wrk_dir . "/last" );
130 open ( CUR
, "<" . $wrk_dir . "/last" ) or die ("Unable to read last track file");
137 sub get_current_id
(;$)
140 my $wrk_dir = $trk_dir;
141 $wrk_dir = $trk_dir . "/" . $trk_id if $trk_id;
143 return undef if ( ! -f
$wrk_dir . "/current" );
144 open ( CUR
, "<" . $wrk_dir . "/current" ) or die ("Unable to read current track file");
151 sub set_current_id
($;$)
155 my $wrk_dir = $trk_dir;
156 $wrk_dir = $trk_dir . "/" . $trk_id if $trk_id;
158 return undef if ( -f
$wrk_dir . "/current" );
159 open ( CUR
, ">" . $wrk_dir . "/current" ) or die ("Unable to write current track file");
160 printf(CUR
"%s\n", $id );
163 open ( LAST
, ">" . $wrk_dir . "/last" ) or die ("Unable to write last track file");
164 printf(LAST
"%s\n", $id );
171 my $wrk_dir = $trk_dir;
172 $wrk_dir = $trk_dir . "/" . $trk_id if $trk_id;
177 foreach my $d ( <$wrk_dir/*> )
180 next if not -f
$d . "/info";
182 my $id = basename
($d);
183 my $title = get_track_name
( $id, $trk_id );
185 $tracks{$id} = $title unless not defined $title;
192 sub get_track_id
($;$)
197 # Get hash of track-id's and -names from get_tracks
198 my $tracks = get_tracks
($trk_id);
200 # Look up name in list
201 foreach my $id ( keys $tracks )
204 return $id if ( $tracks->{$id} eq $title )
207 # If no match, return undef.
211 sub get_track_name
($;$)
215 my $wrk_dir = $trk_dir;
216 $wrk_dir = $trk_dir . "/" . $trk_id if $trk_id;
218 open(PRO
, "<" . $wrk_dir . "/" . $id . "/info" ) or die ("Unable to read track medatata file!");
222 next if not $_ =~ /^title:(.*)/;
229 sub create_track
($;$)
233 my $wrk_dir = $trk_dir;
234 $wrk_dir = $trk_dir . "/" . $trk_id if $trk_id;
241 } while ( -d
$wrk_dir . "/" . $id );
242 mkdir ( $wrk_dir . "/" . $id );
244 open(PRO
, ">" . $wrk_dir . "/" . $id . "/info" ) or die ("Unable to create track medatata file!");
245 printf(PRO
"title:%s\n", $title);
251 sub start_track
($$;$)
253 my $start_time = shift;
257 my $wrk_dir = $trk_dir;
258 $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 ( $wrk_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 my $activity = get_current_id
($current);
382 printf("An active subtask is running: '%s'. Closing it.\n", get_track_name
( $activity, $current ));
383 close_track
($stop_time, $current);
385 close_track
($stop_time);
387 printf("Stopped tracking of '%s' at %s\n\n", $title, scalar localtime $stop_time);
389 elsif ( ( $command eq "activity") || ($command eq "task" ) )
396 my ( $start_time, $title ) = parse_arguments
(START
);
398 my $trk_id = get_current_id
();
401 printf("Starting a task/activity requires an active main track.\n");
407 my $current = get_current_id
( $trk_id );
410 $current = start_track
( $start_time, $title, $trk_id );
414 printf("Something weird happened.\n");
420 printf("A task/activity is being tracked: %s\n", get_track_name
( $current, $trk_id ) );
421 close_track
($start_time, $trk_id);
422 $current = start_track
( $start_time, $title, $trk_id );
425 printf("Started tracking of '%s' at %s\n\n", $title, scalar localtime $start_time);
431 elsif ( $command eq "main" )
440 my $stop_time = parse_arguments
(STOP
);
442 my $trk_id = get_current_id
();
445 printf("Stopping a task/activity requires an active main track.\n");
450 my $current = get_current_id
( $trk_id );
453 printf("No activity/task is currently tracked. To stop, please start first\n");
456 my $title = get_track_name
( $current, $trk_id );
457 close_track
($stop_time, $trk_id);
459 printf("Stopped tracking of '%s' at %s\n\n", $title, scalar localtime $stop_time);
462 elsif ( ( $command eq "projects" ) || ( $command eq "list" ) )
464 # Todo/future extensions:
465 # TODO: Sort list of names alphabetically
466 # TODO: Get total-hours for projects
468 my $tracks = get_tracks
();
469 printf("Currently tracked project names:\n\n");
470 my $current = get_current_id
();
472 foreach my $id ( keys $tracks )
474 printf(" %s %s\n", ($id eq $current ?
">" : " " ),$tracks->{$id} );
478 elsif ( $command eq "edit" )
481 my ( undef, $title ) = parse_arguments
(EDIT
);
482 my $id = get_last_id
();
486 $id = get_track_id
($title);
489 printf("No project by that name. Try 'list'\n");
494 system( "/usr/bin/editor " . $trk_dir . "/" . $id . "/tracking" );