-
Notifications
You must be signed in to change notification settings - Fork 2
/
todo.spell
executable file
·85 lines (78 loc) · 1.9 KB
/
todo.spell
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
#!/bin/sh
# Write/remove a task to do later.
#
# Select an existing entry to remove it from the file, or type a new entry to
# add it.
selector() {
while [ "$#" -gt 0 ]; do
case "$1" in
-l) listsize="$2" ;;
-p) prompt="$2" ;;
esac
shift
done
picker -i -p "$prompt" -l "$listsize"
}
error() {
title=$1
shift
if [ -t 0 ]; then
printf "[1;31mError:[0m[1m %s[0m\n%s\n" "$title" "$*"
else
notify-send "Error: $title" "$*" -u critical -a todo
fi
}
todo() {
case "$LOGNAME" in
work) f=-E ;;
mendess) f=-Ev ;;
*) f=-vF ;;
esac
LC_ALL=C sort "$file" | grep $f '^work'
}
pull() {
o="$(rsync --update --timeout=5 mendess.xyz:todo "$CACHE" 2>&1)" || error "Download failed" "$o"
}
while [ "$#" -gt 0 ]; do
case "$1" in
gui) gui=1 ;;
-l | --list) list=1 ;;
-b | --bg-pull) bg_pull=1 ;;
esac
shift
done
CACHE="/tmp/$LOGNAME"
mkdir -p "$CACHE"
file="$CACHE/todo"
if [ "$bg_pull" ]; then
pull &
[ -e "$file" ] || exit
else
pull
fi
[ -f "$file" ] || exit 1
touch "$file"
height=$(wc -l <"$file")
prompt="Add/delete a task: "
if [ "$list" ]; then
if [ -t 0 ]; then
todo
else
notify-send 'Todo list' "$(todo)" -a todo
fi
else
while :; do
cmd=$(todo | selector -l "$height" -p "$prompt")
[ "$cmd" ] || break
[ "$LOGNAME" = work ] && echo "$cmd" | grep -qv '^work' && cmd="work: $cmd"
if grep --quiet --line-regex --fixed-strings "$cmd" "$file"; then
grep --line-regex --fixed-strings --invert-match "$cmd" "$file" >"$file.$$"
mv "$file.$$" "$file"
height=$((height - 1))
else
echo "$cmd" >>"$file"
height=$((height + 1))
fi
done
o="$(rsync --update --timeout=5 "$file" mendess.xyz: 2>&1)" || error "Upload failed" "$o"
fi