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 )
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
60 # We prevent the program from running more than one instance:
61 PIDFILE
=/var
/tmp
/$
(basename $0 .sh
).pid
63 # Make sure the PID file is removed when we kill the process
64 trap 'rm -f $PIDFILE; exit 1' TERM INT
66 if [[ -r $PIDFILE ]]; then
67 # PIDFILE exists, so I guess there's already an instance running
68 # let's kill it and run again
69 kill -s 15 $
(cat $PIDFILE) > /dev
/null
2>&1
70 # should already be deleted by trap, but just to be sure
77 # check if input is a number, returns false or the number itself
78 function check_noteID
() {
92 echo " $0 [PARAMS] ..."
95 ${BASENAME} configuration is:
97 base directory: ${BASEDIR}/
98 notes archive: ${NOTESDIR}/
101 debug file: /tmp/debug_bash-note.log
103 text editor: ${EDITOR}
104 terminal: ${TERMINAL}
109 echo "${BASENAME} parameters are:"
110 echo " -h | --help : This help text"
111 echo " -p | --plain : Output is in plain text"
112 echo " (without this option the output is formatted)"
113 echo " (this option must precede all others)"
114 echo " -l | --list : List existing notes"
115 echo " -a | --add [\"<title>\"] : Add new note"
116 echo " -e | --edit [<note>] : Edit note"
117 echo " -d | --delete [<note> | all] : Delete single note or all notes at once"
118 echo " -v | --version : Print version"
119 echo " --userconf : Export User config file"
124 # remove eventually existing temp DB file
125 if [[ -f $TMPDB ]]; then
130 echo "adding new note - \"$NOTETITLE\""
131 LASTID
=$
($JQ '.notes[-1].id // 0 | tonumber' $DB)
132 # [ "" == $LASTID ] && LASTID=0
133 NOTEID
=$
(( $LASTID + 1 ))
134 touch ${NOTESDIR}/${NOW}
135 $JQ --arg i
"$NOTEID" --arg t
"$NOTETITLE" --arg f
"$NOW" '.notes += [{"id": $i, "title": $t, "file": $f}]' "$DB" > $TMPDB
137 # example for alacritty:
138 # alacritty --class notes --title notes -e /usr/bin/vim ...
139 $
(${TERMINAL} ${TERM_OPTS} ${EDITOR} ${NOTESDIR}/${NOW})
142 function listnotes
() {
143 # [ $PLAIN == true ] && echo "output is plain text" || echo "output is colored"
144 if [[ $
(ls -A $NOTESDIR) ]]; then
145 if [ $PLAIN == false
]; then
146 echo "listing all notes"
149 [ $PLAIN == false
] && echo "[ID] [TITLE] [CREATED]"
150 for i
in ${NOTESDIR}/*; do
151 local fname
=$
(basename $i)
152 DATE
=$
(date -d @
${fname} +"%d/%m/%Y %R %z%Z")
153 TITLE
=$
($JQ -r --arg z $
(basename $i) '.notes[] | select(.file == $z) | .title' $DB)
154 ID
=$
($JQ -r --arg z $
(basename $i) '.notes[] | select(.file == $z) | .id' $DB)
155 [ $PLAIN == false
] && echo "[${ID}] ${TITLE} ${DATE}" || echo "${ID} - ${TITLE} - ${DATE}"
158 echo "no notes yet. You can add your first one with: ${BASENAME} -a \"your note title\""
162 function editnote
() {
164 local OK
=$
(check_noteID
$NOTE)
166 echo "invalid note \"$NOTE\""
170 TITLE
=$
($JQ --arg i
$OK '.notes[] | select(.id == $i) | .title' $DB)
171 FILE
=$
($JQ -r --arg i
$OK '.notes[] | select(.id == $i) | .file' $DB)
172 if [ "$TITLE" ]; then
173 echo "editing note $TITLE"
174 $
(${TERMINAL} ${TERM_OPTS} ${EDITOR} ${NOTESDIR}/${FILE})
176 echo "note not found"
182 # remove eventually existing temp DB file
183 if [[ -f $TMPDB ]]; then
188 if [ "all" == $NOTE ]; then
189 echo "You're going to delete all notes."
190 read -r -p "Do you wish to continue? (y/N) " ANSWER
193 $JQ 'del(.notes[])' $DB > $TMPDB
196 echo "Deleted all notes"
199 echo "Aborting, no notes were deleted."
204 local OK
=$
(check_noteID
$NOTE)
206 echo "invalid note \"$NOTE\""
210 TITLE
=$
($JQ --arg i
$OK '.notes[] | select(.id == $i) | .title' $DB)
211 FILE
=$
($JQ -r --arg i
$OK '.notes[] | select(.id == $i) | .file' $DB)
212 if [ "$TITLE" ]; then
213 $JQ -r --arg i
$OK 'del(.notes[] | select(.id == $i))' $DB > $TMPDB
216 echo "Deleted note $TITLE"
218 echo "note not found"
224 function export_config
() {
225 if [ -r ${RCFILE} ]; then
226 echo "Backing up current '${RCFILE}'...."
227 mv -f ${RCFILE} ${RCFILE}.$
(date +%Y
%m
%d_
%H
%M
)
229 echo "Writing '${RCFILE}'...."
230 sed -n '/^set_defaults() {/,/^} # end set_defaults, do not change this line./p' $0 \
231 |
grep -v set_defaults \
232 |
sed -e 's/^\([^=]*\)=\${\1:-\([^}]*\)}/\1=\2/' \
234 if [ -r ${RCFILE} ]; then
235 echo "Taking no further action."
238 echo "Could not write '${RCFILE}'...!"
243 # we should expand on this function to add a sample note and explain a little bit
244 # how the program works.
245 function firstrun
() {
246 [ -f $RCFILE ] && RC
=$RCFILE || RC
="none"
249 echo "${BASENAME} configuration:
251 base directory: ${BASEDIR}/
252 notes archive: ${NOTESDIR}/
253 notes database: ${DB}
255 text editor: ${EDITOR}
256 terminal: ${TERMINAL}
260 read -r -p "Do you wish to continue? (y/N) " ANSWER
264 cat << __EOL__ > ${DB}
267 "version": "${VERSION}",
268 "dbversion": "${NOW}"
273 echo; echo "All done, you can now write your first note."
276 echo "No changes made. Exiting"
282 # check for notes dir existance and create it in case it doesn't exists
283 if [[ ! -d $NOTESDIR ]]; then
284 # we don't have a directory. FIRST RUN?
288 # NOTE: This requires GNU getopt. On Mac OS X and FreeBSD, you have to install this
289 # separately; see below.
290 GOPT
=`getopt -o hvpla:e:d: --long help,version,list,plain,userconf,add:,edit:,delete:,editor:,storage: \
291 -n 'bash-notes' -- "$@"`
293 if [ $?
!= 0 ] ; then helptext
>&2 ; exit 1 ; fi
295 # Note the quotes around `$GOPT': they are essential!
305 echo $BASENAME v
${VERSION}
333 echo "config exported to \"$RCFILE\""