Implemented `todo -T`, listing every known tag
[todo.git] / todo.sh
blob069e677b556b17b43b257eb7de9d8fc4686dcaa7
1 #!/bin/sh
2 # TODO, a cli GTD toy
3 # Copyright (C) 2008 Alejandro Mery <amery@geeks.cl>
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 todo_usage()
20 local cmd="${0##*/}"
22 [ $# -eq 0 ] || echo "ERROR: $*" >&2
23 cat <<-EOT >&2
24 Usage: $0 [options] [<object>]
25 ${cmd} [-t <tag>*] [<message>] - new entry
26 ${cmd} [-T|--tags] - list tags
28 EOT
29 exit 1
32 todo_trap()
34 echo "Ctrl-C pressed, aborting." >&2
35 rm -rf "$TODOTMP"
36 exit 1
39 trap ':' INT
41 options=$( getopt -o "t:T" -l "tags" -- "$@" )
42 if [ $? -ne 0 ]; then
43 todo_usage
46 # load new arguments list
47 eval set -- "$options"
49 TAGS=
50 MODE=new
52 while [ $# -gt 0 ]; do
53 case "$1" in
54 -T|--tags)
55 MODE=tags ;;
57 -t) TAGS="$TAGS $2"; shift ;;
59 --) shift; break ;;
60 -*) todo_usage unknown option "$1"
62 esac
63 shift
64 done
66 TAGS=$( echo "$TAGS" | tr 'a-z' 'A-Z' )
68 TODODIR="$HOME/.todo"
70 todo_new()
72 TODOTMP="${TMP:-/tmp}/.todo.$$"
73 DATE=
74 # find a unique date/hash
76 while [ -z "$DATE" ]; do
77 DATE=$(date --rfc-3339=seconds)
78 DATEHASH=$( echo "$DATE" | sha1sum | cut -d' ' -f1 )
79 DATEHASH_1=$( echo "$DATEHASH" | cut -c 1 )
80 ENTRYDIR="$TODODIR/entries/$DATEHASH_1"
81 ENTRY="$ENTRYDIR/$DATEHASH"
83 if [ -e "$ENTRY" ]; then
84 DATE=
85 sleep 1
86 else
87 mkdir -p "$ENTRYDIR"
89 done
91 if [ $# -gt 0 ]; then
92 echo "$*" > "$TODOTMP"
93 echo "Added: $DATE" >> "$TODOTMP"
94 else
95 echo "Subject: ..." > "$TODOTMP.empty"
96 echo "Added: $DATE" >> "$TODOTMP.empty"
97 cp "$TODOTMP.empty" "$TODOTMP"
99 ${EDITOR:-vi} "$TODOTMP"
101 if cmp -s "$TODOTMP" "$TODOTMP.empty"; then
102 rm -f "$TODOTMP" "$TODOTMP.empty"
103 return
107 mv "$TODOTMP" "$ENTRY"
109 for tag in $TAGS INBOX; do
110 [ -d "$TODODIR/$tag" ] || mkdir "$TODODIR/$tag"
112 ln -s "../entries/$DATEHASH_1/$DATEHASH" "$TODODIR/$tag/$DATEHASH"
113 done
115 echo "$DATEHASH"
118 todo_tags()
120 ls -1d "$TODODIR"/* | sed -e "s,^$TODODIR/,," | grep -v '^entries$'
123 case "$MODE" in
124 new) todo_new "$@" ;;
125 tags) todo_tags ;;
126 esac