forked from asb/sh-todo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodo
executable file
·153 lines (133 loc) · 3.67 KB
/
todo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/bin/sh
# Part of sh-todo:
# https://github.com/asb/sh-todo
set -e
. ~/.sh-todo
SCRIPTNAME=$(basename $0)
# Return 0 if $2 contains all tags in $1 and 1 otherwise. Returns 0 in the
# case that $1 is empty.
match_tags() {
WANTEDTAGS=$1
LINETAGS="$2 "
for TAG in $WANTEDTAGS; do
if [ "${LINETAGS#*$TAG }" = "$LINETAGS" ]; then
return 1
fi
done
return 0
}
output_line() {
LINE=$1
if [ "${LINE#DONE }" != "$LINE" ]; then
printf "• $BEGIN_DONE%s$END_DONE\n" "${LINE#DONE }"
else
printf "• %s\n" "${LINE#TODO }"
fi
}
parse_linetags() {
LINETAGS="${1#*\[}"
if [ "$LINETAGS" != "$LINE" ]; then
LINETAGS="${LINETAGS%]*} "
else
LINETAGS=""
fi
printf "%s" "$LINETAGS"
}
# If given a todo, then add that to the list of tasks. Otherwise, print all
# todo items which have the given tags
do_todo() {
[ -e "$TODO_F" ] || touch "$TODO_F"
if [ -z "$INPUT" ]; then # No todo given, so print current tasks with matching tags
while read -r LINE; do
if match_tags "$ARGTAGS" "$(parse_linetags "$LINE")"; then
output_line "$LINE"
fi
done < "$TODO_F"
else # Must be adding a new todo
printf "TODO %s%s\n" "$INPUT" "${ARGTAGS:+ [$ARGTAGS]}" >> "$TODO_F"
fi
exit 0
}
# Mark any tasks matching the given pattern as done. Tags can also be
# specified to limit the scope of the match
do_todone() {
if [ -z "$INPUT" ]; then
printf "Error: no todo to mark done specified\n" >&2
exit 1
fi
> "$TODO_F.tmp"
HAD_MATCH=0
while read -r LINE; do
if [ "${LINE#TODO *$INPUT}" != "$LINE" ] && match_tags "$ARGTAGS" "$(parse_linetags "$LINE")"; then
HAD_MATCH=1
TASK="${LINE#TODO }"
LINE=$(printf "DONE %s ($(date "+%Y-%m-%d %R"))" "$TASK")
output_line "$LINE"
printf "%s\n" "$LINE" >> "$TODONE_F"
fi
printf "%s\n" "$LINE" >> "$TODO_F.tmp"
done < "$TODO_F"
if [ $HAD_MATCH -eq 0 ]; then
printf "Error: no tasks matched the given pattern\n" >&2
rm "$TODO_F.tmp"
exit 1
fi
mv "$TODO_F.tmp" "$TODO_F"
}
# Remove all DONE lines with the given tags from $TODO_F. All such lines will
# already have been added to $TODONE_F
do_todone_archive() {
> "$TODO_F.tmp"
while read -r LINE; do
if [ "${LINE#DONE }" != "$LINE" ] && match_tags "$ARGTAGS" "$(parse_linetags "$LINE")"; then
output_line "$LINE"
else
printf "%s\n" "$LINE" >> "$TODO_F.tmp"
fi;
done < "$TODO_F"
mv "$TODO_F.tmp" "$TODO_F"
}
# Show a formatted list of completed tasks (can be limited by specifying
# desired tags).
# This function uses tac which is non-POSIX, as is the -d argument to date
do_todone_view() {
TAC=tac
#TAC="tail -r" # For OSX/BSD
$TAC "$TODONE_F" | while read -r LINE; do
if ! match_tags "$ARGTAGS" "$(parse_linetags "$LINE")"; then
continue;
fi
TRIMLINE=${LINE%(*)} # strip the timestamp
TIMESTAMP=${LINE#"$TRIMLINE("}
TIMESTAMP=${TIMESTAMP%)}
DATE=${TIMESTAMP%% [0-9][0-9]:[0-9][0-9]}
TIME=${TIMESTAMP#"$DATE "}
if [ "$DATE" != "$LASTDATE" ]; then
printf "\n$(date -d"$DATE" "+%A %d %B %Y")\n\n"
fi
printf "• %s($TIME)\n" "${TRIMLINE#DONE }"
LASTDATE=$DATE
done | less
}
# Parse arguments
ARGTAGS=""
INPUT=""
for ARG in "$@"; do
if [ "${ARG#@}" != "$ARG" ]; then
ARGTAGS="${ARGTAGS:+$ARGTAGS }$ARG"
else
INPUT="${INPUT:+$INPUT }$ARG"
fi
done
if [ "$SCRIPTNAME" = "todo" ]; then
do_todo
elif [ "$SCRIPTNAME" = "todone" ]; then
do_todone
elif [ "$SCRIPTNAME" = "todone-archive" ]; then
do_todone_archive
elif [ "$SCRIPTNAME" = "todone-view" ]; then
do_todone_view
else
printf "Error: this is a multicall script, but was called with unrecognised script name '%s'\n" "$SCRIPTNAME" >&2
exit 1
fi