From: Jon Langseth Date: Wed, 8 May 2013 14:05:05 +0000 (+0200) Subject: New, generified names X-Git-Url: https://git.defcon.no/?a=commitdiff_plain;h=218a7ab2723720f39fed20ff08f7829e84180d6c;p=trk New, generified names --- diff --git a/trk b/trk index a44c59b..deb8e9c 100755 --- a/trk +++ b/trk @@ -95,7 +95,7 @@ sub parse_arguments ($) if ( ($step == START) || ($step == TASK) ) { # TODO: Allow no title! - # If no title is given, read ID of previously used project in stead :) + # If no title is given, read ID of previously used track in stead :) help($step) unless $#ARGV > 3; $title = join(" ", @ARGV[4..$#ARGV]); } @@ -119,56 +119,42 @@ sub parse_arguments ($) } } -sub get_last_project +sub get_last_id { return undef if ( ! -f $trk_dir . "/last" ); - open ( CUR, "<" . $trk_dir . "/last" ) or die ("Unable to read last project file"); + open ( CUR, "<" . $trk_dir . "/last" ) or die ("Unable to read last track file"); my $id = ; chomp($id); close(CUR); return $id; } -sub get_current_project +sub get_current_id { return undef if ( ! -f $trk_dir . "/current" ); - open ( CUR, "<" . $trk_dir . "/current" ) or die ("Unable to read current project file"); + open ( CUR, "<" . $trk_dir . "/current" ) or die ("Unable to read current track file"); my $id = ; chomp($id); close(CUR); return $id; } -sub set_current_project ($) +sub set_current_id ($) { my $id = shift; return undef if ( -f $trk_dir . "/current" ); - open ( CUR, ">" . $trk_dir . "/current" ) or die ("Unable to write current project file"); + open ( CUR, ">" . $trk_dir . "/current" ) or die ("Unable to write current track file"); printf(CUR "%s\n", $id ); close(CUR); - open ( LAST, ">" . $trk_dir . "/last" ) or die ("Unable to write last project file"); + open ( LAST, ">" . $trk_dir . "/last" ) or die ("Unable to write last track file"); printf(LAST "%s\n", $id ); close(LAST); } -sub current_task +sub get_tracks { - my $project = get_current_project(); - return undef if not $project; - - open ( CUR, "<" . $trk_dir . "/current" ) or die ("Unable to read current project file"); - ; - my $id = ; - chomp($id); - close(CUR); - return $id; - -} - -sub get_projects -{ - my %projects; + my %tracks; foreach my $d ( <$trk_dir/*> ) { @@ -176,37 +162,37 @@ sub get_projects next if not -f $d . "/info"; my $id = basename($d); - my $title = get_project_name( $id ); + my $title = get_track_name( $id ); - $projects{$id} = $title unless not defined $title; + $tracks{$id} = $title unless not defined $title; } - return \%projects; + return \%tracks; } -sub get_project_id ($) +sub get_track_id ($) { my $title = shift; - # Get hash of project-id's and -names from get_projects - my $projects = get_projects(); + # Get hash of track-id's and -names from get_tracks + my $tracks = get_tracks(); # Look up name in list - foreach my $id ( keys $projects ) + foreach my $id ( keys $tracks ) { # Return ID for name - return $id if ( $projects->{$id} eq $title ) + return $id if ( $tracks->{$id} eq $title ) } # If no match, return undef. return undef; } -sub get_project_name ($) +sub get_track_name ($) { my $id = shift; - open(PRO, "<" . $trk_dir . "/" . $id . "/info" ) or die ("Unable to read project medatata file!"); + open(PRO, "<" . $trk_dir . "/" . $id . "/info" ) or die ("Unable to read track medatata file!"); my $title = undef; while () { @@ -217,7 +203,7 @@ sub get_project_name ($) return $title; } -sub create_project ($) +sub create_track ($) { my $title = shift; @@ -229,20 +215,20 @@ sub create_project ($) } while ( -d $trk_dir . "/" . $id ); mkdir ( $trk_dir . "/" . $id ); - open(PRO, ">" . $trk_dir . "/" . $id . "/info" ) or die ("Unable to create project medatata file!"); + open(PRO, ">" . $trk_dir . "/" . $id . "/info" ) or die ("Unable to create track medatata file!"); printf(PRO "title:%s\n", $title); close(PRO); return $id; } -sub start_project ($$) +sub start_track ($$) { my $start_time = shift; my $title = shift; - my $current = get_current_project(); + my $current = get_current_id(); if ( not $current ) { if ( not $title ) @@ -251,18 +237,18 @@ sub start_project ($$) } else { - $current = get_project_id( $title ); + $current = get_track_id( $title ); if ( not $current ) { - printf("No project by that name! Creating a new one.\n"); - $current = create_project($title); + printf("No track by that name! Creating a new one.\n"); + $current = create_track($title); } } # Break off here if we haven't gotten an ID yet. return undef if not $current; - set_current_project($current); + set_current_id($current); # First iteration is VERY naive: simply add the start time to the bottom of the tracking file # Will have to do more logic: if the start point is before one of the times already in the track, @@ -277,12 +263,12 @@ sub start_project ($$) return undef; } -sub close_project ($) +sub close_track ($) { my $stop_time = shift; - my $current = get_current_project(); + my $current = get_current_id(); die ("Project exists, but tracking file does not!") if ( not -f $trk_dir . "/" . $current . "/tracking" ); @@ -320,10 +306,10 @@ if ( ( $command eq "start") || ($command eq "on" ) ) my ( $start_time, $title ) = parse_arguments(START); - my $current = get_current_project(); + my $current = get_current_id(); if ( not $current ) { - $current = start_project( $start_time, $title ); + $current = start_track( $start_time, $title ); if ( not $current ) { @@ -333,9 +319,9 @@ if ( ( $command eq "start") || ($command eq "on" ) ) } else { - printf("A project is being tracked: %s\n", get_project_name( $current ) ); - close_project($start_time); - $current = start_project( $start_time, $title ); + printf("A project is being tracked: %s\n", get_track_name( $current ) ); + close_track($start_time); + $current = start_track( $start_time, $title ); } printf("Started tracking of '%s' at %s\n\n", $title, scalar localtime $start_time); @@ -350,15 +336,15 @@ elsif ( ( $command eq "stop") || ($command eq "off" ) ) my $stop_time = parse_arguments(STOP); - my $current = get_current_project(); + my $current = get_current_id(); if ( not $current ) { printf("No project is currently tracked. To stop, please start first\n"); exit(0); } - my $title = get_project_name( $current ); + my $title = get_track_name( $current ); - close_project($stop_time); + close_track($stop_time); printf("Stopped tracking of '%s' at %s\n\n", $title, scalar localtime $stop_time); } @@ -368,13 +354,13 @@ elsif ( ( $command eq "projects" ) || ( $command eq "list" ) ) # TODO: Sort list of names alphabetically # TODO: Get total-hours for projects # TODO: - my $projects = get_projects(); + my $tracks = get_tracks(); printf("Currently tracked project names:\n\n"); - my $current = get_current_project(); + my $current = get_current_id(); - foreach my $id ( keys $projects ) + foreach my $id ( keys $tracks ) { - printf(" %s %s\n", ($id eq $current ? ">" : " " ),$projects->{$id} ); + printf(" %s %s\n", ($id eq $current ? ">" : " " ),$tracks->{$id} ); } print("\n"); } @@ -382,11 +368,11 @@ elsif ( $command eq "edit" ) { my ( undef, $title ) = parse_arguments(EDIT); - my $id = get_last_project(); + my $id = get_last_id(); if ( $title ) { - $id = get_project_id($title); + $id = get_track_id($title); if ( not $id ) { printf("No project by that name. Try 'list'\n");