]>
git.defcon.no Git - trk/blob - trk
f52f49af68b73b821f19f1fa117ebfe2b128cdcc
3 use Digest
::MD5
qw(md5_hex);
7 my $trk_dir = "$ENV{HOME}/.trk";
18 printf("It seems you require assistance\n");
22 printf("How to start\n") if $code == START
;
23 printf("How to time\n") if $code == TIMEFORMAT
;
28 # Input to parse_time is:
29 # * date -> date-string in the form YYYY-MM-DD
30 # * time -> time-string in the form HH:MM
31 # Return value is a unix timestamp, as returned by time()
34 my ( $Y, $M, $D ) = split ("-", shift );
35 my ( $h, $m ) = split(":", shift );
36 return timelocal
(0, $m, $h, $D, ($M-1), $Y);
41 my $id_length = shift;
42 $id_length = 32 if not defined $id_length;
46 for(my $i=0 ; $i<128;)
48 my $tc = chr(int(rand(127)));
49 if($tc =~ /[a-zA-Z0-9]/)
57 while ( length($w) < $id_length )
59 $w .= gen_puuid
( $id_length - length( $w ) );
62 $w = substr( $w, 0, $id_length );
66 sub parse_arguments
($)
71 my $start_time = time;
74 if (( $#ARGV >= 1) && ( $ARGV[1] eq "at" ))
76 # Start and Activity require a title to be present.
77 # All other (stop, main...) do not ^^.
78 if ( ($step == START
) || ($step == TASK
) )
80 # TODO: Allow no title!
81 # If no title is given, read ID of previously used project in stead :)
82 help
($step) unless $#ARGV > 3;
83 $title = join(" ", @ARGV[4..$#ARGV]);
85 help
(TIMEFORMAT
) unless ( $ARGV[2] =~ m/\d\d\d\d-\d\d-\d\d/ && $ARGV[3] =~ m/\d\d:\d\d/);
87 $start_time = parse_time
( $ARGV[2], $ARGV[3] );
89 elsif ( ($step == START
) || ($step == TASK
) )
92 $title = join(" ", @ARGV);
95 if ( not defined $title )
101 return ( $start_time, $title );
105 sub get_current_project
107 return undef if ( ! -f
$trk_dir . "/current" );
108 open ( CUR
, "<" . $trk_dir . "/current" ) or die ("Unable to read current project file");
115 sub set_current_project
($)
118 return undef if ( -f
$trk_dir . "/current" );
119 open ( CUR
, ">" . $trk_dir . "/current" ) or die ("Unable to write current project file");
120 printf(CUR
"%s\n", $id );
123 open ( LAST
, ">" . $trk_dir . "/last" ) or die ("Unable to write last project file");
124 printf(LAST
"%s\n", $id );
130 my $project = get_current_project
();
131 return undef if not $project;
133 open ( CUR
, "<" . $trk_dir . "/current" ) or die ("Unable to read current project file");
146 foreach my $d ( <$trk_dir/*> )
149 next if not -f
$d . "/info";
151 my $id = basename
($d);
152 my $title = get_project_name
( $id );
154 $projects{$id} = $title unless not defined $title;
161 sub get_project_id
($)
165 # Get hash of project-id's and -names from get_projects
166 my $projects = get_projects
();
168 # Look up name in list
169 foreach my $id ( keys $projects )
172 return $id if ( $projects->{$id} eq $title )
175 # If no match, return undef.
179 sub get_project_name
($)
182 open(PRO
, "<" . $trk_dir . "/" . $id . "/info" ) or die ("Unable to read project medatata file!");
186 next if not $_ =~ /^title:(.*)/;
193 sub create_project
($)
202 } while ( -d
$trk_dir . "/" . $id );
203 mkdir ( $trk_dir . "/" . $id );
205 open(PRO
, ">" . $trk_dir . "/" . $id . "/info" ) or die ("Unable to create project medatata file!");
206 printf(PRO
"title:%s", $title);
212 ############################################################
216 mkdir $trk_dir or die("Unable to create data directory");
224 my $command = $ARGV[0];
226 if ( ( $command eq "start") || ($command eq "on" ) )
233 my ( $start_time, $title ) = parse_arguments
(START
);
235 my $current = get_current_project
();
238 $current = get_project_id
( $title );
241 printf("No project by that name! Creating a new one.\n");
242 $current = create_project
($title);
246 printf("Continuing tracking for existing project.\n");
248 set_current_project
($current);
252 printf("A project is being tracked: %s\n", get_project_name
( $current ) );
253 printf("Stop current tracking before starting a new one\n");
257 # First iteration is VERY naive: simply add the start time to the bottom of the tracking file
258 # Will have to do more logic: if the start point is before one of the times already in the track,
259 # the file will have to be manipulated to get coherent tracking!
260 open (TRACK
, ">>" . $trk_dir . "/" . $current . "/tracking" ) or die ("Unable to open file, $!");
261 printf(TRACK
"[%s]", $start_time);
264 printf("Started tracking of '%s' at %s\n\n", $title, scalar localtime $start_time);
266 elsif ( ( $command eq "stop") || ($command eq "off" ) )
274 my $stop_time = parse_arguments
(STOP
);
276 my $current = get_current_project
();
279 printf("No project is currently tracked. To stop, please start first\n");
282 my $title = get_project_name
( $current );
284 die ("Project exists, but tracking file does not!") if ( not -f
$trk_dir . "/" . $current . "/tracking" );
286 # First iteration is VERY naive: simply add the stop time to the bottom line of the tracking file
287 # Will have to do more logic: if the start point is before one of the times already in the track,
288 # the file will have to be manipulated to get coherent tracking!
289 # In addtion to this: actually do some file sanity checking!
290 open (TRACK
, ">>" . $trk_dir . "/" . $current . "/tracking" ) or die ("Unable to open file, $!");
291 printf(TRACK
" to [%s]\n", $stop_time);
294 unlink ( $trk_dir . "/current" );
296 printf("Stopped tracking of '%s' at %s\n\n", $title, scalar localtime $stop_time);