Blob


1 #!/bin/sh
3 set -o pipefail
4 program=$(basename "$0")
5 fail() { echo "$1"; exit 1; }
7 todo_file="${TODO_FILE:-${PWD}/TODO}"
9 test -f "$todo_file" || fail "TODO_FILE not found"
11 usage() {
12 echo "usage: $program TASK"
13 echo " $program [-d LINENUM] [-e] [-Ss QUERY]"
14 }
16 # t_done(int)
17 t_done() {
18 int=$1
19 tmpfile=$(mktemp)
20 sed -n "${int}!p" "$todo_file" > "$tmpfile"
21 mv "$todo_file" "${todo_file}~"
22 mv "$tmpfile" "$todo_file"
23 }
25 t_print() {
26 lines=$(wc -l < "$todo_file")
27 width=$(echo $lines | wc -c)
28 nl -s' ' -w"$width" "$todo_file"
29 }
31 main() {
32 if getopts hed:S:s: opt; then
33 case "$opt" in
34 (e) $EDITOR "$todo_file"
35 exit ;;
36 (d) t_done "$OPTARG"
37 exit ;;
38 (S) t_print | grep -iw "$OPTARG"
39 exit ;;
40 (s) t_print | grep -i "$OPTARG"
41 exit ;;
42 (h) usage
43 exit ;;
44 (?) usage
45 exit 1 ;;
46 esac
47 elif test -n "$1"; then
48 echo "$@" >> "$todo_file"
49 else
50 t_print
51 fi
52 }
54 main "$@"