aboutsummaryrefslogtreecommitdiffstats
path: root/SOURCE/CORE/core-add.sh
blob: 3404f714a0d000a3a90685162f4bbd6ab3b78b47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
addnote() {
	# attempt syncing before adding a note
	gitsync -f
	# remove eventually existing temp DB file
	if [[ -f $TMPDB ]]; then
		rm $TMPDB
	fi

	# RANDOM TITLE
	RTITLE=$(random_title)

	if [[ -z $1 ]]; then
		read -r -p "Title: " TITLE
		case "$TITLE" in
			'' )
				NOTETITLE="$RTITLE"
				;;
			* )
				NOTETITLE=$TITLE
				;;
		esac
	fi

	# [[ -z "$1" ]] && NOTETITLE="$RTITLE" || NOTETITLE="$1"
	echo "adding new note - \"$NOTETITLE\""
	# shellcheck disable=SC2086
	LASTID=$($JQ '.notes[-1].id // 0 | tonumber' $DB)
	# [ "" == $LASTID ] && LASTID=0
	NOTEID=$(( LASTID + 1 ))
	# shellcheck disable=SC2086
	touch ${NOTESDIR}/${NOW}
	# shellcheck disable=SC2016
	$JQ --arg i "$NOTEID" --arg t "$NOTETITLE" --arg f "$NOW" '.notes += [{"id": $i, "title": $t, "file": $f}]' "$DB" > $TMPDB
	# shellcheck disable=SC2086
	mv $TMPDB $DB
	# example for alacritty:
	# alacritty --class notes --title notes -e /usr/bin/vim ...
	# shellcheck disable=SC2086,SC2091
	$(${TERMINAL} ${TERM_OPTS} ${EDITOR} ${NOTESDIR}/${NOW})
	# add note to git and push to remote
	gitadd
}