10 EDITOR
=${EDITOR:-/usr/bin/vim}
11 TERMINAL
=${TERMINAL:-/usr/bin/alacritty}
14 # base directory for program files
15 BASEDIR
=${BASEDIR:-~/.local/share/bash-notes}
16 # notes database in json format
18 # directory containing the actual notes
19 NOTESDIR
=${BASEDIR}/notes
21 } # end set_defaults, do not change this line.
25 # Do not edit below this point
26 RCFILE
=${RCFILE:-~/.config/bash-notes.rc}
28 BASENAME
=$
( basename $0 )
32 echo "jq not found in your PATH"
33 echo "install jq to continue"
37 # IMPORT USER DEFINED OPTIONS IF ANY
38 if [[ -f $RCFILE ]]; then
42 # We prevent the program from running more than one instance:
43 PIDFILE
=/var
/tmp
/$
(basename $0 .sh
).pid
45 # Make sure the PID file is removed when we kill the process
46 trap 'rm -f $PIDFILE; exit 1' TERM INT
48 if [[ -r $PIDFILE ]]; then
49 # PIDFILE exists, so I guess there's already an instance running
50 # let's kill it and run again
51 kill -s 15 $
(cat $PIDFILE) > /dev
/null
2>&1
52 # should already be deleted by trap, but just to be sure
61 echo " $0 [PARAMS] ..."
64 ${BASENAME} configuration is:
66 base directory: ${BASEDIR}/
67 notes archive: ${NOTESDIR}/
71 text editor: ${EDITOR}
77 echo "The script's parameters are:"
78 echo " -h | --help : This help text"
79 echo " -p | --plain : Output is in plain text"
80 echo " (without this option the output is colored)"
81 echo " -l | --list : List existing notes"
82 echo " -a | --add <title> : Add new note"
83 echo " -m | --modify <note> : Modify note"
84 echo " -d | --date <note> : Modify date for note"
85 echo " -r | --remove <note> : Remove note"
86 echo " -v | --version : Print version"
87 echo " --userconf : Export User config file"
92 echo "adding new note - \"$NOTETITLE\""
93 LASTID
=$
($JQ '.notes[-1].id | tonumber' $DB)
94 [ null
== $LASTID ] && LASTID
=0
95 NOTEID
=$
(( $LASTID + 1 ))
96 touch ${NOTESDIR}/${NOW}
97 $JQ --arg i
"$NOTEID" --arg t
"$NOTETITLE" --arg f
"$NOW" '.notes += [{"id": $i, "title": $t, "file": $f}]' "$DB" > $TMPDB
99 $
(${TERMINAL} --class notes --title notes -e ${EDITOR} ${NOTESDIR}/${NOW})
102 function listnotes
() {
103 echo "listing all notes"
104 [ $PLAIN == true
] && echo "output is plain text" ||
echo "output is colored"
107 for i
in ${NOTESDIR}/*; do
108 TITLE
=$
($JQ -r --arg z $
(basename $i) '.notes[] | select(.file == $z) | .title' $DB)
109 ID
=$
($JQ -r --arg z $
(basename $i) '.notes[] | select(.file == $z) | .id' $DB)
111 echo "[${ID}] ${TITLE}"
115 function editnote
() {
116 TITLE
=$
($JQ --arg i
$1 '.notes[] | select(.id == $i) | .title' $DB)
117 FILE
=$
($JQ -r --arg i
$1 '.notes[] | select(.id == $i) | .file' $DB)
118 if [ "$TITLE" ]; then
119 echo "editing note $TITLE"
120 $
(${TERMINAL} --class notes --title notes -e ${EDITOR} ${NOTESDIR}/${FILE})
122 echo "note not found"
127 function datenote
() {
128 echo "edit date for note \"${1}\""
129 # FILEDATE=$(date -d @$NOW +%d/%m/%Y_%T)
135 echo "removing note $NOTE"
138 function export_config
() {
139 if [ -r ${RCFILE} ]; then
140 echo "Backing up current '${RCFILE}'...."
141 mv -f ${RCFILE} ${RCFILE}.$
(date +%Y
%m
%d_
%H
%M
)
143 echo "Writing '${RCFILE}'...."
144 sed -n '/^set_defaults() {/,/^} # end set_defaults, do not change this line./p' $0 \
145 |
grep -v set_defaults \
146 |
sed -e 's/^\([^=]*\)=\${\1:-\([^}]*\)}/\1=\2/' \
148 if [ -r ${RCFILE} ]; then
149 echo "Taking no further action."
152 echo "Could not write '${RCFILE}'...!"
157 # we should expand on this function to add a sample note and explain a little bit
158 # how the program works.
159 function firstrun
() {
160 [ -f $RCFILE ] && RC
=$RCFILE || RC
="none"
163 echo "${BASENAME} configuration:
165 base directory: ${BASEDIR}/
166 notes archive: ${NOTESDIR}/
167 notes database: ${DB}
169 text editor: ${EDITOR}
170 terminal: ${TERMINAL}
174 read -r -p "Do you wish to continue? (y/N) " ANSWER
178 cat << __EOL__ > ${DB}
181 "version": "${VERSION}",
182 "dbversion": "${NOW}"
187 echo; echo "All done, you can now write your first note."
190 echo "No changes made. Exiting"
196 # check for notes dir existance and create it in case it doesn't exists
197 if [[ ! -d $NOTESDIR ]]; then
198 # we don't have a directory. FIRST RUN?
202 # NOTE: This requires GNU getopt. On Mac OS X and FreeBSD, you have to install this
203 # separately; see below.
204 GOPT
=`getopt -o hvpla:m:d:r: --long help,version,list,plain,userconf,add:,modify:,date:,remove:,editor:,storage: \
205 -n 'bash-notes' -- "$@"`
207 if [ $?
!= 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
209 # Note the quotes around `$GOPT': they are essential!
221 echo $BASENAME v
${VERSION}
254 echo "config exported to \"$RCFILE\""