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
59 # check if input is a number, returns false or the number itself
60 function check_noteID
() {
74 echo " $0 [PARAMS] ..."
77 ${BASENAME} configuration is:
79 base directory: ${BASEDIR}/
80 notes archive: ${NOTESDIR}/
84 text editor: ${EDITOR}
90 echo "The script's parameters are:"
91 echo " -h | --help : This help text"
92 echo " -p | --plain : Output is in plain text"
93 echo " (without this option the output is colored)"
94 echo " -l | --list : List existing notes"
95 echo " -a | --add <title> : Add new note"
96 echo " -m | --modify <note> : Modify note"
97 echo " -d | --date <note> : Modify date for note"
98 echo " -r | --remove <note> : Remove note"
99 echo " -v | --version : Print version"
100 echo " --userconf : Export User config file"
105 echo "adding new note - \"$NOTETITLE\""
106 LASTID
=$
($JQ '.notes[-1].id | tonumber' $DB)
107 [ null
== $LASTID ] && LASTID
=0
108 NOTEID
=$
(( $LASTID + 1 ))
109 touch ${NOTESDIR}/${NOW}
110 $JQ --arg i
"$NOTEID" --arg t
"$NOTETITLE" --arg f
"$NOW" '.notes += [{"id": $i, "title": $t, "file": $f}]' "$DB" > $TMPDB
112 $
(${TERMINAL} --class notes --title notes -e ${EDITOR} ${NOTESDIR}/${NOW})
115 function listnotes
() {
116 echo "listing all notes"
117 [ $PLAIN == true
] && echo "output is plain text" ||
echo "output is colored"
120 for i
in ${NOTESDIR}/*; do
121 TITLE
=$
($JQ -r --arg z $
(basename $i) '.notes[] | select(.file == $z) | .title' $DB)
122 ID
=$
($JQ -r --arg z $
(basename $i) '.notes[] | select(.file == $z) | .id' $DB)
124 echo "[${ID}] ${TITLE}"
128 function editnote
() {
130 local OK
=$
(check_noteID
$NOTE)
132 echo "invalid note \"$NOTE\""
136 TITLE
=$
($JQ --arg i
$OK '.notes[] | select(.id == $i) | .title' $DB)
137 FILE
=$
($JQ -r --arg i
$OK '.notes[] | select(.id == $i) | .file' $DB)
138 if [ "$TITLE" ]; then
139 echo "editing note $TITLE"
140 $
(${TERMINAL} --class notes --title notes -e ${EDITOR} ${NOTESDIR}/${FILE})
142 echo "note not found"
147 function datenote
() {
149 local OK
=$
(check_noteID
$NOTE)
150 [ $OK ] && echo "editing date for note $OK" ||
echo "invalid note \"$NOTE\""
151 # FILEDATE=$(date -d @$NOW +%d/%m/%Y_%T)
157 local OK
=$
(check_noteID
$NOTE)
158 [ $OK ] && echo "removing note $OK" ||
echo "invalid note \"$NOTE\""
161 function export_config
() {
162 if [ -r ${RCFILE} ]; then
163 echo "Backing up current '${RCFILE}'...."
164 mv -f ${RCFILE} ${RCFILE}.$
(date +%Y
%m
%d_
%H
%M
)
166 echo "Writing '${RCFILE}'...."
167 sed -n '/^set_defaults() {/,/^} # end set_defaults, do not change this line./p' $0 \
168 |
grep -v set_defaults \
169 |
sed -e 's/^\([^=]*\)=\${\1:-\([^}]*\)}/\1=\2/' \
171 if [ -r ${RCFILE} ]; then
172 echo "Taking no further action."
175 echo "Could not write '${RCFILE}'...!"
180 # we should expand on this function to add a sample note and explain a little bit
181 # how the program works.
182 function firstrun
() {
183 [ -f $RCFILE ] && RC
=$RCFILE || RC
="none"
186 echo "${BASENAME} configuration:
188 base directory: ${BASEDIR}/
189 notes archive: ${NOTESDIR}/
190 notes database: ${DB}
192 text editor: ${EDITOR}
193 terminal: ${TERMINAL}
197 read -r -p "Do you wish to continue? (y/N) " ANSWER
201 cat << __EOL__ > ${DB}
204 "version": "${VERSION}",
205 "dbversion": "${NOW}"
210 echo; echo "All done, you can now write your first note."
213 echo "No changes made. Exiting"
219 # check for notes dir existance and create it in case it doesn't exists
220 if [[ ! -d $NOTESDIR ]]; then
221 # we don't have a directory. FIRST RUN?
225 # NOTE: This requires GNU getopt. On Mac OS X and FreeBSD, you have to install this
226 # separately; see below.
227 GOPT
=`getopt -o hvpla:m:d:r: --long help,version,list,plain,userconf,add:,modify:,date:,remove:,editor:,storage: \
228 -n 'bash-notes' -- "$@"`
230 if [ $?
!= 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
232 # Note the quotes around `$GOPT': they are essential!
244 echo $BASENAME v
${VERSION}
277 echo "config exported to \"$RCFILE\""