cbb34eb5bd987b686e2810f6b84b91ee066a6238
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 # Git binary only used if $USEGIT is true - See below
29 GIT
=${GIT:-/usr/bin/git}
30 # add options for your terminal. Remember to add the last option to execute
31 # your editor program, otherwise the script will fail.
32 # see example in the addnote function
33 TERM_OPTS
="--class notes --title notes -e "
34 # Setting PAGER here overrides whatever is set in your default shell
35 # comment this option to use your default pager if set in your shell.
36 PAGER
=${PAGER:-/usr/bin/more}
38 # set this to true to have output in plain text
39 # or use the -p option on the command line before every other option
41 # base directory for program files
42 BASEDIR
=${BASEDIR:-~/.local/share/bash-notes}
43 # notes database in json format
45 # directory containing the actual notes
46 NOTESDIR
=${BASEDIR}/notes
50 # If you want to store your notes in a git repository set this to true
52 # Address of your remote repository
53 GITREMOTE
=${GITREMOTE:-""}
55 } # end set_defaults, do not change this line.
59 # Do not edit below this point
60 RCFILE
=${RCFILE:-~/.config/bash-notes.rc}
63 if [ ! -x "$JQ" ]; then
64 echo "jq not found in your PATH"
65 echo "install jq to continue"
69 # IMPORT USER DEFINED OPTIONS IF ANY
70 if [[ -f $RCFILE ]]; then
71 # shellcheck disable=SC1090
75 # We prevent the program from running more than one instance:
76 PIDFILE
=/var
/tmp
/$
(basename "$0" .sh
).pid
78 # Make sure the PID file is removed when we kill the process
79 trap 'rm -f $PIDFILE; exit 1' TERM INT
81 if [[ -r $PIDFILE ]]; then
82 # PIDFILE exists, so I guess there's already an instance running
83 # let's kill it and run again
84 # shellcheck disable=SC2046,SC2086
85 kill -s 15 $
(cat $PIDFILE) > /dev
/null
2>&1
86 # should already be deleted by trap, but just to be sure
91 echo $PID > "$PIDFILE"
93 # Export config to file
94 function export_config
() {
95 if [ -r ${RCFILE} ]; then
96 echo "Backing up current '${RCFILE}'...."
97 mv -f ${RCFILE} ${RCFILE}.$
(date +%Y
%m
%d_
%H
%M
)
99 echo "Writing '${RCFILE}'...."
100 sed -n '/^set_defaults() {/,/^} # end set_defaults, do not change this line./p' $0 \
101 |
grep -v set_defaults \
102 |
sed -e 's/^\([^=]*\)=\${\1:-\([^}]*\)}/\1=\2/' \
104 if [ -r ${RCFILE} ]; then
105 echo "Taking no further action."
108 echo "Could not write '${RCFILE}'...!"
113 # we should expand on this function to add a sample note and explain a little bit
114 # how the program works.
115 function firstrun
() {
116 [ -f $RCFILE ] && RC
=$RCFILE || RC
="none"
119 echo "${BASENAME} configuration:
121 base directory: ${BASEDIR}/
122 notes archive: ${NOTESDIR}/
123 notes database: ${DB}
125 text editor: ${EDITOR}
126 terminal: ${TERMINAL}
130 echo "Now I'll create the needed files and directories."
131 read -r -p "Do you wish to continue? (y/N) " ANSWER
135 cat << __EOL__ > ${DB}
138 "version": "${VERSION}",
139 "dbversion": "${DBVERSION}"
144 echo; echo "All done, you can now write your first note."
147 echo "No changes made. Exiting"
153 # check for notes dir existance and create it in case it doesn't exists
154 if [[ ! -d $NOTESDIR ]]; then
155 # we don't have a directory. FIRST RUN?
158 # check if input is a number, returns false or the number itself
159 function check_noteID
() {
171 function helptext
() {
173 echo " $0 [PARAMS] [note ID]..."
175 echo "${BASENAME} parameters are:"
176 echo -e " -h | --help\t\t\t: This help text"
177 echo -e " -p | --plain\t\t\t: Output is in plain text"
178 echo -e "\t\t\t\t (without this option the output is formatted)"
179 echo -e "\t\t\t\t (this option must precede all others)"
180 echo -e " -l | --list\t\t\t: List existing notes"
181 echo -e " -a | --add=[\"<title>\"]\t: Add new note"
182 echo -e " -e | --edit=[<note>]\t\t: Edit note"
183 echo -e " -d | --delete=[<note> | all] : Delete single note or all notes at once"
184 echo -e " -s | --show=[<note>]\t\t: Display note using your favourite PAGER"
185 echo -e " -r | --restore=[<dir>]\t: Restore a previous backup from dir"
186 echo -e " -v | --version\t\t: Print version"
187 echo -e " --userconf\t\t\t: Export User config file"
188 echo -e " --backup [<dest>]\t\t: Backup your data in your destination folder"
190 echo -e "if a non option is passed and is a valid note ID, the note will be displayed."
193 function configtext
() {
195 ${BASENAME} configuration is:
197 base directory: ${BASEDIR}/
198 notes archive: ${NOTESDIR}/
199 notes database: ${DB}
201 debug file: /tmp/debug_bash-note.log
203 text editor: ${EDITOR}
204 terminal: ${TERMINAL}
211 # this function returns a random 2 words title
212 function random_title
() {
215 DICT
=/usr
/share
/dict
/words
218 # total number of non-random words available
219 COUNT
=$
(cat $DICT |
wc -l)
221 # while loop to generate random words
224 RAND
=$
(od -N3 -An -i /dev
/urandom |
awk -v f
=0 -v r
="$COUNT" '{printf "%i\n", f + r * $1 / 16777216}')
225 OUTPUT
+="$(sed `echo $RAND`"q
;d
" $DICT)"
227 [[ $X -eq 1 ]] && OUTPUT
+=" "
233 # returns true if the argument provided directory is a git repository
236 if [[ -d $DIR ]]; then
238 if git rev-parse
2>/dev
/null
; then
246 # sync local repository to remote
248 echo "Syncing notes with git on remote \"$GITREMOTE\""
253 # check for USEGIT and subsequent variables
254 if [[ $USEGIT && -n $GITREMOTE ]]; then
256 if ! is_git_repo
$BASEDIR; then
257 # initializing git repository
260 echo "adding all files to git"
262 $GIT commit
-m "$(basename $0) - initial commit"
263 $GIT remote add origin
$GITREMOTE
264 $GIT push
-u origin master
266 elif [[ $USEGIT && -z $GITREMOTE ]]; then
267 echo "GITREMOTE variable not set. reverting USEGIT to false"
272 # remove eventually existing temp DB file
273 if [[ -f $TMPDB ]]; then
277 RTITLE
=$
(random_title
)
278 [[ -z "$1" ]] && NOTETITLE
="$RTITLE" || NOTETITLE
="$1"
279 echo "adding new note - \"$NOTETITLE\""
280 # shellcheck disable=SC2086
281 LASTID
=$
($JQ '.notes[-1].id // 0 | tonumber' $DB)
282 # [ "" == $LASTID ] && LASTID=0
283 NOTEID
=$
(( LASTID
+ 1 ))
284 # shellcheck disable=SC2086
285 touch ${NOTESDIR}/${NOW}
286 # shellcheck disable=SC2016
287 $JQ --arg i
"$NOTEID" --arg t
"$NOTETITLE" --arg f
"$NOW" '.notes += [{"id": $i, "title": $t, "file": $f}]' "$DB" > $TMPDB
288 # shellcheck disable=SC2086
290 # example for alacritty:
291 # alacritty --class notes --title notes -e /usr/bin/vim ...
292 # shellcheck disable=SC2086,SC2091
293 $
(${TERMINAL} ${TERM_OPTS} ${EDITOR} ${NOTESDIR}/${NOW})
295 function backup_data
() {
297 echo "backing up data in $BACKUPDIR"
300 if [ -d $BACKUPDIR ]; then
301 if [ $
(/bin
/ls -A $BACKUPDIR) ]; then
302 echo "$BACKUPDIR is not empty. Cannot continue"
305 echo "$BACKUPDIR is ok. Continuing!"
308 # BACKUPDIR doesn't exists
309 echo "$BACKUPDIR doesn't exists"
310 read -r -p "Do you want me to create it for you? (y/N) " ANSWER
316 echo "No changes made. Exiting"
321 # ok, we have a backup directory
322 if [ -r $RCFILE ]; then
323 BCKUP_COMM
=$
(rsync
-avz --progress ${RCFILE}* ${BASEDIR}/* ${BACKUPDIR})
325 BCKUP_COMM
=$
(rsync
-avz --progress ${BASEDIR}/* ${BACKUPDIR})
328 if [ "$BCKUP_COMM" ]; then
329 echo -e "All files backed up."
330 echo -e "BACKUP directory:\t$BACKUPDIR"
331 tree
$BACKUPDIR |
$PAGER
332 echo; echo "BACKUP COMPLETED"
336 function backup_restore
() {
338 echo "restoring backup from $BACKUPDIR"
339 echo "This will overwrite all your notes and configurations with the backup."
340 read -r -p "Do you want to continue? (y/N) " ANSWER
344 BACKUPRC
=$
(basename $RCFILE)
345 if [ -r ${BACKUPDIR}/${BACKUPRC} ]; then
346 if [ -r ${RCFILE} ]; then
347 echo "Backing up current '${RCFILE}'...."
348 mv -f ${RCFILE} ${RCFILE}.$
(date +%Y
%m
%d_
%H
%M
)
350 cp --verbose ${BACKUPDIR}/${BACKUPRC} $RCFILE
352 # restoring notes directory
353 if [ -d $BACKUPDIR/notes
]; then
354 if [ $
(/bin
/ls -A $NOTESDIR) ]; then
355 rm --verbose $NOTESDIR/*
357 cp -r --verbose $BACKUPDIR/notes
$BASEDIR
360 BACKUPDB
=$
(basename $DB)
361 if [ -f ${BACKUPDIR}/${BACKUPDB} ]; then
362 if [ -r ${DB} ]; then
363 echo "Backing up current '${DB}'...."
364 mv -f ${DB} ${DB}.$
(date +%Y
%m
%d_
%H
%M
)
366 cp --verbose ${BACKUPDIR}/${BACKUPDB} $DB
370 echo "No changes made. Exiting"
376 function editnote
() {
378 # shellcheck disable=SC2155
379 local OK
=$
(check_noteID
"$NOTE")
381 echo "invalid note \"$NOTE\""
382 echo "Use the note ID that you can fetch after listing your notes"
386 # shellcheck disable=SC2016,SC2086
387 TITLE
=$
($JQ --arg i
$OK '.notes[] | select(.id == $i) | .title' $DB)
388 # shellcheck disable=SC2016,SC2086
389 FILE
=$
($JQ -r --arg i
$OK '.notes[] | select(.id == $i) | .file' $DB)
390 if [ "$TITLE" ]; then
391 echo "editing note $TITLE"
392 # shellcheck disable=SC2086,SC2091
393 $
(${TERMINAL} ${TERM_OPTS} ${EDITOR} ${NOTESDIR}/${FILE})
395 echo "note not found"
399 function listnotes
() {
400 # [ $PLAIN == true ] && echo "output is plain text" || echo "output is colored"
401 if [[ $
(ls -A "$NOTESDIR") ]]; then
402 if [ $PLAIN == false
]; then
403 echo "listing all notes"
406 [ $PLAIN == false
] && echo "[ID] [TITLE] [CREATED]"
407 for i
in "${NOTESDIR}"/*; do
408 # shellcheck disable=SC2155
409 local fname
=$
(basename $i)
410 DATE
=$
(date -d @
${fname} +"%d/%m/%Y %R %z%Z")
411 # shellcheck disable=SC2016,SC2086
412 TITLE
=$
($JQ -r --arg z $
(basename $i) '.notes[] | select(.file == $z) | .title' $DB)
413 # shellcheck disable=SC2016,SC2086
414 ID
=$
($JQ -r --arg z $
(basename $i) '.notes[] | select(.file == $z) | .id' $DB)
415 [ $PLAIN == false
] && echo "[${ID}] ${TITLE} ${DATE}" || echo "${ID} - ${TITLE} - ${DATE}"
418 echo "no notes yet. You can add your first one with: ${BASENAME} -a \"your note title\""
422 # remove eventually existing temp DB file
423 if [[ -f $TMPDB ]]; then
428 if [ "all" == "$NOTE" ]; then
429 echo "You're going to delete all notes."
430 read -r -p "Do you wish to continue? (y/N) " ANSWER
433 # shellcheck disable=SC2086
434 $JQ 'del(.notes[])' $DB > $TMPDB
435 # shellcheck disable=SC2086
437 # shellcheck disable=SC2086
439 echo "Deleted all notes"
442 echo "Aborting, no notes were deleted."
447 # shellcheck disable=SC2155
448 local OK
=$
(check_noteID
"$NOTE")
450 echo "invalid note \"$NOTE\""
451 echo "Use the note ID that you can fetch after listing your notes"
456 # shellcheck disable=SC2016,SC2086
457 TITLE
=$
($JQ --arg i
$OK '.notes[] | select(.id == $i) | .title' $DB)
458 # shellcheck disable=SC2016,SC2086
459 FILE
=$
($JQ -r --arg i
$OK '.notes[] | select(.id == $i) | .file' $DB)
460 if [ "$TITLE" ]; then
461 # shellcheck disable=SC2016,SC2086
462 $JQ -r --arg i
$OK 'del(.notes[] | select(.id == $i))' $DB > $TMPDB
463 # shellcheck disable=SC2086
466 echo "Deleted note $TITLE"
470 echo "note not found"
476 function shownote
() {
479 # shellcheck disable=SC2155
480 local OK
=$
(check_noteID
"$NOTE")
482 echo "invalid note \"$NOTE\""
483 echo "Use the note ID that you can fetch after listing your notes"
487 FILE
=$
($JQ -r --arg i
$OK '.notes[] | select(.id == $i) | .file' $DB)
490 $PAGER ${NOTESDIR}/${FILE}
493 # shellcheck disable=SC2006
494 GOPT
=$
(getopt
-o hvplr
::a
::e
::d
::s
:: --long help,version
,list
,plain
,userconf
,sync
,restore
::,backup
::,add
::,edit
::,delete
::,show
:: -n 'bash-notes' -- "$@")
496 # shellcheck disable=SC2181
497 if [ $?
!= 0 ] ; then helptext
>&2 ; exit 1 ; fi
499 # Note the quotes around `$GOPT': they are essential!
510 echo $BASENAME v
${VERSION}
524 read -r -p "Title: " TITLE
537 read -r -p "Note ID: " NOTE
550 read -r -p "Note ID: " NOTE
563 read -r -p "Note ID: " NOTE
576 read -r -p "Backup Dir: " RDIR
592 # shellcheck disable=SC2317
593 echo "config exported to \"$RCFILE\""
594 # shellcheck disable=SC2317
600 read -r -p "Backup Dir: " BDIR
621 if [ $
(check_noteID
$arg) ]; then