aboutsummaryrefslogtreecommitdiff
path: root/.local/bin/bookmarks
blob: cd8462bb5fbf0b8146c9612c35ac053d3d715803 (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/bin/dash

URLQUERY_FILE="${HOME}/.local/share/urlquery"
ACTION_MENU="@@"

CLIPBOARD() {
	wl-paste
}

DMENU() {
	wmenu -i -l "${1}" -p "${2}"
}

error_notify() {
	notify-send "${1}"
	exit "1"
}

ensure_file_exists() {
	[ -f "${URLQUERY_FILE}" ] || {
		notify-send "${URLQUERY_FILE} does not exist. Creating it now."
		printf "SearXNG=https://searx.tiekoetter.com/search?q=\n" > "${URLQUERY_FILE}"
	}
}

get_selection() {
	cut -d= -f1 "${URLQUERY_FILE}" | DMENU "${LINE_COUNT}" "Bookmarks"
}

update_file() {
	pattern="${1}"
	replacement="${2}"

	sed "/${pattern}/c\\${replacement}" "${URLQUERY_FILE}" > "${URLQUERY_FILE}.tmp" &&
			mv "${URLQUERY_FILE}.tmp" "${URLQUERY_FILE}" ||
				error_notify "Failed to update the file."
}

is_valid_url() {
	printf "%s\n" "${1}" | grep -qE "^https?://[^[:space:]/?#][^[:space:]]+$"
}

add_bookmark() {
	URL="$(CLIPBOARD)"

	is_valid_url "${URL}" || error_notify "The clipboard content is not a valid URL."

	grep -q "=${URL}$" "${URLQUERY_FILE}" &&
		notify-send "The URL is already in the list." && return

	NAME="$(printf "" | DMENU "0" "Name")"

	[ -n "${NAME}" ] && printf "%s\n" "${NAME}=${URL}" >> "${URLQUERY_FILE}" &&
		notify-send "'${NAME}' is bookmarked."
}

delete_bookmark() {
	NAME="$(get_selection)"

	[ -z "${NAME}" ] && error_notify "Failed to delete the bookmark." && return

	sed "/^${NAME}=/d" "${URLQUERY_FILE}" > "${URLQUERY_FILE}.tmp"
	mv "${URLQUERY_FILE}.tmp" "${URLQUERY_FILE}"

	[ -s "${URLQUERY_FILE}" ] && grep -qE "\S" "${URLQUERY_FILE}" || rm "${URLQUERY_FILE}"

	notify-send "'${NAME}' is deleted."
}

edit_name() {
	OLD_NAME="${1}"
	NEW_NAME="$(printf "" | DMENU "0" "New Name")"

	[ -z "${NEW_NAME}" ] && return

	URL="$(grep "^${OLD_NAME}=" "${URLQUERY_FILE}" | cut -d= -f2)"

	update_file "^${OLD_NAME}=" "${NEW_NAME}=${URL}"
}

edit_url() {
	NAME="${1}"
	NEW_URL="$(echo "" | DMENU "0" "New URL")"

	[ -z "${NEW_URL}" ] && return

	update_file "^${NAME}=.*" "${NAME}=${NEW_URL}"
}

edit_bookmark() {
	NAME="$(get_selection)"

	[ -z "${NAME}" ] && error_notify "Failed to edit the bookmark." && return

	FIELD="$(printf "Name\nURL\n" | DMENU "2" "Edit")"

	case "${FIELD}" in
		"Name")edit_name "${NAME}";;
		"URL")edit_url "${NAME}"
	esac

	notify-send "'${NAME}' is updated."
}

open_bookmark() {
	URL="$(grep "^${SELECTION}=" "${URLQUERY_FILE}" | cut -d= -f2-)"

	[ -z "${URL}" ] && notify-send "Bookmark not found." && exit "1"

	case "${URL}" in
		*"search"* | *"wiki"* | *"packages"* | *"chatgpt"*) QUERY="$(echo "" | DMENU "0" "Search")"
		URL="${URL}${QUERY}"
		;;
	esac

	"${BROWSER}" "${URL}" || notify-send "Failed to open the URL."
}

ensure_file_exists

LINE_COUNT="$(wc -l < "${URLQUERY_FILE}")"

[ "${LINE_COUNT}" -ge "15" ] && LINE_COUNT="15"

SELECTION="$(get_selection)"

[ -z "${SELECTION}" ] && exit

case "${SELECTION}" in
        "${ACTION_MENU}")
                ACTION="$(printf "Add\nDelete\nEdit\n" | DMENU "3" "Action")"

		case "${ACTION}" in
                        "Add") add_bookmark ;;
                        "Delete") delete_bookmark ;;
                        "Edit") edit_bookmark ;;
                esac
                ;;
        *)
                open_bookmark
                ;;
esac