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