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