summaryrefslogtreecommitdiffstats
path: root/.local/bin/notes
blob: 814c0216ecb5265a76a607ba30d4631c7882d097 (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
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/bash
set -Eeuo pipefail

_srealpath() {
	local path=$1
	realpath "$path" 2>/dev/null && return
	printf '%s\n' "$path"
}

_err_handler() {
	local status=$?
	local cmd=$BASH_COMMAND
	local line=${BASH_LINENO[0]}
	local err_log err_msg
	local temp

	err_log=$({
		printf "error: command failed\n\n"
		printf '\t%-10s: %s\n' \
			"at" "$(_srealpath "${BASH_SOURCE[1]}"):$line" \
			"command" "$cmd" \
			"status" "$status"
		printf '\nstack:\n\n'
		local i
		for ((i = 1; i < ${#FUNCNAME[@]}; ++i)); do
			printf '\t%s at %s:%s\n' \
				"${FUNCNAME[i]}" \
				"$(_srealpath "${BASH_SOURCE[i]}")" \
				"${BASH_LINENO[$((i - 1))]}"
		done
	})
	if temp=$(mktemp --suffix .log /tmp/notes.XXXXXXXXXX) &&
		printf '%s\n' "$err_log" >"$temp"; then
		err_msg="Log saved to $temp"
	else
		err_msg="Could not create temp file for log"
	fi
	notify-send --expire-time 0 "Failed to open notes" "$err_msg"
}

trap _err_handler ERR

################################################################################

main() {
	local app_name=notes
	[[ -d $NOTES_DIR ]]
	cd "$NOTES_DIR"
	exec foot --app-id "$app_name" --title "$app_name" -- \
		tmux new-session -A -D -s "$app_name" -- \
		nvim +NotesFindTags
}

if [[ ${BASH_SOURCE[0]} == "$0" ]]; then main; fi