a4aaf855 |
1 | #! /bin/bash |
2 | |
6c152f7e |
3 | # bash-notes © 2023 by danix is licensed under CC BY-NC 4.0. |
4 | # To view a copy of this license, visit http://creativecommons.org/licenses/by-nc/4.0/ |
5 | |
cb8fcb2f |
6 | # to debug the script run it like: |
7 | # DEBUG=true notes.sh ... |
8 | # and check /tmp/debug_bash-notes.log |
9 | if [[ $DEBUG == true ]]; then |
10 | exec 5> /tmp/debug_bash-notes.log |
11 | BASH_XTRACEFD="5" |
12 | PS4='$LINENO: ' |
13 | set -x |
14 | fi |
a4aaf855 |
15 | |
16 | PID=$$ |
ad818a9d |
17 | BASENAME=$( basename "$0" ) |
18 | NOW=$(date +%s) |
19 | |
20 | VERSION="0.3" |
21 | DBVERSION=${VERSION}_${NOW} |
53f2ed57 |
22 | |
d80ac20a |
23 | set_defaults() { |
24 | # Binaries to use |
cb8fcb2f |
25 | JQ=${JQ:-/usr/bin/jq} |
a4aaf855 |
26 | EDITOR=${EDITOR:-/usr/bin/vim} |
53f2ed57 |
27 | TERMINAL=${TERMINAL:-/usr/bin/alacritty} |
b648c006 |
28 | # add options for your terminal. Remember to add the last option to execute |
29 | # your editor program, otherwise the script will fail. |
30 | # see example in the addnote function |
e3670e83 |
31 | TERM_OPTS="--class notes --title notes -e " |
ad818a9d |
32 | # Setting PAGER here overrides whatever is set in your default shell |
33 | # comment this option to use your default pager if set in your shell. |
34 | PAGER=${PAGER:-/usr/bin/more} |
53f2ed57 |
35 | |
cb8fcb2f |
36 | # set this to true to have output in plain text |
37 | # or use the -p option on the command line before every other option |
38 | PLAIN=false |
d80ac20a |
39 | # base directory for program files |
53f2ed57 |
40 | BASEDIR=${BASEDIR:-~/.local/share/bash-notes} |
d80ac20a |
41 | # notes database in json format |
a4aaf855 |
42 | DB=${BASEDIR}/db.json |
d80ac20a |
43 | # directory containing the actual notes |
a4aaf855 |
44 | NOTESDIR=${BASEDIR}/notes |
53f2ed57 |
45 | |
d80ac20a |
46 | } # end set_defaults, do not change this line. |
47 | |
48 | set_defaults |
49 | |
50 | # Do not edit below this point |
51 | RCFILE=${RCFILE:-~/.config/bash-notes.rc} |
53f2ed57 |
52 | TMPDB=/tmp/db.json |
a4aaf855 |
53 | |
b9f21021 |
54 | if [ ! -x "$JQ" ]; then |
a4aaf855 |
55 | echo "jq not found in your PATH" |
56 | echo "install jq to continue" |
57 | exit 1 |
58 | fi |
59 | |
53f2ed57 |
60 | # IMPORT USER DEFINED OPTIONS IF ANY |
61 | if [[ -f $RCFILE ]]; then |
b9f21021 |
62 | # shellcheck disable=SC1090 |
63 | source "$RCFILE" |
53f2ed57 |
64 | fi |
65 | |
a4aaf855 |
66 | # We prevent the program from running more than one instance: |
b9f21021 |
67 | PIDFILE=/var/tmp/$(basename "$0" .sh).pid |
a4aaf855 |
68 | |
69 | # Make sure the PID file is removed when we kill the process |
70 | trap 'rm -f $PIDFILE; exit 1' TERM INT |
71 | |
72 | if [[ -r $PIDFILE ]]; then |
73 | # PIDFILE exists, so I guess there's already an instance running |
74 | # let's kill it and run again |
b9f21021 |
75 | # shellcheck disable=SC2046,SC2086 |
a4aaf855 |
76 | kill -s 15 $(cat $PIDFILE) > /dev/null 2>&1 |
77 | # should already be deleted by trap, but just to be sure |
b9f21021 |
78 | rm "$PIDFILE" |
a4aaf855 |
79 | fi |
80 | |
81 | # create PIDFILE |
b9f21021 |
82 | echo $PID > "$PIDFILE" |
a4aaf855 |
83 | |
fb711183 |
84 | # Export config to file |
85 | function export_config() { |
86 | if [ -r ${RCFILE} ]; then |
87 | echo "Backing up current '${RCFILE}'...." |
88 | mv -f ${RCFILE} ${RCFILE}.$(date +%Y%m%d_%H%M) |
89 | fi |
90 | echo "Writing '${RCFILE}'...." |
91 | sed -n '/^set_defaults() {/,/^} # end set_defaults, do not change this line./p' $0 \ |
92 | | grep -v set_defaults \ |
93 | | sed -e 's/^\([^=]*\)=\${\1:-\([^}]*\)}/\1=\2/' \ |
94 | > ${RCFILE} |
95 | if [ -r ${RCFILE} ]; then |
96 | echo "Taking no further action." |
97 | exit 0 |
98 | else |
99 | echo "Could not write '${RCFILE}'...!" |
100 | exit 1 |
101 | fi |
102 | } |
103 | |
104 | # we should expand on this function to add a sample note and explain a little bit |
105 | # how the program works. |
106 | function firstrun() { |
107 | [ -f $RCFILE ] && RC=$RCFILE || RC="none" |
108 | |
109 | clear |
110 | echo "${BASENAME} configuration: |
111 | |
112 | base directory: ${BASEDIR}/ |
113 | notes archive: ${NOTESDIR}/ |
114 | notes database: ${DB} |
115 | rc file: $RC |
116 | text editor: ${EDITOR} |
117 | terminal: ${TERMINAL} |
118 | jq executable: ${JQ} |
119 | " |
120 | |
121 | read -r -p "Do you wish to continue? (y/N) " ANSWER |
122 | case $ANSWER in |
123 | y|Y ) |
124 | mkdir -p $NOTESDIR |
125 | cat << __EOL__ > ${DB} |
126 | { |
127 | "params": { |
128 | "version": "${VERSION}", |
129 | "dbversion": "${DBVERSION}" |
130 | }, |
131 | "notes": [] |
132 | } |
133 | __EOL__ |
134 | echo; echo "All done, you can now write your first note." |
135 | ;; |
136 | * ) |
137 | echo "No changes made. Exiting" |
138 | exit |
139 | ;; |
140 | esac |
141 | } |
142 | |
143 | # check for notes dir existance and create it in case it doesn't exists |
144 | if [[ ! -d $NOTESDIR ]]; then |
145 | # we don't have a directory. FIRST RUN? |
146 | firstrun |
147 | fi |
61c91990 |
148 | # check if input is a number, returns false or the number itself |
149 | function check_noteID() { |
150 | IN=$1 |
151 | case $IN in |
152 | ''|*[!0-9]*) |
153 | return 1 |
154 | ;; |
155 | *) |
b9f21021 |
156 | echo "$IN" |
61c91990 |
157 | ;; |
158 | esac |
159 | } |
160 | |
a4aaf855 |
161 | function helptext() { |
d80ac20a |
162 | echo "Usage:" |
c018122c |
163 | echo " $0 [PARAMS] ..." |
d80ac20a |
164 | echo "" |
165 | cat << __NOWCONF__ |
166 | ${BASENAME} configuration is: |
167 | |
168 | base directory: ${BASEDIR}/ |
169 | notes archive: ${NOTESDIR}/ |
170 | notes database: ${DB} |
171 | rc file: $RCFILE |
cb8fcb2f |
172 | debug file: /tmp/debug_bash-note.log |
c018122c |
173 | |
d80ac20a |
174 | text editor: ${EDITOR} |
175 | terminal: ${TERMINAL} |
176 | jq executable: ${JQ} |
177 | __NOWCONF__ |
178 | |
179 | echo "" |
e3670e83 |
180 | echo "${BASENAME} parameters are:" |
d80ac20a |
181 | echo " -h | --help : This help text" |
c018122c |
182 | echo " -p | --plain : Output is in plain text" |
b648c006 |
183 | echo " (without this option the output is formatted)" |
184 | echo " (this option must precede all others)" |
d80ac20a |
185 | echo " -l | --list : List existing notes" |
026502da |
186 | echo " -a | --add [\"<title>\"] : Add new note" |
6c152f7e |
187 | echo " -e | --edit [<note>] : Edit note" |
026502da |
188 | echo " -d | --delete [<note> | all] : Delete single note or all notes at once" |
ad818a9d |
189 | echo " -s | --show [<note>] : Display note using your favourite PAGER" |
d80ac20a |
190 | echo " -v | --version : Print version" |
191 | echo " --userconf : Export User config file" |
e3670e83 |
192 | echo "" |
a4aaf855 |
193 | } |
a4aaf855 |
194 | function addnote() { |
026502da |
195 | # remove eventually existing temp DB file |
196 | if [[ -f $TMPDB ]]; then |
197 | rm $TMPDB |
198 | fi |
199 | |
53f2ed57 |
200 | NOTETITLE="$1" |
201 | echo "adding new note - \"$NOTETITLE\"" |
b9f21021 |
202 | # shellcheck disable=SC2086 |
e3670e83 |
203 | LASTID=$($JQ '.notes[-1].id // 0 | tonumber' $DB) |
204 | # [ "" == $LASTID ] && LASTID=0 |
b9f21021 |
205 | NOTEID=$(( LASTID + 1 )) |
206 | # shellcheck disable=SC2086 |
a4aaf855 |
207 | touch ${NOTESDIR}/${NOW} |
b9f21021 |
208 | # shellcheck disable=SC2016 |
a4aaf855 |
209 | $JQ --arg i "$NOTEID" --arg t "$NOTETITLE" --arg f "$NOW" '.notes += [{"id": $i, "title": $t, "file": $f}]' "$DB" > $TMPDB |
b9f21021 |
210 | # shellcheck disable=SC2086 |
a4aaf855 |
211 | mv $TMPDB $DB |
b648c006 |
212 | # example for alacritty: |
213 | # alacritty --class notes --title notes -e /usr/bin/vim ... |
b9f21021 |
214 | # shellcheck disable=SC2086,SC2091 |
e3670e83 |
215 | $(${TERMINAL} ${TERM_OPTS} ${EDITOR} ${NOTESDIR}/${NOW}) |
a4aaf855 |
216 | } |
a4aaf855 |
217 | function editnote() { |
61c91990 |
218 | NOTE=$1 |
b9f21021 |
219 | # shellcheck disable=SC2155 |
220 | local OK=$(check_noteID "$NOTE") |
221 | if [ ! "$OK" ]; then |
61c91990 |
222 | echo "invalid note \"$NOTE\"" |
b9f21021 |
223 | echo "Use the note ID that you can fetch after listing your notes" |
61c91990 |
224 | exit 1 |
225 | fi |
226 | |
b9f21021 |
227 | # shellcheck disable=SC2016,SC2086 |
61c91990 |
228 | TITLE=$($JQ --arg i $OK '.notes[] | select(.id == $i) | .title' $DB) |
b9f21021 |
229 | # shellcheck disable=SC2016,SC2086 |
61c91990 |
230 | FILE=$($JQ -r --arg i $OK '.notes[] | select(.id == $i) | .file' $DB) |
44abbfe7 |
231 | if [ "$TITLE" ]; then |
232 | echo "editing note $TITLE" |
b9f21021 |
233 | # shellcheck disable=SC2086,SC2091 |
b648c006 |
234 | $(${TERMINAL} ${TERM_OPTS} ${EDITOR} ${NOTESDIR}/${FILE}) |
44abbfe7 |
235 | else |
236 | echo "note not found" |
237 | exit 1 |
238 | fi |
07d42c7a |
239 | exit |
a4aaf855 |
240 | } |
fb711183 |
241 | function listnotes() { |
242 | # [ $PLAIN == true ] && echo "output is plain text" || echo "output is colored" |
243 | if [[ $(ls -A "$NOTESDIR") ]]; then |
244 | if [ $PLAIN == false ]; then |
245 | echo "listing all notes" |
246 | echo "" |
247 | fi |
248 | [ $PLAIN == false ] && echo "[ID] [TITLE] [CREATED]" |
249 | for i in "${NOTESDIR}"/*; do |
250 | # shellcheck disable=SC2155 |
251 | local fname=$(basename $i) |
252 | DATE=$(date -d @${fname} +"%d/%m/%Y %R %z%Z") |
253 | # shellcheck disable=SC2016,SC2086 |
254 | TITLE=$($JQ -r --arg z $(basename $i) '.notes[] | select(.file == $z) | .title' $DB) |
255 | # shellcheck disable=SC2016,SC2086 |
256 | ID=$($JQ -r --arg z $(basename $i) '.notes[] | select(.file == $z) | .id' $DB) |
257 | [ $PLAIN == false ] && echo "[${ID}] ${TITLE} ${DATE}" || echo "${ID} - ${TITLE} - ${DATE}" |
258 | done |
259 | else |
260 | echo "no notes yet. You can add your first one with: ${BASENAME} -a \"your note title\"" |
261 | fi |
262 | } |
a4aaf855 |
263 | function rmnote() { |
026502da |
264 | # remove eventually existing temp DB file |
265 | if [[ -f $TMPDB ]]; then |
266 | rm $TMPDB |
b648c006 |
267 | fi |
268 | |
026502da |
269 | NOTE=$1 |
b9f21021 |
270 | if [ "all" == "$NOTE" ]; then |
026502da |
271 | echo "You're going to delete all notes." |
272 | read -r -p "Do you wish to continue? (y/N) " ANSWER |
273 | case $ANSWER in |
274 | y|Y ) |
b9f21021 |
275 | # shellcheck disable=SC2086 |
026502da |
276 | $JQ 'del(.notes[])' $DB > $TMPDB |
b9f21021 |
277 | # shellcheck disable=SC2086 |
026502da |
278 | mv $TMPDB $DB |
b9f21021 |
279 | # shellcheck disable=SC2086 |
026502da |
280 | rm $NOTESDIR/* |
281 | echo "Deleted all notes" |
282 | ;; |
283 | * ) |
284 | echo "Aborting, no notes were deleted." |
285 | exit 1 |
286 | ;; |
287 | esac |
b648c006 |
288 | else |
b9f21021 |
289 | # shellcheck disable=SC2155 |
290 | local OK=$(check_noteID "$NOTE") |
291 | if [ ! "$OK" ]; then |
026502da |
292 | echo "invalid note \"$NOTE\"" |
b9f21021 |
293 | echo "Use the note ID that you can fetch after listing your notes" |
026502da |
294 | exit 1 |
295 | fi |
296 | |
b9f21021 |
297 | # shellcheck disable=SC2016,SC2086 |
026502da |
298 | TITLE=$($JQ --arg i $OK '.notes[] | select(.id == $i) | .title' $DB) |
b9f21021 |
299 | # shellcheck disable=SC2016,SC2086 |
026502da |
300 | FILE=$($JQ -r --arg i $OK '.notes[] | select(.id == $i) | .file' $DB) |
301 | if [ "$TITLE" ]; then |
ad818a9d |
302 | # shellcheck disable=SC2016,SC2086 |
303 | $JQ -r --arg i $OK 'del(.notes[] | select(.id == $i))' $DB > $TMPDB |
304 | # shellcheck disable=SC2086 |
305 | mv $TMPDB $DB |
306 | rm $NOTESDIR/$FILE |
307 | echo "Deleted note $TITLE" |
026502da |
308 | else |
309 | echo "note not found" |
310 | exit 1 |
311 | fi |
b648c006 |
312 | fi |
a4aaf855 |
313 | } |
ad818a9d |
314 | function shownote() { |
315 | NOTE=$1 |
316 | |
317 | # shellcheck disable=SC2155 |
318 | local OK=$(check_noteID "$NOTE") |
319 | if [ ! "$OK" ]; then |
320 | echo "invalid note \"$NOTE\"" |
321 | echo "Use the note ID that you can fetch after listing your notes" |
322 | exit 1 |
323 | fi |
324 | |
325 | FILE=$($JQ -r --arg i $OK '.notes[] | select(.id == $i) | .file' $DB) |
326 | |
327 | if [ "$FILE" ]; then |
328 | $PAGER ${NOTESDIR}/${FILE} |
329 | fi |
330 | } |
b9f21021 |
331 | # shellcheck disable=SC2006 |
ad818a9d |
332 | GOPT=$(getopt -o hvpla::e::d::s:: --long help,version,list,plain,userconf,backup::,add::,edit::,delete::,show:: -n 'bash-notes' -- "$@") |
53f2ed57 |
333 | |
b9f21021 |
334 | # shellcheck disable=SC2181 |
cb8fcb2f |
335 | if [ $? != 0 ] ; then helptext >&2 ; exit 1 ; fi |
53f2ed57 |
336 | |
337 | # Note the quotes around `$GOPT': they are essential! |
338 | eval set -- "$GOPT" |
ad818a9d |
339 | unset GOPT |
53f2ed57 |
340 | |
341 | while true; do |
342 | case "$1" in |
343 | -h | --help ) |
344 | helptext |
345 | exit |
346 | ;; |
347 | -v | --version ) |
348 | echo $BASENAME v${VERSION} |
349 | exit |
350 | ;; |
c018122c |
351 | -p | --plain ) |
352 | PLAIN=true |
353 | shift |
354 | ;; |
53f2ed57 |
355 | -l | --list ) |
356 | listnotes |
357 | exit |
358 | ;; |
359 | -a | --add ) |
ad818a9d |
360 | case "$2" in |
361 | '' ) |
362 | read -r -p "Title: " TITLE |
363 | ;; |
364 | * ) |
365 | TITLE=$2 |
366 | ;; |
367 | esac |
53f2ed57 |
368 | shift 2 |
369 | addnote "$TITLE" |
07d42c7a |
370 | exit |
53f2ed57 |
371 | ;; |
6c152f7e |
372 | -e | --edit ) |
ad818a9d |
373 | case "$2" in |
374 | '' ) |
375 | read -r -p "Note ID: " NOTE |
376 | ;; |
377 | * ) |
378 | NOTE=$2 |
379 | ;; |
380 | esac |
53f2ed57 |
381 | shift 2 |
382 | editnote "$NOTE" |
07d42c7a |
383 | exit |
53f2ed57 |
384 | ;; |
b648c006 |
385 | -d | --delete ) |
ad818a9d |
386 | case "$2" in |
387 | '' ) |
388 | read -r -p "Note ID: " NOTE |
389 | ;; |
390 | * ) |
391 | NOTE=$2 |
392 | ;; |
393 | esac |
53f2ed57 |
394 | shift 2 |
395 | rmnote "$NOTE" |
07d42c7a |
396 | exit |
53f2ed57 |
397 | ;; |
ad818a9d |
398 | -s | --show ) |
399 | case "$2" in |
400 | '' ) |
401 | read -r -p "Note ID: " NOTE |
402 | ;; |
403 | * ) |
404 | NOTE=$2 |
405 | ;; |
406 | esac |
407 | shift 2 |
408 | shownote "$NOTE" |
07d42c7a |
409 | exit |
ad818a9d |
410 | ;; |
d80ac20a |
411 | --userconf ) |
412 | export_config |
b9f21021 |
413 | # shellcheck disable=SC2317 |
d80ac20a |
414 | echo "config exported to \"$RCFILE\"" |
b9f21021 |
415 | # shellcheck disable=SC2317 |
d80ac20a |
416 | exit |
53f2ed57 |
417 | ;; |
418 | -- ) |
026502da |
419 | shift |
420 | break |
53f2ed57 |
421 | ;; |
422 | * ) |
423 | break |
424 | ;; |
425 | esac |
a4aaf855 |
426 | done |