adding notes now pushes changes to remote
[bash-notes.git] / SOURCE / CORE / core-add.sh
1 addnote() {
2 # attempt syncing before adding a note
3 gitsync
4 # remove eventually existing temp DB file
5 if [[ -f $TMPDB ]]; then
6 rm $TMPDB
7 fi
8
9 RTITLE=$(random_title)
10 [[ -z "$1" ]] && NOTETITLE="$RTITLE" || NOTETITLE="$1"
11 echo "adding new note - \"$NOTETITLE\""
12 # shellcheck disable=SC2086
13 LASTID=$($JQ '.notes[-1].id // 0 | tonumber' $DB)
14 # [ "" == $LASTID ] && LASTID=0
15 NOTEID=$(( LASTID + 1 ))
16 # shellcheck disable=SC2086
17 touch ${NOTESDIR}/${NOW}
18 # shellcheck disable=SC2016
19 $JQ --arg i "$NOTEID" --arg t "$NOTETITLE" --arg f "$NOW" '.notes += [{"id": $i, "title": $t, "file": $f}]' "$DB" > $TMPDB
20 # shellcheck disable=SC2086
21 mv $TMPDB $DB
22 # example for alacritty:
23 # alacritty --class notes --title notes -e /usr/bin/vim ...
24 # shellcheck disable=SC2086,SC2091
25 $(${TERMINAL} ${TERM_OPTS} ${EDITOR} ${NOTESDIR}/${NOW})
26 # add note to git and push to remote
27 gitadd
28 }