tt.sh
author viric <viriketo@gmail.com>
Mon, 15 Mar 2010 23:33:56 +0100
changeset 15 58420761aeae
parent 9 d571ba4d952c
child 17 f0fcc2a59caf
permissions -rwxr-xr-x
Adding AUTHORS file.

#!/bin/sh -e
# (encoding: UTF-8)
#
# tt 0.6 - Time Tracker
#   (Instructions using -h)
# LICENSE
# Copyright (C) 2009 LluĂ­s Batlle i Rossell
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

PNAME=`basename "$0"`

if [ -z "$EDITOR" ]; then
	EDITOR=vim
fi

if [ -z "$TT_PROJECT" ]; then
	TT_PROJECT=$HOME/.tt
fi

function usage
{
	echo "Usage: $PNAME [-d] [-l] [-t]"
    echo "       $PNAME <task>"
    echo "Manual:"
    echo "  Switch to a task         :  tt mytask"
    echo "  List task switches       :  tt -l"
    echo "  List time spent per task :  tt -t"
    echo "  Sum times given with -t  :  tt -t | grep ... | tt -s"
    echo "  List what is tracked     :  tt"
    echo "  \$TT_PROJECT or ~/.tt stores the tracking."
}

function date2iso
{
	date -d "$1" '+%Y-%m-%d %T'
}

function date2timestamp
{
	date -d "$1" '+%s'
}

function add
{
	echo "`date2iso now`"$'\t'"$1" >> "$TT_PROJECT"
}

function list
{
	if [ -f "$TT_PROJECT" ]; then
		cat "$TT_PROJECT"
	fi
}

function listtime
{
	if [ -f "$TT_PROJECT" ]; then
		cat < "$TT_PROJECT" |
        (
            read A
            STARTTIME=`echo "$A" | cut -f 1`
            NAME=`echo "$A" | cut -f 2`
            while read A; do
                ENDTIME=`echo "$A" | cut -f 1`
                echo `timebetween "$STARTTIME" "$ENDTIME"` \
                    "$NAME" "($STARTTIME)"
                NAME=`echo "$A" | cut -f 2`
                STARTTIME="$ENDTIME"
            done
            echo `timebetween "$STARTTIME" now` \
                "$NAME" "($STARTTIME)"
        )
	fi
}

function sec2hms
{
    MINUTES=0
    HOURS=0
    SECONDS=$1
    if [ $SECONDS -ge 60 ]; then
        MINUTES=$((SECONDS / 60))
        SECONDS=$((SECONDS - MINUTES * 60))
    fi

    if [ $MINUTES -ge 60 ]; then
        HOURS=$((MINUTES / 60))
        MINUTES=$((MINUTES - HOURS * 60))
    fi

    printf "%02d:%02d:%02d\n" $HOURS $MINUTES $SECONDS
}

function timebetween
{
	STARTSEC=`date2timestamp "$1"`
	ENDSEC=`date2timestamp "$2"`
    
    SECONDS=$((ENDSEC - STARTSEC))

    echo `sec2hms $SECONDS`
}

function status
{
	if [ -f "$TT_PROJECT" ]; then
        NAME=`tail -n 1 "$TT_PROJECT" | cut -f 2`
        STARTDATE=`tail -n 1 "$TT_PROJECT" | cut -f 1`

        echo `timebetween "$STARTDATE" "now"` "$NAME"
    fi
}

function sum
{
    VAL=`awk -F : -- '
    BEGIN { val=0; }
    { val += $1 * 60 * 60 + $2 * 60 + $3 }
    END { print val }' `
    
    echo `sec2hms $VAL`
}

if [ $# -eq 0 ]; then
	status || exit 1
elif [ "$1" == "-l" ]; then
	list
elif [ "$1" == "-t" ]; then
	listtime
elif [ "$1" == "-s" ]; then
	sum
elif [ "$1" == "-h" ]; then
	usage
else
	add $@
fi

exit 0