a4aaf855 |
1 | #! /bin/bash |
2 | |
6c152f7e |
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/ |
5 | |
cb8fcb2f |
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 |
11 | BASH_XTRACEFD="5" |
12 | PS4='$LINENO: ' |
13 | set -x |
14 | fi |
a4aaf855 |
15 | |
16 | PID=$$ |
cb8fcb2f |
17 | VERSION="0.2" |
53f2ed57 |
18 | |
d80ac20a |
19 | set_defaults() { |
20 | # Binaries to use |
cb8fcb2f |
21 | JQ=${JQ:-/usr/bin/jq} |
a4aaf855 |
22 | EDITOR=${EDITOR:-/usr/bin/vim} |
53f2ed57 |
23 | TERMINAL=${TERMINAL:-/usr/bin/alacritty} |
b648c006 |
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 |
e3670e83 |
27 | TERM_OPTS="--class notes --title notes -e " |
53f2ed57 |
28 | |
cb8fcb2f |
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 |
31 | PLAIN=false |
d80ac20a |
32 | # base directory for program files |
53f2ed57 |
33 | BASEDIR=${BASEDIR:-~/.local/share/bash-notes} |
d80ac20a |
34 | # notes database in json format |
a4aaf855 |
35 | DB=${BASEDIR}/db.json |
d80ac20a |
36 | # directory containing the actual notes |
a4aaf855 |
37 | NOTESDIR=${BASEDIR}/notes |
53f2ed57 |
38 | |
d80ac20a |
39 | } # end set_defaults, do not change this line. |
40 | |
41 | set_defaults |
42 | |
43 | # Do not edit below this point |
44 | RCFILE=${RCFILE:-~/.config/bash-notes.rc} |
53f2ed57 |
45 | TMPDB=/tmp/db.json |
a4aaf855 |
46 | BASENAME=$( basename $0 ) |
d80ac20a |
47 | NOW=$(date +%s) |
a4aaf855 |
48 | |
49 | if [ ! -x $JQ ]; then |
50 | echo "jq not found in your PATH" |
51 | echo "install jq to continue" |
52 | exit 1 |
53 | fi |
54 | |
53f2ed57 |
55 | # IMPORT USER DEFINED OPTIONS IF ANY |
56 | if [[ -f $RCFILE ]]; then |
57 | source $RCFILE |
58 | fi |
59 | |
a4aaf855 |
60 | # We prevent the program from running more than one instance: |
61 | PIDFILE=/var/tmp/$(basename $0 .sh).pid |
62 | |
63 | # Make sure the PID file is removed when we kill the process |
64 | trap 'rm -f $PIDFILE; exit 1' TERM INT |
65 | |
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 |
71 | rm $PIDFILE |
72 | fi |
73 | |
74 | # create PIDFILE |
75 | echo $PID > $PIDFILE |
76 | |
61c91990 |
77 | # check if input is a number, returns false or the number itself |
78 | function check_noteID() { |
79 | IN=$1 |
80 | case $IN in |
81 | ''|*[!0-9]*) |
82 | return 1 |
83 | ;; |
84 | *) |
85 | echo $IN |
86 | ;; |
87 | esac |
88 | } |
89 | |
a4aaf855 |
90 | function helptext() { |
d80ac20a |
91 | echo "Usage:" |
c018122c |
92 | echo " $0 [PARAMS] ..." |
d80ac20a |
93 | echo "" |
94 | cat << __NOWCONF__ |
95 | ${BASENAME} configuration is: |
96 | |
97 | base directory: ${BASEDIR}/ |
98 | notes archive: ${NOTESDIR}/ |
99 | notes database: ${DB} |
100 | rc file: $RCFILE |
cb8fcb2f |
101 | debug file: /tmp/debug_bash-note.log |
c018122c |
102 | |
d80ac20a |
103 | text editor: ${EDITOR} |
104 | terminal: ${TERMINAL} |
105 | jq executable: ${JQ} |
106 | __NOWCONF__ |
107 | |
108 | echo "" |
e3670e83 |
109 | echo "${BASENAME} parameters are:" |
d80ac20a |
110 | echo " -h | --help : This help text" |
c018122c |
111 | echo " -p | --plain : Output is in plain text" |
b648c006 |
112 | echo " (without this option the output is formatted)" |
113 | echo " (this option must precede all others)" |
d80ac20a |
114 | echo " -l | --list : List existing notes" |
026502da |
115 | echo " -a | --add [\"<title>\"] : Add new note" |
6c152f7e |
116 | echo " -e | --edit [<note>] : Edit note" |
026502da |
117 | echo " -d | --delete [<note> | all] : Delete single note or all notes at once" |
d80ac20a |
118 | echo " -v | --version : Print version" |
119 | echo " --userconf : Export User config file" |
e3670e83 |
120 | echo "" |
a4aaf855 |
121 | } |
122 | |
123 | function addnote() { |
026502da |
124 | # remove eventually existing temp DB file |
125 | if [[ -f $TMPDB ]]; then |
126 | rm $TMPDB |
127 | fi |
128 | |
53f2ed57 |
129 | NOTETITLE="$1" |
130 | echo "adding new note - \"$NOTETITLE\"" |
e3670e83 |
131 | LASTID=$($JQ '.notes[-1].id // 0 | tonumber' $DB) |
132 | # [ "" == $LASTID ] && LASTID=0 |
a4aaf855 |
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 |
136 | mv $TMPDB $DB |
b648c006 |
137 | # example for alacritty: |
138 | # alacritty --class notes --title notes -e /usr/bin/vim ... |
e3670e83 |
139 | $(${TERMINAL} ${TERM_OPTS} ${EDITOR} ${NOTESDIR}/${NOW}) |
a4aaf855 |
140 | } |
141 | |
142 | function listnotes() { |
e3670e83 |
143 | # [ $PLAIN == true ] && echo "output is plain text" || echo "output is colored" |
144 | if [[ $(ls -A $NOTESDIR) ]]; then |
b648c006 |
145 | if [ $PLAIN == false ]; then |
146 | echo "listing all notes" |
147 | echo "" |
148 | fi |
149 | [ $PLAIN == false ] && echo "[ID] [TITLE] [CREATED]" |
e3670e83 |
150 | for i in ${NOTESDIR}/*; do |
b648c006 |
151 | local fname=$(basename $i) |
152 | DATE=$(date -d @${fname} +"%d/%m/%Y %R %z%Z") |
e3670e83 |
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) |
b648c006 |
155 | [ $PLAIN == false ] && echo "[${ID}] ${TITLE} ${DATE}" || echo "${ID} - ${TITLE} - ${DATE}" |
e3670e83 |
156 | done |
157 | else |
158 | echo "no notes yet. You can add your first one with: ${BASENAME} -a \"your note title\"" |
159 | fi |
a4aaf855 |
160 | } |
161 | |
162 | function editnote() { |
61c91990 |
163 | NOTE=$1 |
164 | local OK=$(check_noteID $NOTE) |
165 | if [ ! $OK ]; then |
166 | echo "invalid note \"$NOTE\"" |
167 | exit 1 |
168 | fi |
169 | |
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) |
44abbfe7 |
172 | if [ "$TITLE" ]; then |
173 | echo "editing note $TITLE" |
b648c006 |
174 | $(${TERMINAL} ${TERM_OPTS} ${EDITOR} ${NOTESDIR}/${FILE}) |
44abbfe7 |
175 | else |
176 | echo "note not found" |
177 | exit 1 |
178 | fi |
a4aaf855 |
179 | } |
180 | |
a4aaf855 |
181 | function rmnote() { |
026502da |
182 | # remove eventually existing temp DB file |
183 | if [[ -f $TMPDB ]]; then |
184 | rm $TMPDB |
b648c006 |
185 | fi |
186 | |
026502da |
187 | NOTE=$1 |
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 |
191 | case $ANSWER in |
192 | y|Y ) |
193 | $JQ 'del(.notes[])' $DB > $TMPDB |
194 | mv $TMPDB $DB |
195 | rm $NOTESDIR/* |
196 | echo "Deleted all notes" |
197 | ;; |
198 | * ) |
199 | echo "Aborting, no notes were deleted." |
200 | exit 1 |
201 | ;; |
202 | esac |
b648c006 |
203 | else |
026502da |
204 | local OK=$(check_noteID $NOTE) |
205 | if [ ! $OK ]; then |
206 | echo "invalid note \"$NOTE\"" |
207 | exit 1 |
208 | fi |
209 | |
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 |
214 | mv $TMPDB $DB |
215 | rm $NOTESDIR/$FILE |
216 | echo "Deleted note $TITLE" |
217 | else |
218 | echo "note not found" |
219 | exit 1 |
220 | fi |
b648c006 |
221 | fi |
a4aaf855 |
222 | } |
223 | |
d80ac20a |
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) |
228 | fi |
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/' \ |
233 | > ${RCFILE} |
234 | if [ -r ${RCFILE} ]; then |
235 | echo "Taking no further action." |
236 | exit 0 |
237 | else |
238 | echo "Could not write '${RCFILE}'...!" |
239 | exit 1 |
240 | fi |
241 | } |
242 | |
53f2ed57 |
243 | # we should expand on this function to add a sample note and explain a little bit |
244 | # how the program works. |
a4aaf855 |
245 | function firstrun() { |
53f2ed57 |
246 | [ -f $RCFILE ] && RC=$RCFILE || RC="none" |
247 | |
248 | clear |
249 | echo "${BASENAME} configuration: |
250 | |
251 | base directory: ${BASEDIR}/ |
252 | notes archive: ${NOTESDIR}/ |
253 | notes database: ${DB} |
254 | rc file: $RC |
255 | text editor: ${EDITOR} |
256 | terminal: ${TERMINAL} |
257 | jq executable: ${JQ} |
258 | " |
259 | |
260 | read -r -p "Do you wish to continue? (y/N) " ANSWER |
261 | case $ANSWER in |
262 | y|Y ) |
263 | mkdir -p $NOTESDIR |
d80ac20a |
264 | cat << __EOL__ > ${DB} |
a4aaf855 |
265 | { |
d80ac20a |
266 | "params": { |
267 | "version": "${VERSION}", |
268 | "dbversion": "${NOW}" |
269 | }, |
a4aaf855 |
270 | "notes": [] |
271 | } |
272 | __EOL__ |
d80ac20a |
273 | echo; echo "All done, you can now write your first note." |
53f2ed57 |
274 | ;; |
275 | * ) |
276 | echo "No changes made. Exiting" |
277 | exit |
278 | ;; |
279 | esac |
a4aaf855 |
280 | } |
281 | |
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? |
285 | firstrun |
286 | fi |
287 | |
53f2ed57 |
288 | # NOTE: This requires GNU getopt. On Mac OS X and FreeBSD, you have to install this |
289 | # separately; see below. |
6c152f7e |
290 | GOPT=`getopt -o hvpla:e:d: --long help,version,list,plain,userconf,add:,edit:,delete:,editor:,storage: \ |
53f2ed57 |
291 | -n 'bash-notes' -- "$@"` |
292 | |
cb8fcb2f |
293 | if [ $? != 0 ] ; then helptext >&2 ; exit 1 ; fi |
53f2ed57 |
294 | |
295 | # Note the quotes around `$GOPT': they are essential! |
296 | eval set -- "$GOPT" |
297 | |
298 | while true; do |
299 | case "$1" in |
300 | -h | --help ) |
301 | helptext |
302 | exit |
303 | ;; |
304 | -v | --version ) |
305 | echo $BASENAME v${VERSION} |
306 | exit |
307 | ;; |
c018122c |
308 | -p | --plain ) |
309 | PLAIN=true |
310 | shift |
311 | ;; |
53f2ed57 |
312 | -l | --list ) |
313 | listnotes |
314 | exit |
315 | ;; |
316 | -a | --add ) |
317 | TITLE="$2" |
318 | shift 2 |
319 | addnote "$TITLE" |
320 | ;; |
6c152f7e |
321 | -e | --edit ) |
53f2ed57 |
322 | NOTE="$2" |
323 | shift 2 |
324 | editnote "$NOTE" |
325 | ;; |
b648c006 |
326 | -d | --delete ) |
53f2ed57 |
327 | NOTE="$2" |
328 | shift 2 |
329 | rmnote "$NOTE" |
330 | ;; |
d80ac20a |
331 | --userconf ) |
332 | export_config |
333 | echo "config exported to \"$RCFILE\"" |
334 | exit |
53f2ed57 |
335 | ;; |
336 | -- ) |
026502da |
337 | shift |
338 | break |
53f2ed57 |
339 | ;; |
340 | * ) |
341 | break |
342 | ;; |
343 | esac |
a4aaf855 |
344 | done |
345 | |