/
notes
executable file
·442 lines (370 loc) · 11.6 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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
#!/usr/bin/env bash
# Version
notes_version="1.4.0"
# Default Date string before config
QUICKNOTE_FORMAT="quicknote-%Y-%m-%d"
NOTES_EXT="md"
TEMPLATES_DIR=".templates"
# 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 | grep -v "~$")
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 | sort)
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 | sort)
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.\n"
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 | sort)
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 template_path="" # Either empty (no template) or the path to the template we'll use
if [[ "$1" == "-t" ]]; then
shift
local template_name="$1"
if [[ $template_name == "" ]]; then
printf "Error: Template name is missing.\n"
exit 1
fi
shift
local template_folder="$notes_dir/$TEMPLATES_DIR"
if [[ ! -d "$template_folder" ]]; then
printf "Template folder not found: $template_folder\n"
exit 1
fi
if [[ ! "$template_name" == *.$NOTES_EXT ]]; then
template_name="$template_name.$NOTES_EXT"
fi
template_path="$template_folder/$template_name"
if [[ ! -e "$template_path" ]]; then
printf "Template not found\n"
printf "You can create it by running: notes new .templates/$template_name\n"
exit 1
fi
fi
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")"
if [[ "$template_path" != "" ]]; then
local note_path=$(get_full_note_path "$note_name")
cp "$template_path" "$note_path"
fi
open_note "$note_name"
}
remove_note() {
local rm_args=()
if [[ "$1" == "-r" || "$1" == "--recursive" ]]; then
# -r is more portable than recursive, supported in some envs e.g. OSX, Busybox
rm_args+=("-r")
shift
fi
if [ ! "$#" -gt 0 ]; then
printf "Remove requires a file or folder, but none was provided.\n"
return 1
fi
local note_name="$*"
local to_remove="$notes_dir/$note_name"
if [ ! -f "$to_remove" ] && [ -f "$notes_dir/$note_name.$NOTES_EXT" ]; then
# append default extension only if no file exists with exact filename given
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
# first check if file exists
if [ -f "$note_path" ]; then # note path given is good absolute or relative path
note_path="$note_path"
elif [ -f "$notes_dir/$note_path" ]; then # exists in notes_dir
note_path="$notes_dir/$note_path"
elif [ -f "$notes_dir/$note_path.$NOTES_EXT" ]; then # note with this name and default extension exists
note_path="$notes_dir/$note_path.$NOTES_EXT"
elif echo "$note_path" | grep '[.][A-Za-z]\{1,4\}$' &>/dev/null; then # given name has a 1-4 letter extension
note_path="$notes_dir/$note_path"
else
if [[ "$note_path" != *.$NOTES_EXT ]]; then
note_path="$note_path.$NOTES_EXT"
fi
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" )
if bash -c ": >/dev/tty" >/dev/null 2>/dev/null; then
$EDITOR "$note_path" </dev/tty
else
$EDITOR "$note_path"
fi
}
append_note() {
local source_note_path=$( get_full_note_path "$1" )
local to_append="${@:2}"
# If no note name was provided, exit
if [[ -z "$1" ]]; then
printf "Append requires a name, but none was provided.\n"
exit 1
fi
# If note doesn't exist, make sure the directory does
if [[ ! -e "$source_note_path" ]]; then
mkdir -p "$(dirname "$source_note_path")"
fi
# if to_append is empty, check stdin
if [[ -z "$to_append" ]] && [[ -p /dev/stdin ]]; then
to_append=$(cat)
fi
# If to_append is *still* empty, report an error
if [[ -z "$to_append" ]]; then
printf "No text was provided to append\n"
exit 1
fi
echo "$to_append" >> "$source_note_path"
}
move_note() {
local source_note_path=$( get_full_note_path "$1" )
local dest_or_dir_path=$2
if [[ ! -e "$source_note_path" ]]; then
printf "mv requires a source note that exists\n"
exit 1
fi
if [[ -z "$dest_or_dir_path" ]]; then
printf "mv requires a destination name or folder\n"
exit 1
fi
dir_path="$notes_dir/$dest_or_dir_path"
if [[ -d "$dir_path" ]]; then
mv $source_note_path $dir_path
return
fi
local dest_path=$( get_full_note_path "$dest_or_dir_path" )
mkdir -p "$( dirname $dest_path)"
mv $source_note_path $dest_path
}
cat_note() {
local note_path=$1
if [[ -z "$note_path" ]]; then
printf "Cat requires a name, but none was provided.\n"
exit 1
fi
note_path=$( get_full_note_path "$note_path" )
cat "$note_path"
}
realpath() {
local note_path=$1
# If no note name was provided, return notes directory
if [[ -z "$note_path" ]]; then
note_path=$notes_dir
else # otherwise, get full path of note
note_path=$( get_full_note_path "$note_path" )
fi
echo "$note_path"
}
usage() {
local name=$(basename $0)
cat <<EOF
$name is a command line note taking tool.
Usage:
$name new|n [-t <template>] <name> # Create a new note, if -t is given, use template
$name ls <pattern> # List notes by path
$name find|f [pattern] # Search notes by filename and path
$name grep|g <pattern> # Search notes by content
$name search|s [pattern] # Search notes by filename or content
$name open|o # Open your notes directory
$name open|o <name> # Open a note for editing by full name
$name append|a <name> [message] # Appends a note. Will use stdin if no message is given
$name mv <source> <dest>|<directory> # Rename a note, or move a note when a directory is given
$name rm [-r | --recursive] <name> # Remove note, or folder if -r or --recursive is given
$name cat|c <name> # Display note
$name realpath [name] # Display full path of note, or notes directory if no name is given
echo <name> | $name open|o # Open all note filenames piped in
echo <name> | $name cat # Display all note filenames piped in
$name --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
}
version() {
local name=$(basename $0)
cat <<EOF
$name $notes_version
EOF
}
main() {
local ret=0
local cmd=""
# variable to indicate whether it's a modification command
local modified=0
if [ -z "$1" ]; then
printf "No command specified\n\n"
usage
exit 1
fi
case "$1" in
"new"|"n" )
cmd="new_note"
modified=1
;;
"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"
modified=1
;;
"append"|"a" )
cmd="append_note"
modified=1
;;
"mv" )
cmd="move_note"
modified=1
;;
"rm" )
cmd="remove_note"
modified=1
;;
"cat" | "c" )
cmd="handle_multiple_notes cat"
;;
"realpath" )
cmd="realpath"
;;
--help | -help | -h )
cmd="usage"
;;
--version | -version )
cmd="version"
;;
* )
printf "$1 is not a recognized notes command.\n\n"
cmd="usage"
ret=1
;;
esac
shift
$cmd "$@"
ret=$[$ret+$?]
# run POST_COMMAND hook when modification cmd succeeds
if [ $ret -eq 0 ] && [ $modified -eq 1 ] && [ -n "$POST_COMMAND" ]; then
eval "$POST_COMMAND"
fi
exit $ret
}
main "$@"