author | llbatlle@comanegra |
Fri, 01 Apr 2011 10:06:32 +0200 | |
changeset 21 | 5dababf1cb7d |
parent 17 | f0fcc2a59caf |
permissions | -rwxr-xr-x |
0 | 1 |
#!/bin/sh -e |
2 |
# (encoding: UTF-8) |
|
3 |
# |
|
5 | 4 |
# tt 0.6 - Time Tracker |
6 | 5 |
# (Instructions using -h) |
0 | 6 |
# LICENSE |
1 | 7 |
# Copyright (C) 2009 LluĂs Batlle i Rossell |
0 | 8 |
# |
9 |
# This program is free software; you can redistribute it and/or |
|
10 |
# modify it under the terms of the GNU General Public License |
|
11 |
# as published by the Free Software Foundation; either version 2 |
|
12 |
# of the License, or (at your option) any later version. |
|
13 |
# |
|
14 |
# This program is distributed in the hope that it will be useful, |
|
15 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17 |
# GNU General Public License for more details. |
|
18 |
# |
|
19 |
# You should have received a copy of the GNU General Public License |
|
20 |
# along with this program; if not, write to the Free Software |
|
21 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
22 |
||
23 |
PNAME=`basename "$0"` |
|
24 |
||
25 |
if [ -z "$EDITOR" ]; then |
|
26 |
EDITOR=vim |
|
27 |
fi |
|
28 |
||
29 |
if [ -z "$TT_PROJECT" ]; then |
|
30 |
TT_PROJECT=$HOME/.tt |
|
31 |
fi |
|
32 |
||
33 |
function usage |
|
34 |
{ |
|
17
f0fcc2a59caf
Making the tt help show the proper parameters available.
viric <viriketo@gmail.com>
parents:
9
diff
changeset
|
35 |
echo "Usage: $PNAME <-s|-l|-t|-h>" |
1 | 36 |
echo " $PNAME <task>" |
37 |
echo "Manual:" |
|
38 |
echo " Switch to a task : tt mytask" |
|
39 |
echo " List task switches : tt -l" |
|
40 |
echo " List time spent per task : tt -t" |
|
4 | 41 |
echo " Sum times given with -t : tt -t | grep ... | tt -s" |
1 | 42 |
echo " List what is tracked : tt" |
4 | 43 |
echo " \$TT_PROJECT or ~/.tt stores the tracking." |
0 | 44 |
} |
45 |
||
46 |
function date2iso |
|
47 |
{ |
|
48 |
date -d "$1" '+%Y-%m-%d %T' |
|
49 |
} |
|
50 |
||
51 |
function date2timestamp |
|
52 |
{ |
|
53 |
date -d "$1" '+%s' |
|
54 |
} |
|
55 |
||
56 |
function add |
|
57 |
{ |
|
58 |
echo "`date2iso now`"$'\t'"$1" >> "$TT_PROJECT" |
|
59 |
} |
|
60 |
||
61 |
function list |
|
62 |
{ |
|
63 |
if [ -f "$TT_PROJECT" ]; then |
|
64 |
cat "$TT_PROJECT" |
|
65 |
fi |
|
66 |
} |
|
67 |
||
68 |
function listtime |
|
69 |
{ |
|
70 |
if [ -f "$TT_PROJECT" ]; then |
|
71 |
cat < "$TT_PROJECT" | |
|
72 |
( |
|
73 |
read A |
|
74 |
STARTTIME=`echo "$A" | cut -f 1` |
|
75 |
NAME=`echo "$A" | cut -f 2` |
|
76 |
while read A; do |
|
77 |
ENDTIME=`echo "$A" | cut -f 1` |
|
2 | 78 |
echo `timebetween "$STARTTIME" "$ENDTIME"` \ |
1 | 79 |
"$NAME" "($STARTTIME)" |
2 | 80 |
NAME=`echo "$A" | cut -f 2` |
81 |
STARTTIME="$ENDTIME" |
|
0 | 82 |
done |
83 |
echo `timebetween "$STARTTIME" now` \ |
|
1 | 84 |
"$NAME" "($STARTTIME)" |
0 | 85 |
) |
86 |
fi |
|
87 |
} |
|
88 |
||
4 | 89 |
function sec2hms |
0 | 90 |
{ |
91 |
MINUTES=0 |
|
92 |
HOURS=0 |
|
4 | 93 |
SECONDS=$1 |
9
d571ba4d952c
Fixing a bug in the 'sh' version, regarding to 00:60:00 time reports, for example.
Lluís Batlle <viriketo@gmail.com>
parents:
7
diff
changeset
|
94 |
if [ $SECONDS -ge 60 ]; then |
0 | 95 |
MINUTES=$((SECONDS / 60)) |
96 |
SECONDS=$((SECONDS - MINUTES * 60)) |
|
97 |
fi |
|
98 |
||
9
d571ba4d952c
Fixing a bug in the 'sh' version, regarding to 00:60:00 time reports, for example.
Lluís Batlle <viriketo@gmail.com>
parents:
7
diff
changeset
|
99 |
if [ $MINUTES -ge 60 ]; then |
0 | 100 |
HOURS=$((MINUTES / 60)) |
101 |
MINUTES=$((MINUTES - HOURS * 60)) |
|
102 |
fi |
|
103 |
||
104 |
printf "%02d:%02d:%02d\n" $HOURS $MINUTES $SECONDS |
|
105 |
} |
|
106 |
||
4 | 107 |
function timebetween |
108 |
{ |
|
109 |
STARTSEC=`date2timestamp "$1"` |
|
110 |
ENDSEC=`date2timestamp "$2"` |
|
111 |
||
112 |
SECONDS=$((ENDSEC - STARTSEC)) |
|
113 |
||
114 |
echo `sec2hms $SECONDS` |
|
115 |
} |
|
116 |
||
0 | 117 |
function status |
118 |
{ |
|
3
75385193714b
Marc Weber found a bug in 'tt' without parameter.
viric@mandarina
parents:
2
diff
changeset
|
119 |
if [ -f "$TT_PROJECT" ]; then |
75385193714b
Marc Weber found a bug in 'tt' without parameter.
viric@mandarina
parents:
2
diff
changeset
|
120 |
NAME=`tail -n 1 "$TT_PROJECT" | cut -f 2` |
75385193714b
Marc Weber found a bug in 'tt' without parameter.
viric@mandarina
parents:
2
diff
changeset
|
121 |
STARTDATE=`tail -n 1 "$TT_PROJECT" | cut -f 1` |
0 | 122 |
|
3
75385193714b
Marc Weber found a bug in 'tt' without parameter.
viric@mandarina
parents:
2
diff
changeset
|
123 |
echo `timebetween "$STARTDATE" "now"` "$NAME" |
75385193714b
Marc Weber found a bug in 'tt' without parameter.
viric@mandarina
parents:
2
diff
changeset
|
124 |
fi |
0 | 125 |
} |
126 |
||
4 | 127 |
function sum |
128 |
{ |
|
129 |
VAL=`awk -F : -- ' |
|
130 |
BEGIN { val=0; } |
|
131 |
{ val += $1 * 60 * 60 + $2 * 60 + $3 } |
|
132 |
END { print val }' ` |
|
133 |
||
134 |
echo `sec2hms $VAL` |
|
135 |
} |
|
136 |
||
0 | 137 |
if [ $# -eq 0 ]; then |
138 |
status || exit 1 |
|
139 |
elif [ "$1" == "-l" ]; then |
|
140 |
list |
|
141 |
elif [ "$1" == "-t" ]; then |
|
142 |
listtime |
|
4 | 143 |
elif [ "$1" == "-s" ]; then |
144 |
sum |
|
1 | 145 |
elif [ "$1" == "-h" ]; then |
146 |
usage |
|
0 | 147 |
else |
148 |
add $@ |
|
149 |
fi |
|
150 |
||
151 |
exit 0 |