68feaf964c6e23d2665c9c6640cbd83377146810
10 EDITOR
=${EDITOR:-/usr/bin/vim}
11 TERMINAL
=${TERMINAL:-/usr/bin/alacritty}
12 TERM_OPTS
="--class notes --title notes -e "
15 # base directory for program files
16 BASEDIR
=${BASEDIR:-~/.local/share/bash-notes}
17 # notes database in json format
19 # directory containing the actual notes
20 NOTESDIR
=${BASEDIR}/notes
22 } # end set_defaults, do not change this line.
26 # Do not edit below this point
27 RCFILE
=${RCFILE:-~/.config/bash-notes.rc}
29 BASENAME
=$
( basename $0 )
33 echo "jq not found in your PATH"
34 echo "install jq to continue"
38 # IMPORT USER DEFINED OPTIONS IF ANY
39 if [[ -f $RCFILE ]]; then
43 # We prevent the program from running more than one instance:
44 PIDFILE
=/var
/tmp
/$
(basename $0 .sh
).pid
46 # Make sure the PID file is removed when we kill the process
47 trap 'rm -f $PIDFILE; exit 1' TERM INT
49 if [[ -r $PIDFILE ]]; then
50 # PIDFILE exists, so I guess there's already an instance running
51 # let's kill it and run again
52 kill -s 15 $
(cat $PIDFILE) > /dev
/null
2>&1
53 # should already be deleted by trap, but just to be sure
60 # check if input is a number, returns false or the number itself
61 function check_noteID
() {
75 echo " $0 [PARAMS] ..."
78 ${BASENAME} configuration is:
80 base directory: ${BASEDIR}/
81 notes archive: ${NOTESDIR}/
85 text editor: ${EDITOR}
91 echo "${BASENAME} parameters are:"
92 echo " -h | --help : This help text"
93 echo " -p | --plain : Output is in plain text"
94 echo " (without this option the output is colored)"
95 echo " -l | --list : List existing notes"
96 echo " -a | --add <title> : Add new note"
97 echo " -m | --modify <note> : Modify note"
98 echo " -d | --date <note> : Modify date for note"
99 echo " -r | --remove <note> : Remove note"
100 echo " -v | --version : Print version"
101 echo " --userconf : Export User config file"
107 echo "adding new note - \"$NOTETITLE\""
108 LASTID
=$
($JQ '.notes[-1].id // 0 | tonumber' $DB)
109 # [ "" == $LASTID ] && LASTID=0
110 NOTEID
=$
(( $LASTID + 1 ))
111 touch ${NOTESDIR}/${NOW}
112 $JQ --arg i
"$NOTEID" --arg t
"$NOTETITLE" --arg f
"$NOW" '.notes += [{"id": $i, "title": $t, "file": $f}]' "$DB" > $TMPDB
114 $
(${TERMINAL} ${TERM_OPTS} ${EDITOR} ${NOTESDIR}/${NOW})
117 function listnotes
() {
118 # [ $PLAIN == true ] && echo "output is plain text" || echo "output is colored"
119 if [[ $
(ls -A $NOTESDIR) ]]; then
120 echo "listing all notes"
122 echo "[ID] [TITLE] [SIZE]"
123 for i
in ${NOTESDIR}/*; do
124 SIZE
=$
(du
-k $i |cut
-f 1)
125 TITLE
=$
($JQ -r --arg z $
(basename $i) '.notes[] | select(.file == $z) | .title' $DB)
126 ID
=$
($JQ -r --arg z $
(basename $i) '.notes[] | select(.file == $z) | .id' $DB)
128 echo "[${ID}] ${TITLE} ${SIZE}kb"
131 echo "no notes yet. You can add your first one with: ${BASENAME} -a \"your note title\""
135 function editnote
() {
137 local OK
=$
(check_noteID
$NOTE)
139 echo "invalid note \"$NOTE\""
143 TITLE
=$
($JQ --arg i
$OK '.notes[] | select(.id == $i) | .title' $DB)
144 FILE
=$
($JQ -r --arg i
$OK '.notes[] | select(.id == $i) | .file' $DB)
145 if [ "$TITLE" ]; then
146 echo "editing note $TITLE"
147 $
(${TERMINAL} --class notes --title notes -e ${EDITOR} ${NOTESDIR}/${FILE})
149 echo "note not found"
154 function datenote
() {
156 local OK
=$
(check_noteID
$NOTE)
157 [ $OK ] && echo "editing date for note $OK" ||
echo "invalid note \"$NOTE\""
158 # FILEDATE=$(date -d @$NOW +%d/%m/%Y_%T)
164 local OK
=$
(check_noteID
$NOTE)
165 [ $OK ] && echo "removing note $OK" ||
echo "invalid note \"$NOTE\""
168 function export_config
() {
169 if [ -r ${RCFILE} ]; then
170 echo "Backing up current '${RCFILE}'...."
171 mv -f ${RCFILE} ${RCFILE}.$
(date +%Y
%m
%d_
%H
%M
)
173 echo "Writing '${RCFILE}'...."
174 sed -n '/^set_defaults() {/,/^} # end set_defaults, do not change this line./p' $0 \
175 |
grep -v set_defaults \
176 |
sed -e 's/^\([^=]*\)=\${\1:-\([^}]*\)}/\1=\2/' \
178 if [ -r ${RCFILE} ]; then
179 echo "Taking no further action."
182 echo "Could not write '${RCFILE}'...!"
187 # we should expand on this function to add a sample note and explain a little bit
188 # how the program works.
189 function firstrun
() {
190 [ -f $RCFILE ] && RC
=$RCFILE || RC
="none"
193 echo "${BASENAME} configuration:
195 base directory: ${BASEDIR}/
196 notes archive: ${NOTESDIR}/
197 notes database: ${DB}
199 text editor: ${EDITOR}
200 terminal: ${TERMINAL}
204 read -r -p "Do you wish to continue? (y/N) " ANSWER
208 cat << __EOL__ > ${DB}
211 "version": "${VERSION}",
212 "dbversion": "${NOW}"
217 echo; echo "All done, you can now write your first note."
220 echo "No changes made. Exiting"
226 # check for notes dir existance and create it in case it doesn't exists
227 if [[ ! -d $NOTESDIR ]]; then
228 # we don't have a directory. FIRST RUN?
232 # NOTE: This requires GNU getopt. On Mac OS X and FreeBSD, you have to install this
233 # separately; see below.
234 GOPT
=`getopt -o hvpla:m:d:r: --long help,version,list,plain,userconf,add:,modify:,date:,remove:,editor:,storage: \
235 -n 'bash-notes' -- "$@"`
237 if [ $?
!= 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
239 # Note the quotes around `$GOPT': they are essential!
251 echo $BASENAME v
${VERSION}
284 echo "config exported to \"$RCFILE\""