1a4b56683299015168a2f6edafb955c714c28ad5
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/
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
17 BASENAME
=$
( basename "$0" )
21 DBVERSION
=${VERSION}_
${NOW}
26 EDITOR
=${EDITOR:-/usr/bin/vim}
27 TERMINAL
=${TERMINAL:-/usr/bin/alacritty}
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
31 TERM_OPTS
="--class notes --title notes -e "
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}
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
39 # base directory for program files
40 BASEDIR
=${BASEDIR:-~/.local/share/bash-notes}
41 # notes database in json format
43 # directory containing the actual notes
44 NOTESDIR
=${BASEDIR}/notes
46 } # end set_defaults, do not change this line.
50 # Do not edit below this point
51 RCFILE
=${RCFILE:-~/.config/bash-notes.rc}
54 if [ ! -x "$JQ" ]; then
55 echo "jq not found in your PATH"
56 echo "install jq to continue"
60 # IMPORT USER DEFINED OPTIONS IF ANY
61 if [[ -f $RCFILE ]]; then
62 # shellcheck disable=SC1090
66 # We prevent the program from running more than one instance:
67 PIDFILE
=/var
/tmp
/$
(basename "$0" .sh
).pid
69 # Make sure the PID file is removed when we kill the process
70 trap 'rm -f $PIDFILE; exit 1' TERM INT
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
75 # shellcheck disable=SC2046,SC2086
76 kill -s 15 $
(cat $PIDFILE) > /dev
/null
2>&1
77 # should already be deleted by trap, but just to be sure
82 echo $PID > "$PIDFILE"
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
)
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/' \
95 if [ -r ${RCFILE} ]; then
96 echo "Taking no further action."
99 echo "Could not write '${RCFILE}'...!"
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"
110 echo "${BASENAME} configuration:
112 base directory: ${BASEDIR}/
113 notes archive: ${NOTESDIR}/
114 notes database: ${DB}
116 text editor: ${EDITOR}
117 terminal: ${TERMINAL}
121 echo "Now I'll create the needed files and directories."
122 read -r -p "Do you wish to continue? (y/N) " ANSWER
126 cat << __EOL__ > ${DB}
129 "version": "${VERSION}",
130 "dbversion": "${DBVERSION}"
135 echo; echo "All done, you can now write your first note."
138 echo "No changes made. Exiting"
144 # check for notes dir existance and create it in case it doesn't exists
145 if [[ ! -d $NOTESDIR ]]; then
146 # we don't have a directory. FIRST RUN?
149 # check if input is a number, returns false or the number itself
150 function check_noteID
() {
162 function helptext
() {
164 echo " $0 [PARAMS] [note ID]..."
166 echo "${BASENAME} parameters are:"
167 echo -e " -h | --help\t\t\t: This help text"
168 echo -e " -p | --plain\t\t\t: Output is in plain text"
169 echo -e "\t\t\t\t (without this option the output is formatted)"
170 echo -e "\t\t\t\t (this option must precede all others)"
171 echo -e " -l | --list\t\t\t: List existing notes"
172 echo -e " -a | --add=[\"<title>\"]\t: Add new note"
173 echo -e " -e | --edit=[<note>]\t\t: Edit note"
174 echo -e " -d | --delete=[<note> | all] : Delete single note or all notes at once"
175 echo -e " -s | --show=[<note>]\t\t: Display note using your favourite PAGER"
176 echo -e " -r | --restore=[<dir>]\t: Restore a previous backup from dir"
177 echo -e " -v | --version\t\t: Print version"
178 echo -e " --userconf\t\t\t: Export User config file"
179 echo -e " --backup [<dest>]\t\t: Backup your data in your destination folder"
181 echo -e "if a non option is passed and is a valid note ID, the note will be displayed."
184 function configtext
() {
186 ${BASENAME} configuration is:
188 base directory: ${BASEDIR}/
189 notes archive: ${NOTESDIR}/
190 notes database: ${DB}
192 debug file: /tmp/debug_bash-note.log
194 text editor: ${EDITOR}
195 terminal: ${TERMINAL}
202 # this function returns a random 2 words title
203 function random_title
() {
206 DICT
=/usr
/share
/dict
/words
209 # total number of non-random words available
210 COUNT
=$
(cat $DICT |
wc -l)
212 # while loop to generate random words
215 RAND
=$
(od -N3 -An -i /dev
/urandom |
awk -v f
=0 -v r
="$COUNT" '{printf "%i\n", f + r * $1 / 16777216}')
216 OUTPUT
+="$(sed `echo $RAND`"q
;d
" $DICT)"
218 [[ $X -eq 1 ]] && OUTPUT
+=" "
225 # remove eventually existing temp DB file
226 if [[ -f $TMPDB ]]; then
230 RTITLE
=$
(random_title
)
231 [[ -z "$1" ]] && NOTETITLE
="$RTITLE" || NOTETITLE
="$1"
232 echo "adding new note - \"$NOTETITLE\""
233 # shellcheck disable=SC2086
234 LASTID
=$
($JQ '.notes[-1].id // 0 | tonumber' $DB)
235 # [ "" == $LASTID ] && LASTID=0
236 NOTEID
=$
(( LASTID
+ 1 ))
237 # shellcheck disable=SC2086
238 touch ${NOTESDIR}/${NOW}
239 # shellcheck disable=SC2016
240 $JQ --arg i
"$NOTEID" --arg t
"$NOTETITLE" --arg f
"$NOW" '.notes += [{"id": $i, "title": $t, "file": $f}]' "$DB" > $TMPDB
241 # shellcheck disable=SC2086
243 # example for alacritty:
244 # alacritty --class notes --title notes -e /usr/bin/vim ...
245 # shellcheck disable=SC2086,SC2091
246 $
(${TERMINAL} ${TERM_OPTS} ${EDITOR} ${NOTESDIR}/${NOW})
248 function backup_data
() {
250 echo "backing up data in $BACKUPDIR"
253 if [ -d $BACKUPDIR ]; then
254 if [ $
(/bin
/ls -A $BACKUPDIR) ]; then
255 echo "$BACKUPDIR is not empty. Cannot continue"
258 echo "$BACKUPDIR is ok. Continuing!"
261 # BACKUPDIR doesn't exists
262 echo "$BACKUPDIR doesn't exists"
263 read -r -p "Do you want me to create it for you? (y/N) " ANSWER
269 echo "No changes made. Exiting"
274 # ok, we have a backup directory
275 if [ -r $RCFILE ]; then
276 BCKUP_COMM
=$
(rsync
-avz --progress ${RCFILE}* ${BASEDIR}/* ${BACKUPDIR})
278 BCKUP_COMM
=$
(rsync
-avz --progress ${BASEDIR}/* ${BACKUPDIR})
281 if [ "$BCKUP_COMM" ]; then
282 echo -e "All files backed up."
283 echo -e "BACKUP directory:\t$BACKUPDIR"
284 tree
$BACKUPDIR |
$PAGER
285 echo; echo "BACKUP COMPLETED"
289 function backup_restore
() {
291 echo "restoring backup from $BACKUPDIR"
292 echo "This will overwrite all your notes and configurations with the backup."
293 read -r -p "Do you want to continue? (y/N) " ANSWER
297 BACKUPRC
=$
(basename $RCFILE)
298 if [ -r ${BACKUPDIR}/${BACKUPRC} ]; then
299 if [ -r ${RCFILE} ]; then
300 echo "Backing up current '${RCFILE}'...."
301 mv -f ${RCFILE} ${RCFILE}.$
(date +%Y
%m
%d_
%H
%M
)
303 cp --verbose ${BACKUPDIR}/${BACKUPRC} $RCFILE
305 # restoring notes directory
306 if [ -d $BACKUPDIR/notes
]; then
307 if [ $
(/bin
/ls -A $NOTESDIR) ]; then
308 rm --verbose $NOTESDIR/*
310 cp -r --verbose $BACKUPDIR/notes
$BASEDIR
313 BACKUPDB
=$
(basename $DB)
314 if [ -f ${BACKUPDIR}/${BACKUPDB} ]; then
315 if [ -r ${DB} ]; then
316 echo "Backing up current '${DB}'...."
317 mv -f ${DB} ${DB}.$
(date +%Y
%m
%d_
%H
%M
)
319 cp --verbose ${BACKUPDIR}/${BACKUPDB} $DB
323 echo "No changes made. Exiting"
329 function editnote
() {
331 # shellcheck disable=SC2155
332 local OK
=$
(check_noteID
"$NOTE")
334 echo "invalid note \"$NOTE\""
335 echo "Use the note ID that you can fetch after listing your notes"
339 # shellcheck disable=SC2016,SC2086
340 TITLE
=$
($JQ --arg i
$OK '.notes[] | select(.id == $i) | .title' $DB)
341 # shellcheck disable=SC2016,SC2086
342 FILE
=$
($JQ -r --arg i
$OK '.notes[] | select(.id == $i) | .file' $DB)
343 if [ "$TITLE" ]; then
344 echo "editing note $TITLE"
345 # shellcheck disable=SC2086,SC2091
346 $
(${TERMINAL} ${TERM_OPTS} ${EDITOR} ${NOTESDIR}/${FILE})
348 echo "note not found"
352 function listnotes
() {
353 # [ $PLAIN == true ] && echo "output is plain text" || echo "output is colored"
354 if [[ $
(ls -A "$NOTESDIR") ]]; then
355 if [ $PLAIN == false
]; then
356 echo "listing all notes"
359 [ $PLAIN == false
] && echo "[ID] [TITLE] [CREATED]"
360 for i
in "${NOTESDIR}"/*; do
361 # shellcheck disable=SC2155
362 local fname
=$
(basename $i)
363 DATE
=$
(date -d @
${fname} +"%d/%m/%Y %R %z%Z")
364 # shellcheck disable=SC2016,SC2086
365 TITLE
=$
($JQ -r --arg z $
(basename $i) '.notes[] | select(.file == $z) | .title' $DB)
366 # shellcheck disable=SC2016,SC2086
367 ID
=$
($JQ -r --arg z $
(basename $i) '.notes[] | select(.file == $z) | .id' $DB)
368 [ $PLAIN == false
] && echo "[${ID}] ${TITLE} ${DATE}" || echo "${ID} - ${TITLE} - ${DATE}"
371 echo "no notes yet. You can add your first one with: ${BASENAME} -a \"your note title\""
375 # remove eventually existing temp DB file
376 if [[ -f $TMPDB ]]; then
381 if [ "all" == "$NOTE" ]; then
382 echo "You're going to delete all notes."
383 read -r -p "Do you wish to continue? (y/N) " ANSWER
386 # shellcheck disable=SC2086
387 $JQ 'del(.notes[])' $DB > $TMPDB
388 # shellcheck disable=SC2086
390 # shellcheck disable=SC2086
392 echo "Deleted all notes"
395 echo "Aborting, no notes were deleted."
400 # shellcheck disable=SC2155
401 local OK
=$
(check_noteID
"$NOTE")
403 echo "invalid note \"$NOTE\""
404 echo "Use the note ID that you can fetch after listing your notes"
409 # shellcheck disable=SC2016,SC2086
410 TITLE
=$
($JQ --arg i
$OK '.notes[] | select(.id == $i) | .title' $DB)
411 # shellcheck disable=SC2016,SC2086
412 FILE
=$
($JQ -r --arg i
$OK '.notes[] | select(.id == $i) | .file' $DB)
413 if [ "$TITLE" ]; then
414 # shellcheck disable=SC2016,SC2086
415 $JQ -r --arg i
$OK 'del(.notes[] | select(.id == $i))' $DB > $TMPDB
416 # shellcheck disable=SC2086
419 echo "Deleted note $TITLE"
423 echo "note not found"
429 function shownote
() {
432 # shellcheck disable=SC2155
433 local OK
=$
(check_noteID
"$NOTE")
435 echo "invalid note \"$NOTE\""
436 echo "Use the note ID that you can fetch after listing your notes"
440 FILE
=$
($JQ -r --arg i
$OK '.notes[] | select(.id == $i) | .file' $DB)
443 $PAGER ${NOTESDIR}/${FILE}
446 # shellcheck disable=SC2006
447 GOPT
=$
(getopt
-o hvplr
::a
::e
::d
::s
:: --long help,version
,list
,plain
,userconf
,restore
::,backup
::,add
::,edit
::,delete
::,show
:: -n 'bash-notes' -- "$@")
449 # shellcheck disable=SC2181
450 if [ $?
!= 0 ] ; then helptext
>&2 ; exit 1 ; fi
452 # Note the quotes around `$GOPT': they are essential!
463 echo $BASENAME v
${VERSION}
477 read -r -p "Title: " TITLE
490 read -r -p "Note ID: " NOTE
503 read -r -p "Note ID: " NOTE
516 read -r -p "Note ID: " NOTE
529 read -r -p "Backup Dir: " RDIR
541 # shellcheck disable=SC2317
542 echo "config exported to \"$RCFILE\""
543 # shellcheck disable=SC2317
549 read -r -p "Backup Dir: " BDIR
570 if [ $
(check_noteID
$arg) ]; then