Separated script into multiple files for easier management. Added Makefile
[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
fb711183 84# Export config to file
85function 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.
106function firstrun() {
107 [ -f $RCFILE ] && RC=$RCFILE || RC="none"
108
109 clear
110 echo "${BASENAME} configuration:
111
112base directory: ${BASEDIR}/
113notes archive: ${NOTESDIR}/
114notes database: ${DB}
115rc file: $RC
116text editor: ${EDITOR}
117terminal: ${TERMINAL}
118jq 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
144if [[ ! -d $NOTESDIR ]]; then
145 # we don't have a directory. FIRST RUN?
146 firstrun
147fi
61c91990 148# check if input is a number, returns false or the number itself
149function 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 161function helptext() {
d80ac20a 162 echo "Usage:"
c018122c 163 echo " $0 [PARAMS] ..."
d80ac20a 164 echo ""
165 cat << __NOWCONF__
166${BASENAME} configuration is:
167
168base directory: ${BASEDIR}/
169notes archive: ${NOTESDIR}/
170notes database: ${DB}
171rc file: $RCFILE
cb8fcb2f 172debug file: /tmp/debug_bash-note.log
c018122c 173
d80ac20a 174text editor: ${EDITOR}
175terminal: ${TERMINAL}
176jq 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 194function 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 217function 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
a4aaf855 239}
fb711183 240function listnotes() {
241 # [ $PLAIN == true ] && echo "output is plain text" || echo "output is colored"
242 if [[ $(ls -A "$NOTESDIR") ]]; then
243 if [ $PLAIN == false ]; then
244 echo "listing all notes"
245 echo ""
246 fi
247 [ $PLAIN == false ] && echo "[ID] [TITLE] [CREATED]"
248 for i in "${NOTESDIR}"/*; do
249 # shellcheck disable=SC2155
250 local fname=$(basename $i)
251 DATE=$(date -d @${fname} +"%d/%m/%Y %R %z%Z")
252 # shellcheck disable=SC2016,SC2086
253 TITLE=$($JQ -r --arg z $(basename $i) '.notes[] | select(.file == $z) | .title' $DB)
254 # shellcheck disable=SC2016,SC2086
255 ID=$($JQ -r --arg z $(basename $i) '.notes[] | select(.file == $z) | .id' $DB)
256 [ $PLAIN == false ] && echo "[${ID}] ${TITLE} ${DATE}" || echo "${ID} - ${TITLE} - ${DATE}"
257 done
258 else
259 echo "no notes yet. You can add your first one with: ${BASENAME} -a \"your note title\""
260 fi
261}
a4aaf855 262function rmnote() {
026502da 263 # remove eventually existing temp DB file
264 if [[ -f $TMPDB ]]; then
265 rm $TMPDB
b648c006 266 fi
267
026502da 268 NOTE=$1
b9f21021 269 if [ "all" == "$NOTE" ]; then
026502da 270 echo "You're going to delete all notes."
271 read -r -p "Do you wish to continue? (y/N) " ANSWER
272 case $ANSWER in
273 y|Y )
b9f21021 274 # shellcheck disable=SC2086
026502da 275 $JQ 'del(.notes[])' $DB > $TMPDB
b9f21021 276 # shellcheck disable=SC2086
026502da 277 mv $TMPDB $DB
b9f21021 278 # shellcheck disable=SC2086
026502da 279 rm $NOTESDIR/*
280 echo "Deleted all notes"
281 ;;
282 * )
283 echo "Aborting, no notes were deleted."
284 exit 1
285 ;;
286 esac
b648c006 287 else
b9f21021 288 # shellcheck disable=SC2155
289 local OK=$(check_noteID "$NOTE")
290 if [ ! "$OK" ]; then
026502da 291 echo "invalid note \"$NOTE\""
b9f21021 292 echo "Use the note ID that you can fetch after listing your notes"
026502da 293 exit 1
294 fi
295
b9f21021 296 # shellcheck disable=SC2016,SC2086
026502da 297 TITLE=$($JQ --arg i $OK '.notes[] | select(.id == $i) | .title' $DB)
b9f21021 298 # shellcheck disable=SC2016,SC2086
026502da 299 FILE=$($JQ -r --arg i $OK '.notes[] | select(.id == $i) | .file' $DB)
300 if [ "$TITLE" ]; then
ad818a9d 301 # shellcheck disable=SC2016,SC2086
302 $JQ -r --arg i $OK 'del(.notes[] | select(.id == $i))' $DB > $TMPDB
303 # shellcheck disable=SC2086
304 mv $TMPDB $DB
305 rm $NOTESDIR/$FILE
306 echo "Deleted note $TITLE"
026502da 307 else
308 echo "note not found"
309 exit 1
310 fi
b648c006 311 fi
a4aaf855 312}
ad818a9d 313function shownote() {
314 NOTE=$1
315
316 # shellcheck disable=SC2155
317 local OK=$(check_noteID "$NOTE")
318 if [ ! "$OK" ]; then
319 echo "invalid note \"$NOTE\""
320 echo "Use the note ID that you can fetch after listing your notes"
321 exit 1
322 fi
323
324 FILE=$($JQ -r --arg i $OK '.notes[] | select(.id == $i) | .file' $DB)
325
326 if [ "$FILE" ]; then
327 $PAGER ${NOTESDIR}/${FILE}
328 fi
329}
b9f21021 330# shellcheck disable=SC2006
ad818a9d 331GOPT=$(getopt -o hvpla::e::d::s:: --long help,version,list,plain,userconf,backup::,add::,edit::,delete::,show:: -n 'bash-notes' -- "$@")
53f2ed57 332
b9f21021 333# shellcheck disable=SC2181
cb8fcb2f 334if [ $? != 0 ] ; then helptext >&2 ; exit 1 ; fi
53f2ed57 335
336# Note the quotes around `$GOPT': they are essential!
337eval set -- "$GOPT"
ad818a9d 338unset GOPT
53f2ed57 339
340while true; do
341 case "$1" in
342 -h | --help )
343 helptext
344 exit
345 ;;
346 -v | --version )
347 echo $BASENAME v${VERSION}
348 exit
349 ;;
c018122c 350 -p | --plain )
351 PLAIN=true
352 shift
353 ;;
53f2ed57 354 -l | --list )
355 listnotes
356 exit
357 ;;
358 -a | --add )
ad818a9d 359 case "$2" in
360 '' )
361 read -r -p "Title: " TITLE
362 ;;
363 * )
364 TITLE=$2
365 ;;
366 esac
53f2ed57 367 shift 2
368 addnote "$TITLE"
369 ;;
6c152f7e 370 -e | --edit )
ad818a9d 371 case "$2" in
372 '' )
373 read -r -p "Note ID: " NOTE
374 ;;
375 * )
376 NOTE=$2
377 ;;
378 esac
53f2ed57 379 shift 2
380 editnote "$NOTE"
381 ;;
b648c006 382 -d | --delete )
ad818a9d 383 case "$2" in
384 '' )
385 read -r -p "Note ID: " NOTE
386 ;;
387 * )
388 NOTE=$2
389 ;;
390 esac
53f2ed57 391 shift 2
392 rmnote "$NOTE"
393 ;;
ad818a9d 394 -s | --show )
395 case "$2" in
396 '' )
397 read -r -p "Note ID: " NOTE
398 ;;
399 * )
400 NOTE=$2
401 ;;
402 esac
403 shift 2
404 shownote "$NOTE"
405 ;;
d80ac20a 406 --userconf )
407 export_config
b9f21021 408 # shellcheck disable=SC2317
d80ac20a 409 echo "config exported to \"$RCFILE\""
b9f21021 410 # shellcheck disable=SC2317
d80ac20a 411 exit
53f2ed57 412 ;;
413 -- )
026502da 414 shift
415 break
53f2ed57 416 ;;
417 * )
418 break
419 ;;
420 esac
a4aaf855 421done
422
ad818a9d 423if [ -z $1 ]; then
424 helptext
425fi