10 EDITOR
=${EDITOR:-/usr/bin/vim}
11 TERMINAL
=${TERMINAL:-/usr/bin/alacritty}
12 # add options for your terminal. Remember to add the last option to execute
13 # your editor program, otherwise the script will fail.
14 # see example in the addnote function
15 TERM_OPTS
="--class notes --title notes -e "
18 # base directory for program files
19 BASEDIR
=${BASEDIR:-~/.local/share/bash-notes}
20 # notes database in json format
22 # directory containing the actual notes
23 NOTESDIR
=${BASEDIR}/notes
25 } # end set_defaults, do not change this line.
29 # Do not edit below this point
30 RCFILE
=${RCFILE:-~/.config/bash-notes.rc}
32 BASENAME
=$
( basename $0 )
36 echo "jq not found in your PATH"
37 echo "install jq to continue"
41 # IMPORT USER DEFINED OPTIONS IF ANY
42 if [[ -f $RCFILE ]]; then
46 # We prevent the program from running more than one instance:
47 PIDFILE
=/var
/tmp
/$
(basename $0 .sh
).pid
49 # Make sure the PID file is removed when we kill the process
50 trap 'rm -f $PIDFILE; exit 1' TERM INT
52 if [[ -r $PIDFILE ]]; then
53 # PIDFILE exists, so I guess there's already an instance running
54 # let's kill it and run again
55 kill -s 15 $
(cat $PIDFILE) > /dev
/null
2>&1
56 # should already be deleted by trap, but just to be sure
63 # check if input is a number, returns false or the number itself
64 function check_noteID
() {
78 echo " $0 [PARAMS] ..."
81 ${BASENAME} configuration is:
83 base directory: ${BASEDIR}/
84 notes archive: ${NOTESDIR}/
88 text editor: ${EDITOR}
94 echo "${BASENAME} parameters are:"
95 echo " -h | --help : This help text"
96 echo " -p | --plain : Output is in plain text"
97 echo " (without this option the output is formatted)"
98 echo " (this option must precede all others)"
99 echo " -l | --list : List existing notes"
100 echo " -a | --add [\"<title>\"] : Add new note"
101 echo " -m | --modify [<note>] : Modify note"
102 echo " -d | --delete [<note> | all] : Delete single note or all notes at once"
103 echo " -v | --version : Print version"
104 echo " --userconf : Export User config file"
109 # remove eventually existing temp DB file
110 if [[ -f $TMPDB ]]; then
115 echo "adding new note - \"$NOTETITLE\""
116 LASTID
=$
($JQ '.notes[-1].id // 0 | tonumber' $DB)
117 # [ "" == $LASTID ] && LASTID=0
118 NOTEID
=$
(( $LASTID + 1 ))
119 touch ${NOTESDIR}/${NOW}
120 $JQ --arg i
"$NOTEID" --arg t
"$NOTETITLE" --arg f
"$NOW" '.notes += [{"id": $i, "title": $t, "file": $f}]' "$DB" > $TMPDB
122 # example for alacritty:
123 # alacritty --class notes --title notes -e /usr/bin/vim ...
124 $
(${TERMINAL} ${TERM_OPTS} ${EDITOR} ${NOTESDIR}/${NOW})
127 function listnotes
() {
128 # [ $PLAIN == true ] && echo "output is plain text" || echo "output is colored"
129 if [[ $
(ls -A $NOTESDIR) ]]; then
130 if [ $PLAIN == false
]; then
131 echo "listing all notes"
134 [ $PLAIN == false
] && echo "[ID] [TITLE] [CREATED]"
135 for i
in ${NOTESDIR}/*; do
136 local fname
=$
(basename $i)
137 DATE
=$
(date -d @
${fname} +"%d/%m/%Y %R %z%Z")
138 TITLE
=$
($JQ -r --arg z $
(basename $i) '.notes[] | select(.file == $z) | .title' $DB)
139 ID
=$
($JQ -r --arg z $
(basename $i) '.notes[] | select(.file == $z) | .id' $DB)
140 [ $PLAIN == false
] && echo "[${ID}] ${TITLE} ${DATE}" || echo "${ID} - ${TITLE} - ${DATE}"
143 echo "no notes yet. You can add your first one with: ${BASENAME} -a \"your note title\""
147 function editnote
() {
149 local OK
=$
(check_noteID
$NOTE)
151 echo "invalid note \"$NOTE\""
155 TITLE
=$
($JQ --arg i
$OK '.notes[] | select(.id == $i) | .title' $DB)
156 FILE
=$
($JQ -r --arg i
$OK '.notes[] | select(.id == $i) | .file' $DB)
157 if [ "$TITLE" ]; then
158 echo "editing note $TITLE"
159 $
(${TERMINAL} ${TERM_OPTS} ${EDITOR} ${NOTESDIR}/${FILE})
161 echo "note not found"
167 # remove eventually existing temp DB file
168 if [[ -f $TMPDB ]]; then
173 if [ "all" == $NOTE ]; then
174 echo "You're going to delete all notes."
175 read -r -p "Do you wish to continue? (y/N) " ANSWER
178 $JQ 'del(.notes[])' $DB > $TMPDB
181 echo "Deleted all notes"
184 echo "Aborting, no notes were deleted."
189 local OK
=$
(check_noteID
$NOTE)
191 echo "invalid note \"$NOTE\""
195 TITLE
=$
($JQ --arg i
$OK '.notes[] | select(.id == $i) | .title' $DB)
196 FILE
=$
($JQ -r --arg i
$OK '.notes[] | select(.id == $i) | .file' $DB)
197 if [ "$TITLE" ]; then
198 $JQ -r --arg i
$OK 'del(.notes[] | select(.id == $i))' $DB > $TMPDB
201 echo "Deleted note $TITLE"
203 echo "note not found"
209 function export_config
() {
210 if [ -r ${RCFILE} ]; then
211 echo "Backing up current '${RCFILE}'...."
212 mv -f ${RCFILE} ${RCFILE}.$
(date +%Y
%m
%d_
%H
%M
)
214 echo "Writing '${RCFILE}'...."
215 sed -n '/^set_defaults() {/,/^} # end set_defaults, do not change this line./p' $0 \
216 |
grep -v set_defaults \
217 |
sed -e 's/^\([^=]*\)=\${\1:-\([^}]*\)}/\1=\2/' \
219 if [ -r ${RCFILE} ]; then
220 echo "Taking no further action."
223 echo "Could not write '${RCFILE}'...!"
228 # we should expand on this function to add a sample note and explain a little bit
229 # how the program works.
230 function firstrun
() {
231 [ -f $RCFILE ] && RC
=$RCFILE || RC
="none"
234 echo "${BASENAME} configuration:
236 base directory: ${BASEDIR}/
237 notes archive: ${NOTESDIR}/
238 notes database: ${DB}
240 text editor: ${EDITOR}
241 terminal: ${TERMINAL}
245 read -r -p "Do you wish to continue? (y/N) " ANSWER
249 cat << __EOL__ > ${DB}
252 "version": "${VERSION}",
253 "dbversion": "${NOW}"
258 echo; echo "All done, you can now write your first note."
261 echo "No changes made. Exiting"
267 # check for notes dir existance and create it in case it doesn't exists
268 if [[ ! -d $NOTESDIR ]]; then
269 # we don't have a directory. FIRST RUN?
273 # NOTE: This requires GNU getopt. On Mac OS X and FreeBSD, you have to install this
274 # separately; see below.
275 GOPT
=`getopt -o hvpla:m:d: --long help,version,list,plain,userconf,add:,modify:,delete:,editor:,storage: \
276 -n 'bash-notes' -- "$@"`
278 if [ $?
!= 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
280 # Note the quotes around `$GOPT': they are essential!
292 echo $BASENAME v
${VERSION}
320 echo "config exported to \"$RCFILE\""