reimplemented title dialog when adding note if nothing is passed on the command line.
[bash-notes.git] / SOURCE / CORE / core-add.sh
1 addnote() {
2 # attempt syncing before adding a note
3 gitsync -f
4 # remove eventually existing temp DB file
5 if [[ -f $TMPDB ]]; then
6 rm $TMPDB
7 fi
8
9 # RANDOM TITLE
10 RTITLE=$(random_title)
11
12 if [[ -z $1 ]]; then
13 read -r -p "Title: " TITLE
14 case "$TITLE" in
15 '' )
16 NOTETITLE="$RTITLE"
17 ;;
18 * )
19 NOTETITLE=$TITLE
20 ;;
21 esac
22 fi
23
24 # [[ -z "$1" ]] && NOTETITLE="$RTITLE" || NOTETITLE="$1"
25 echo "adding new note - \"$NOTETITLE\""
26 # shellcheck disable=SC2086
27 LASTID=$($JQ '.notes[-1].id // 0 | tonumber' $DB)
28 # [ "" == $LASTID ] && LASTID=0
29 NOTEID=$(( LASTID + 1 ))
30 # shellcheck disable=SC2086
31 touch ${NOTESDIR}/${NOW}
32 # shellcheck disable=SC2016
33 $JQ --arg i "$NOTEID" --arg t "$NOTETITLE" --arg f "$NOW" '.notes += [{"id": $i, "title": $t, "file": $f}]' "$DB" > $TMPDB
34 # shellcheck disable=SC2086
35 mv $TMPDB $DB
36 # example for alacritty:
37 # alacritty --class notes --title notes -e /usr/bin/vim ...
38 # shellcheck disable=SC2086,SC2091
39 $(${TERMINAL} ${TERM_OPTS} ${EDITOR} ${NOTESDIR}/${NOW})
40 # add note to git and push to remote
41 gitadd
42 }