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/
13 EDITOR
=${EDITOR:-/usr/bin/vim}
14 TERMINAL
=${TERMINAL:-/usr/bin/alacritty}
15 # add options for your terminal. Remember to add the last option to execute
16 # your editor program, otherwise the script will fail.
17 # see example in the addnote function
18 TERM_OPTS
="--class notes --title notes -e "
21 # base directory for program files
22 BASEDIR
=${BASEDIR:-~/.local/share/bash-notes}
23 # notes database in json format
25 # directory containing the actual notes
26 NOTESDIR
=${BASEDIR}/notes
28 } # end set_defaults, do not change this line.
32 # Do not edit below this point
33 RCFILE
=${RCFILE:-~/.config/bash-notes.rc}
35 BASENAME
=$
( basename $0 )
39 echo "jq not found in your PATH"
40 echo "install jq to continue"
44 # IMPORT USER DEFINED OPTIONS IF ANY
45 if [[ -f $RCFILE ]]; then
49 # We prevent the program from running more than one instance:
50 PIDFILE
=/var
/tmp
/$
(basename $0 .sh
).pid
52 # Make sure the PID file is removed when we kill the process
53 trap 'rm -f $PIDFILE; exit 1' TERM INT
55 if [[ -r $PIDFILE ]]; then
56 # PIDFILE exists, so I guess there's already an instance running
57 # let's kill it and run again
58 kill -s 15 $
(cat $PIDFILE) > /dev
/null
2>&1
59 # should already be deleted by trap, but just to be sure
66 # check if input is a number, returns false or the number itself
67 function check_noteID
() {
81 echo " $0 [PARAMS] ..."
84 ${BASENAME} configuration is:
86 base directory: ${BASEDIR}/
87 notes archive: ${NOTESDIR}/
91 text editor: ${EDITOR}
97 echo "${BASENAME} parameters are:"
98 echo " -h | --help : This help text"
99 echo " -p | --plain : Output is in plain text"
100 echo " (without this option the output is formatted)"
101 echo " (this option must precede all others)"
102 echo " -l | --list : List existing notes"
103 echo " -a | --add [\"<title>\"] : Add new note"
104 echo " -e | --edit [<note>] : Edit note"
105 echo " -d | --delete [<note> | all] : Delete single note or all notes at once"
106 echo " -v | --version : Print version"
107 echo " --userconf : Export User config file"
112 # remove eventually existing temp DB file
113 if [[ -f $TMPDB ]]; then
118 echo "adding new note - \"$NOTETITLE\""
119 LASTID
=$
($JQ '.notes[-1].id // 0 | tonumber' $DB)
120 # [ "" == $LASTID ] && LASTID=0
121 NOTEID
=$
(( $LASTID + 1 ))
122 touch ${NOTESDIR}/${NOW}
123 $JQ --arg i
"$NOTEID" --arg t
"$NOTETITLE" --arg f
"$NOW" '.notes += [{"id": $i, "title": $t, "file": $f}]' "$DB" > $TMPDB
125 # example for alacritty:
126 # alacritty --class notes --title notes -e /usr/bin/vim ...
127 $
(${TERMINAL} ${TERM_OPTS} ${EDITOR} ${NOTESDIR}/${NOW})
130 function listnotes
() {
131 # [ $PLAIN == true ] && echo "output is plain text" || echo "output is colored"
132 if [[ $
(ls -A $NOTESDIR) ]]; then
133 if [ $PLAIN == false
]; then
134 echo "listing all notes"
137 [ $PLAIN == false
] && echo "[ID] [TITLE] [CREATED]"
138 for i
in ${NOTESDIR}/*; do
139 local fname
=$
(basename $i)
140 DATE
=$
(date -d @
${fname} +"%d/%m/%Y %R %z%Z")
141 TITLE
=$
($JQ -r --arg z $
(basename $i) '.notes[] | select(.file == $z) | .title' $DB)
142 ID
=$
($JQ -r --arg z $
(basename $i) '.notes[] | select(.file == $z) | .id' $DB)
143 [ $PLAIN == false
] && echo "[${ID}] ${TITLE} ${DATE}" || echo "${ID} - ${TITLE} - ${DATE}"
146 echo "no notes yet. You can add your first one with: ${BASENAME} -a \"your note title\""
150 function editnote
() {
152 local OK
=$
(check_noteID
$NOTE)
154 echo "invalid note \"$NOTE\""
158 TITLE
=$
($JQ --arg i
$OK '.notes[] | select(.id == $i) | .title' $DB)
159 FILE
=$
($JQ -r --arg i
$OK '.notes[] | select(.id == $i) | .file' $DB)
160 if [ "$TITLE" ]; then
161 echo "editing note $TITLE"
162 $
(${TERMINAL} ${TERM_OPTS} ${EDITOR} ${NOTESDIR}/${FILE})
164 echo "note not found"
170 # remove eventually existing temp DB file
171 if [[ -f $TMPDB ]]; then
176 if [ "all" == $NOTE ]; then
177 echo "You're going to delete all notes."
178 read -r -p "Do you wish to continue? (y/N) " ANSWER
181 $JQ 'del(.notes[])' $DB > $TMPDB
184 echo "Deleted all notes"
187 echo "Aborting, no notes were deleted."
192 local OK
=$
(check_noteID
$NOTE)
194 echo "invalid note \"$NOTE\""
198 TITLE
=$
($JQ --arg i
$OK '.notes[] | select(.id == $i) | .title' $DB)
199 FILE
=$
($JQ -r --arg i
$OK '.notes[] | select(.id == $i) | .file' $DB)
200 if [ "$TITLE" ]; then
201 $JQ -r --arg i
$OK 'del(.notes[] | select(.id == $i))' $DB > $TMPDB
204 echo "Deleted note $TITLE"
206 echo "note not found"
212 function export_config
() {
213 if [ -r ${RCFILE} ]; then
214 echo "Backing up current '${RCFILE}'...."
215 mv -f ${RCFILE} ${RCFILE}.$
(date +%Y
%m
%d_
%H
%M
)
217 echo "Writing '${RCFILE}'...."
218 sed -n '/^set_defaults() {/,/^} # end set_defaults, do not change this line./p' $0 \
219 |
grep -v set_defaults \
220 |
sed -e 's/^\([^=]*\)=\${\1:-\([^}]*\)}/\1=\2/' \
222 if [ -r ${RCFILE} ]; then
223 echo "Taking no further action."
226 echo "Could not write '${RCFILE}'...!"
231 # we should expand on this function to add a sample note and explain a little bit
232 # how the program works.
233 function firstrun
() {
234 [ -f $RCFILE ] && RC
=$RCFILE || RC
="none"
237 echo "${BASENAME} configuration:
239 base directory: ${BASEDIR}/
240 notes archive: ${NOTESDIR}/
241 notes database: ${DB}
243 text editor: ${EDITOR}
244 terminal: ${TERMINAL}
248 read -r -p "Do you wish to continue? (y/N) " ANSWER
252 cat << __EOL__ > ${DB}
255 "version": "${VERSION}",
256 "dbversion": "${NOW}"
261 echo; echo "All done, you can now write your first note."
264 echo "No changes made. Exiting"
270 # check for notes dir existance and create it in case it doesn't exists
271 if [[ ! -d $NOTESDIR ]]; then
272 # we don't have a directory. FIRST RUN?
276 # NOTE: This requires GNU getopt. On Mac OS X and FreeBSD, you have to install this
277 # separately; see below.
278 GOPT
=`getopt -o hvpla:e:d: --long help,version,list,plain,userconf,add:,edit:,delete:,editor:,storage: \
279 -n 'bash-notes' -- "$@"`
281 if [ $?
!= 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
283 # Note the quotes around `$GOPT': they are essential!
295 echo $BASENAME v
${VERSION}
323 echo "config exported to \"$RCFILE\""