From 79941f1d420465470a9965b007f48dfd5592e0d4 Mon Sep 17 00:00:00 2001 From: Jon Langseth Date: Wed, 8 May 2013 15:51:46 +0200 Subject: [PATCH] Moved starting and stopping to subs, allowing more flexible code. Added auto-switch --- trk | 96 +++++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 68 insertions(+), 28 deletions(-) diff --git a/trk b/trk index 3895640..a44c59b 100755 --- a/trk +++ b/trk @@ -236,6 +236,67 @@ sub create_project ($) return $id; } +sub start_project ($$) +{ + my $start_time = shift; + my $title = shift; + + + my $current = get_current_project(); + if ( not $current ) + { + if ( not $title ) + { + $current = get_last( ); + } + else + { + $current = get_project_id( $title ); + if ( not $current ) + { + printf("No project by that name! Creating a new one.\n"); + $current = create_project($title); + } + } + + # Break off here if we haven't gotten an ID yet. + return undef if not $current; + + set_current_project($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, + # the file will have to be manipulated to get coherent tracking! + open (TRACK, ">>" . $trk_dir . "/" . $current . "/tracking" ) or die ("Unable to open file, $!"); + printf(TRACK "[%s]", time2str($start_time)); + close (TRACK); + + return $current; + } + + return undef; +} + +sub close_project ($) +{ + + my $stop_time = shift; + + my $current = get_current_project(); + + die ("Project exists, but tracking file does not!") if ( not -f $trk_dir . "/" . $current . "/tracking" ); + + # First iteration is VERY naive: simply add the stop time to the bottom line of the tracking file + # Will have to do more logic: if the start point is before one of the times already in the track, + # the file will have to be manipulated to get coherent tracking! + # In addtion to this: actually do some file sanity checking! + open (TRACK, ">>" . $trk_dir . "/" . $current . "/tracking" ) or die ("Unable to open file, $!"); + printf(TRACK " to [%s]\n", time2str($stop_time)); + close (TRACK); + + unlink ( $trk_dir . "/current" ); +} + ############################################################ if ( ! -d $trk_dir ) @@ -262,32 +323,21 @@ if ( ( $command eq "start") || ($command eq "on" ) ) my $current = get_current_project(); if ( not $current ) { - $current = get_project_id( $title ); + $current = start_project( $start_time, $title ); + if ( not $current ) { - printf("No project by that name! Creating a new one.\n"); - $current = create_project($title); + printf("Something weird happened.\n"); + exit(1); } - else - { - printf("Continuing tracking for existing project.\n"); - } - set_current_project($current); } else { printf("A project is being tracked: %s\n", get_project_name( $current ) ); - printf("Stop current tracking before starting a new one\n"); - exit(0); + close_project($start_time); + $current = start_project( $start_time, $title ); } - # 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, - # the file will have to be manipulated to get coherent tracking! - open (TRACK, ">>" . $trk_dir . "/" . $current . "/tracking" ) or die ("Unable to open file, $!"); - printf(TRACK "[%s]", time2str($start_time)); - close (TRACK); - printf("Started tracking of '%s' at %s\n\n", $title, scalar localtime $start_time); } elsif ( ( $command eq "stop") || ($command eq "off" ) ) @@ -308,17 +358,7 @@ elsif ( ( $command eq "stop") || ($command eq "off" ) ) } my $title = get_project_name( $current ); - die ("Project exists, but tracking file does not!") if ( not -f $trk_dir . "/" . $current . "/tracking" ); - - # First iteration is VERY naive: simply add the stop time to the bottom line of the tracking file - # Will have to do more logic: if the start point is before one of the times already in the track, - # the file will have to be manipulated to get coherent tracking! - # In addtion to this: actually do some file sanity checking! - open (TRACK, ">>" . $trk_dir . "/" . $current . "/tracking" ) or die ("Unable to open file, $!"); - printf(TRACK " to [%s]\n", time2str($stop_time)); - close (TRACK); - - unlink ( $trk_dir . "/current" ); + close_project($stop_time); printf("Stopped tracking of '%s' at %s\n\n", $title, scalar localtime $stop_time); } -- 2.39.2