-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathnotes-todos
executable file
·80 lines (66 loc) · 2.08 KB
/
notes-todos
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
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
__usage="
Usage: notes todos [--help | -h | -?] [--all | --done] [--export | -e] [<folder>...]
\`notes todos\` lists all todos from your notes
Options:
-h, -?, --help Output usage
--all, -a List both done and not done todos
--done, -d List only done todos
Arguments:
<folder> If passed space-separated folders inside your \$NOTES_PATH, will search for todos only in those recursively
\$NOTES_PATH defaults to ~/notes.
By default \`notes todos\` will list only not done todos.
You can filter by using ``--all\` for all todos or \`--done\` for only done todos.
"
FOLDERS=()
MODE_NOT_DONE=0x1
MODE_DONE=0x10
MODE_ALL=0x11
MODE=$MODE_NOT_DONE
OUTPUT=/dev/stdout
NOTES_PATH=${NOTES_PATH:-$HOME/notes}
while (( "$#" )); do
case "$1" in
-h|-'?'|--help)
echo "$__usage"
exit 0
;;
--all|-a)
MODE=$MODE_ALL
shift
;;
--done|-d)
MODE=$MODE_DONE
shift
;;
--*=|-*) # unsupported options
echo "Error: Unsupported option \"$1\"" >&2
echo "$__usage" >&2
exit 1
;;
*) # preserve positional arguments
FOLDERS+=( "$1" )
shift
;;
esac
done
# set positional arguments in their proper place
set -- "${FOLDERS[@]}"
# Use regex pattern depending on todo filters
checked=""
(( (MODE & MODE_DONE) != 0 )) && checked="x$checked"
(( (MODE & MODE_NOT_DONE) != 0 )) && checked=" $checked"
pattern="\s*-\s*\[[$checked]\]\s*.*"
# Determine note sources based on folders
notes_sources=("${FOLDERS[@]/#/$NOTES_PATH\/}")
matching_files=$(grep --recursive --files-with-matches --extended-regexp --regexp="$pattern" --exclude="_*" --exclude-dir="_*" --exclude=".*" --exclude-dir=".*" "${notes_sources[@]-$NOTES_PATH}")
for filename in $matching_files; do
{
echo "${filename#$NOTES_PATH/}:"
echo
grep --extended-regexp "$pattern" "$filename"
echo
} >> "$OUTPUT"
done