04141e6d25c03e83ff77ff4ec0e490e1186d1b63
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
22 EDITOR
=${EDITOR:-/usr/bin/vim}
23 TERMINAL
=${TERMINAL:-/usr/bin/alacritty}
24 # add options for your terminal. Remember to add the last option to execute
25 # your editor program, otherwise the script will fail.
26 # see example in the addnote function
27 TERM_OPTS
="--class notes --title notes -e "
29 # set this to true to have output in plain text
30 # or use the -p option on the command line before every other option
32 # base directory for program files
33 BASEDIR
=${BASEDIR:-~/.local/share/bash-notes}
34 # notes database in json format
36 # directory containing the actual notes
37 NOTESDIR
=${BASEDIR}/notes
39 } # end set_defaults, do not change this line.
43 # Do not edit below this point
44 RCFILE
=${RCFILE:-~/.config/bash-notes.rc}
46 BASENAME
=$
( basename "$0" )
49 if [ ! -x "$JQ" ]; then
50 echo "jq not found in your PATH"
51 echo "install jq to continue"
55 # IMPORT USER DEFINED OPTIONS IF ANY
56 if [[ -f $RCFILE ]]; then
57 # shellcheck disable=SC1090
61 # We prevent the program from running more than one instance:
62 PIDFILE
=/var
/tmp
/$
(basename "$0" .sh
).pid
64 # Make sure the PID file is removed when we kill the process
65 trap 'rm -f $PIDFILE; exit 1' TERM INT
67 if [[ -r $PIDFILE ]]; then
68 # PIDFILE exists, so I guess there's already an instance running
69 # let's kill it and run again
70 # shellcheck disable=SC2046,SC2086
71 kill -s 15 $
(cat $PIDFILE) > /dev
/null
2>&1
72 # should already be deleted by trap, but just to be sure
77 echo $PID > "$PIDFILE"
79 # check if input is a number, returns false or the number itself
80 function check_noteID
() {
94 echo " $0 [PARAMS] ..."
97 ${BASENAME} configuration is:
99 base directory: ${BASEDIR}/
100 notes archive: ${NOTESDIR}/
101 notes database: ${DB}
103 debug file: /tmp/debug_bash-note.log
105 text editor: ${EDITOR}
106 terminal: ${TERMINAL}
111 echo "${BASENAME} parameters are:"
112 echo " -h | --help : This help text"
113 echo " -p | --plain : Output is in plain text"
114 echo " (without this option the output is formatted)"
115 echo " (this option must precede all others)"
116 echo " -l | --list : List existing notes"
117 echo " -a | --add [\"<title>\"] : Add new note"
118 echo " -e | --edit [<note>] : Edit note"
119 echo " -d | --delete [<note> | all] : Delete single note or all notes at once"
120 echo " -v | --version : Print version"
121 echo " --userconf : Export User config file"
126 # remove eventually existing temp DB file
127 if [[ -f $TMPDB ]]; then
132 echo "adding new note - \"$NOTETITLE\""
133 # shellcheck disable=SC2086
134 LASTID
=$
($JQ '.notes[-1].id // 0 | tonumber' $DB)
135 # [ "" == $LASTID ] && LASTID=0
136 NOTEID
=$
(( LASTID
+ 1 ))
137 # shellcheck disable=SC2086
138 touch ${NOTESDIR}/${NOW}
139 # shellcheck disable=SC2016
140 $JQ --arg i
"$NOTEID" --arg t
"$NOTETITLE" --arg f
"$NOW" '.notes += [{"id": $i, "title": $t, "file": $f}]' "$DB" > $TMPDB
141 # shellcheck disable=SC2086
143 # example for alacritty:
144 # alacritty --class notes --title notes -e /usr/bin/vim ...
145 # shellcheck disable=SC2086,SC2091
146 $
(${TERMINAL} ${TERM_OPTS} ${EDITOR} ${NOTESDIR}/${NOW})
149 function listnotes
() {
150 # [ $PLAIN == true ] && echo "output is plain text" || echo "output is colored"
151 if [[ $
(ls -A "$NOTESDIR") ]]; then
152 if [ $PLAIN == false
]; then
153 echo "listing all notes"
156 [ $PLAIN == false
] && echo "[ID] [TITLE] [CREATED]"
157 for i
in "${NOTESDIR}"/*; do
158 # shellcheck disable=SC2155
159 local fname
=$
(basename $i)
160 DATE
=$
(date -d @
${fname} +"%d/%m/%Y %R %z%Z")
161 # shellcheck disable=SC2016,SC2086
162 TITLE
=$
($JQ -r --arg z $
(basename $i) '.notes[] | select(.file == $z) | .title' $DB)
163 # shellcheck disable=SC2016,SC2086
164 ID
=$
($JQ -r --arg z $
(basename $i) '.notes[] | select(.file == $z) | .id' $DB)
165 [ $PLAIN == false
] && echo "[${ID}] ${TITLE} ${DATE}" || echo "${ID} - ${TITLE} - ${DATE}"
168 echo "no notes yet. You can add your first one with: ${BASENAME} -a \"your note title\""
172 function editnote
() {
174 # shellcheck disable=SC2155
175 local OK
=$
(check_noteID
"$NOTE")
177 echo "invalid note \"$NOTE\""
178 echo "Use the note ID that you can fetch after listing your notes"
182 # shellcheck disable=SC2016,SC2086
183 TITLE
=$
($JQ --arg i
$OK '.notes[] | select(.id == $i) | .title' $DB)
184 # shellcheck disable=SC2016,SC2086
185 FILE
=$
($JQ -r --arg i
$OK '.notes[] | select(.id == $i) | .file' $DB)
186 if [ "$TITLE" ]; then
187 echo "editing note $TITLE"
188 # shellcheck disable=SC2086,SC2091
189 $
(${TERMINAL} ${TERM_OPTS} ${EDITOR} ${NOTESDIR}/${FILE})
191 echo "note not found"
197 # remove eventually existing temp DB file
198 if [[ -f $TMPDB ]]; then
203 if [ "all" == "$NOTE" ]; then
204 echo "You're going to delete all notes."
205 read -r -p "Do you wish to continue? (y/N) " ANSWER
208 # shellcheck disable=SC2086
209 $JQ 'del(.notes[])' $DB > $TMPDB
210 # shellcheck disable=SC2086
212 # shellcheck disable=SC2086
214 echo "Deleted all notes"
217 echo "Aborting, no notes were deleted."
222 # shellcheck disable=SC2155
223 local OK
=$
(check_noteID
"$NOTE")
225 echo "invalid note \"$NOTE\""
226 echo "Use the note ID that you can fetch after listing your notes"
230 # shellcheck disable=SC2016,SC2086
231 TITLE
=$
($JQ --arg i
$OK '.notes[] | select(.id == $i) | .title' $DB)
232 # shellcheck disable=SC2016,SC2086
233 FILE
=$
($JQ -r --arg i
$OK '.notes[] | select(.id == $i) | .file' $DB)
234 if [ "$TITLE" ]; then
235 # shellcheck disable=SC2016,SC2086
236 $JQ -r --arg i
$OK 'del(.notes[] | select(.id == $i))' $DB > $TMPDB
237 # shellcheck disable=SC2086
240 echo "Deleted note $TITLE"
242 echo "note not found"
248 function export_config
() {
249 if [ -r ${RCFILE} ]; then
250 echo "Backing up current '${RCFILE}'...."
251 mv -f ${RCFILE} ${RCFILE}.$
(date +%Y
%m
%d_
%H
%M
)
253 echo "Writing '${RCFILE}'...."
254 sed -n '/^set_defaults() {/,/^} # end set_defaults, do not change this line./p' $0 \
255 |
grep -v set_defaults \
256 |
sed -e 's/^\([^=]*\)=\${\1:-\([^}]*\)}/\1=\2/' \
258 if [ -r ${RCFILE} ]; then
259 echo "Taking no further action."
262 echo "Could not write '${RCFILE}'...!"
267 # we should expand on this function to add a sample note and explain a little bit
268 # how the program works.
269 function firstrun
() {
270 [ -f $RCFILE ] && RC
=$RCFILE || RC
="none"
273 echo "${BASENAME} configuration:
275 base directory: ${BASEDIR}/
276 notes archive: ${NOTESDIR}/
277 notes database: ${DB}
279 text editor: ${EDITOR}
280 terminal: ${TERMINAL}
284 read -r -p "Do you wish to continue? (y/N) " ANSWER
288 cat << __EOL__ > ${DB}
291 "version": "${VERSION}",
292 "dbversion": "${NOW}"
297 echo; echo "All done, you can now write your first note."
300 echo "No changes made. Exiting"
306 # check for notes dir existance and create it in case it doesn't exists
307 if [[ ! -d $NOTESDIR ]]; then
308 # we don't have a directory. FIRST RUN?
312 # NOTE: This requires GNU getopt. On Mac OS X and FreeBSD, you have to install this
313 # separately; see below.
314 # shellcheck disable=SC2006
315 GOPT
=`getopt -o hvpla:e:d: --long help,version,list,plain,userconf,add:,edit:,delete:,editor:,storage: \
316 -n 'bash-notes' -- "$@"`
318 # shellcheck disable=SC2181
319 if [ $?
!= 0 ] ; then helptext
>&2 ; exit 1 ; fi
321 # Note the quotes around `$GOPT': they are essential!
331 echo $BASENAME v
${VERSION}
359 # shellcheck disable=SC2317
360 echo "config exported to \"$RCFILE\""
361 # shellcheck disable=SC2317