Base tt.
authorviric@mandarina
Thu, 12 Feb 2009 22:39:06 +0100
changeset 0 00043643385b
child 1 248540522080
Base tt.
tt
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tt	Thu Feb 12 22:39:06 2009 +0100
@@ -0,0 +1,126 @@
+#!/bin/sh -e
+# (encoding: UTF-8)
+#
+# tt 0.5 - Time Tracker
+#  (Instructions below the license)
+# LICENSE
+# Copyright (C) 2006 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.
+#
+# Instructions:
+#  1. Your $TT_PROJECT should point to a file. It will be the database you'll
+#     be using. Use an absolute path. It may be, for instance, "~/todo.bug"
+#
+
+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] task"
+}
+
+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" "$ENDTDIME"` \
+                    "$NAME"
+                STARTTIME=$ENDTIME
+            done
+            echo `timebetween "$STARTTIME" now` \
+                "$NAME"
+        )
+	fi
+}
+
+function timebetween
+{
+	STARTSEC=`date2timestamp "$1"`
+	ENDSEC=`date2timestamp "$2"`
+    
+    SECONDS=$((ENDSEC - STARTSEC))
+
+    MINUTES=0
+    HOURS=0
+
+    if [ $SECONDS -gt 60 ]; then
+        MINUTES=$((SECONDS / 60))
+        SECONDS=$((SECONDS - MINUTES * 60))
+    fi
+
+    if [ $MINUTES -gt 60 ]; then
+        HOURS=$((MINUTES / 60))
+        MINUTES=$((MINUTES - HOURS * 60))
+    fi
+
+    printf "%02d:%02d:%02d\n" $HOURS $MINUTES $SECONDS
+}
+
+function status
+{
+    NAME=`tail -n 1 "$TT_PROJECT" | cut -f 2`
+	STARTDATE=`tail -n 1 "$TT_PROJECT" | cut -f 1`
+
+    echo `timebetween "$STARTDATE" "now"` "$NAME"
+}
+
+if [ $# -eq 0 ]; then
+	status || exit 1
+elif [ "$1" == "-l" ]; then
+	list
+elif [ "$1" == "-t" ]; then
+	listtime
+else
+	add $@
+fi
+
+exit 0