forked from pimterry/notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notes
executable file
·290 lines (243 loc) · 7.14 KB
/
notes
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#!/usr/bin/env bash
# Default Date string before config
QUICKNOTE_FORMAT="quicknote-%Y-%m-%d"
NOTES_EXT="md"
# Look for configuration file at ~/.config/notes/config and use it
if [ -f ~/.config/notes/config ]; then
. ~/.config/notes/config
fi
configured_dir=${NOTES_DIRECTORY%/} # Remove trailing slashes
notes_dir="${configured_dir:-$HOME/notes}"
escaped_notes_dir="$(printf "$notes_dir" | sed -e 's/[]\/$*.^|[]/\\&/g')"
# Make sure the notes directory actually exists, and create it if it doesn't
if ! $(mkdir -p "$notes_dir"); then
echo "Could not create directory $notes_dir, please update your \$NOTES_DIRECTORY" >&2
exit 1
fi
# If no $EDITOR, look for `editor` (symlink on debian/ubuntu/etc)
if [ -z "$EDITOR" ] && type editor &>/dev/null; then
EDITOR=editor
fi
without_notes_dir() {
cat | sed -e "s/^$escaped_notes_dir//g" | sed -E "s/^\/+//g"
}
ls_notes() {
local ls_output=$(ls -p "$notes_dir/$*" 2>&1)
local ls_result=$?
local formatted_output
if [ $# -gt 0 ]; then
local path_prefix=$(printf "${*%/}" | sed -e 's/[]\/$*.^|[]/\\&/g')
formatted_output=$(printf "$ls_output" | sed -E "s/^/$path_prefix\//")
else
formatted_output=$ls_output
fi
if [[ $ls_result == 0 && "$formatted_output" ]]; then
printf "$formatted_output\n"
return 0
else
return 2
fi
}
search_filenames_and_contents() {
if [ "$#" -gt 0 ]; then
find_output=$(find "$notes_dir" -type f -exec bash -c \
"shopt -s nocasematch
grep -il \"$*\" \"{}\" || if [[ \"{}\" =~ \"$*\" ]]; then
echo \"{}\";
fi" \;\
)
else
find_output=$(find "$notes_dir" -type f)
fi
find_result=$?
formatted_output=$(printf "$find_output" | without_notes_dir)
if [[ $find_result == 0 && "$formatted_output" ]]; then
printf "$formatted_output\n"
return 0
else
return 2
fi
}
find_notes() {
local find_output=$(find "$notes_dir" -ipath "$notes_dir/*$**" -type f 2>&1)
local find_result=$?
local formatted_output=$(printf "$find_output" | without_notes_dir)
if [[ $find_result == 0 && "$formatted_output" ]]; then
printf "$formatted_output\n"
return 0
else
return 2
fi
}
grep_notes() {
if [ ! "$#" -gt 0 ]; then
printf "Grep requires a pattern, but none was provided."
return 1
fi
local grep_output=$(grep -r "$notes_dir" -li -e "$*" 2>&1)
local grep_result=$?
local formatted_output=$(printf "$grep_output" | without_notes_dir)
if [[ $grep_result == 0 && "$formatted_output" ]]; then
printf "$formatted_output\n"
return 0
else
return 2
fi
}
generate_name() {
local append_num=0
local format_string="`date +$QUICKNOTE_FORMAT`"
# Initial test has no append
local resolved_name=$format_string
while [[ -e "$notes_dir/$resolved_name.$NOTES_EXT" ]]
do
append_num=$[$append_num+1]
resolved_name=$format_string.$append_num
done
printf $resolved_name
}
new_note() {
local note_name="$*"
if [[ $note_name == "" ]]; then
note_name="$(generate_name)"
fi
if echo "$note_name" | grep "/$" &> /dev/null; then
note_name="${note_name}/$(generate_name)"
fi
mkdir -p "$(dirname "$notes_dir/$note_name")"
open_note "$note_name"
}
remove_note() {
local rm_args=()
if [[ "$1" == "-r" || "$1" == "--recursive" ]]; then
# checks for macos, as it doesn't support long arguments for rm
if [[ "$OSTYPE" == "darwin"* ]]; then
rm_args+=("-r")
else
rm_args+=("--recursive")
fi
shift
fi
if [ ! "$#" -gt 0 ]; then
printf "Remove requires a file or folder, but none was provided."
return 1
fi
local note_name="$*"
local to_remove="$notes_dir/$note_name"
if [ -f "$notes_dir/$note_name.$NOTES_EXT" ]; then
to_remove="$notes_dir/$note_name.$NOTES_EXT"
fi
rm "${rm_args[@]}" "$to_remove"
}
handle_multiple_notes() {
local cmd=$1
if [[ -p /dev/stdin ]]; then
read -d'\n' note_names
while read note_name; do
${cmd}_note "$note_name"
done <<< "$note_names"
else
${cmd}_note "${@:2}"
fi
}
get_full_note_path() {
local note_path=$1
if [[ "$note_path" != *.$NOTES_EXT ]]; then
note_path="$note_path.$NOTES_EXT"
fi
if [ ! -f "$note_path" ]; then
note_path="$notes_dir/$note_path"
fi
echo "$note_path"
}
open_note() {
local note_path=$1
if [[ -z "$note_path" ]]; then
open "$notes_dir"
return
fi
if [ -z "$EDITOR" ]; then
printf "Please set \$EDITOR to edit notes\n"
exit 1
fi
note_path=$( get_full_note_path "$note_path" )
$EDITOR "$note_path" < /dev/tty
}
cat_note() {
local note_path=$1
if [[ -z "$note_path" ]]; then
printf "Cat requires a name, but none was provided."
exit 1
fi
note_path=$( get_full_note_path "$note_path" )
cat "$note_path"
}
usage() {
cat <<EOF
notes is a command line note taking tool.
Usage:
notes new|n <name> # Create a new note
notes ls <pattern> # List notes by path
notes find|f [pattern] # Search notes by filename and path
notes grep|g <pattern> # Search notes by content
notes search|s [pattern] # Search notes by filename or content
notes open|o # Open your notes directory
notes open|o <name> # Open a note for editing by full name
notes rm [-r | --recursive] <name> # Remove note, or folder if -r or --recursive is given
notes cat <name> # Display note
echo <name> | notes open|o # Open all note filenames piped in
echo <name> | notes cat # Display all note filenames piped in
notes --help # Print this usage information
'command|c' means you can use 'command' or the equivalent shorthand alias 'c'
Your notes directory is $notes_dir. You can
override this by setting \$NOTES_DIRECTORY to your preferred path.
EOF
}
main() {
local ret=0
local cmd=""
if [ -z "$1" ]; then
printf "No command specified\n\n"
usage
exit 1
fi
case "$1" in
"new"|"n" )
cmd="new_note"
;;
"ls" )
cmd="ls_notes"
;;
"search"|"s" )
cmd="search_filenames_and_contents"
;;
"find"|"f" )
cmd="find_notes"
;;
"grep"|"g" )
cmd="grep_notes"
;;
"open"|"o" )
cmd="handle_multiple_notes open"
;;
"rm" )
cmd="remove_note"
;;
"cat" )
cmd="handle_multiple_notes cat"
;;
--help | -help | -h )
cmd="usage"
;;
* )
printf "$1 is not a recognized notes command.\n\n"
cmd="usage"
ret=1
;;
esac
shift
$cmd "$@"
ret=$[$ret+$?]
exit $ret
}
main "$@"