aboutsummaryrefslogtreecommitdiff
path: root/damb
blob: e8f9d48362ed7098ab2d001c6f3a935088bc0afd (plain) (blame)
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#!/bin/sh

damb_conf="${XDG_CONFIG_HOME:-$HOME/.config}/damb/damb.conf"
damb_data="${XDG_DATA_HOME:-$HOME/.local/share}/damb"
damb_pids="${XDG_RUNTIME_DIR:-${TMPDIR:-/tmp}}/damb.pids"
damb_dlim="|"

w_menu="wmenu"
x_menu="dmenu"

ico_damb="󰎆 "
ico_locl="󱝟 "
ico_ytbe=" "
ico_sndc=" "
ico_gurl="󰌹 "
ico_what=" "

usage()
{
	cat <<- EOF
	Usage: damb [ command ] [ option ]
	a bookmark manager using dmenu
	Commands:
	  help	 show this help cruft
	  save	 <key> <value>, save a new key
	  rm	 <key>, remove a key

	EOF
}

die()
{
	: "${1:?}"

	command -v notify-send > /dev/null &&
		notify-send "${ico_damb}  damb" "$1"

	printf "\033[31;1merr: %b\033[0m\n" "$1"
	exit "${2:-1}"
}

dep_check()
{
	: "${1:?}"

	for dep; do
		command -v "$dep" 1>/dev/null ||
			die "$dep not found, please install it" 127
	done

	unset dep
}

trim()
{
	: "${1:?}"

	_trimstr="${1#"${1%%[![:space:]]*}"}"
	_trimstr="${_trimstr%"${_trimstr##*[![:space:]]}"}"

	echo "$_trimstr"
}

gen_ico()
{
	case "$1" in
	https://www.youtube.com/*|https://youtu.be/*)
		echo "$ico_ytbe" ;;
	https://soundcloud.com/*)
		echo "$ico_sndc" ;;
	http*)
		echo "$ico_gurl" ;;
	/*)
		echo "$ico_locl" ;;
	*)
		echo "$ico_what" ;;
	esac
}

parse_keys()
{
	# usage parse_data [ getval <key> ]
	line=
	_key=
	_value=

	[ -s "$damb_conf" ] || return 1

	while read -r line
	do
		[ -z "$line" ] && continue

		_key="${line%%"$damb_dlim"*}"
		_key="$(trim "$_key")"

		case "$_key" in
		\#*) continue
		esac

		if [ "$1" = "getval" ]; then
			[ "$2" != "$_key" ] && continue

			_value="${line##*"$damb_dlim"}"
			_value="$(trim "$_value")"
			echo "$_value"
			return 0
		else
			echo "$_key"
		fi
	done < "$damb_conf"

	unset _key _value
	[ "$1" = "getval" ] && return 1
}

rm_key()
{
	# usage rm_data <key>
	: "${1:?}"
	cl=
	line=
	file=
	value=
	old_ifs="$IFS"

	IFS=
	while read -r line
	do
		cl="$line\n"

		key="$(trim "${cl%%"$damb_dlim"*}")"
		# catch match
		if [ "$key" = "$1" ]; then
			value="$(trim "${cl##*"$damb_dlim"}")"

			# delete if saved
			case "$value" in
			"$damb_data"*) rm -f "$value"
			esac
			continue
		fi

		file="${file}${cl}"
	done < "$damb_conf"

	IFS="$old_ifs"
	# shellcheck disable=SC2059
	printf "$file" > "$damb_conf"
	unset cl line file value old_ifs
}

verify_val()
{
	# verify_val <value>

	case "$1" in
	*"\n"*) return 1 ;;
	*$damb_dlim*) return 1 ;;
	esac

	case "$(file --brief --dereference --mime-type "$1")" in
	audio/*|video/*) ;;
	*)
		echo "$1" | grep -qxE '^(https?://)?[^/]*\.[A-Za-z0-9]*(/.*)?' ||
			return 1
	esac
}

write_kv()
{
	# verify_val <key> <value>
	: "${1:?}"
	: "${2:?}"

	key="$(echo "$1" | tr -d "$damb_dlim")"
	key="$(trim "$key")"
	val="$(trim "$2")"

	case "$key" in
	[[:print:]]*) key="$(gen_ico "$val") ${key}" ;;
	esac

	parse_keys getval "$key" > /dev/null &&
		die "keys must be unique"
	verify_val "$val" ||
		die "invalid: $val"

	echo "${key:?} $damb_dlim $val" >> "$damb_conf"
}

kill_ambs()
{
	while read -r pid; do
		kill -9 "$pid"
	done < "$damb_pids"

	rm "$damb_pids"
}

play_amb()
{
	: "${1:?}"

	mpv --no-video --no-cache --no-resume-playback "$@" &
	echo "$!" >> "$damb_pids"
}

set_menu()
{
	if [ -n "$WAYLAND_DISPLAY" ]; then
		menu="$w_menu"
	elif [ -n "$DISPLAY" ]; then
		menu="$x_menu"
	else
		die "tty not supported"
	fi

	dep_check "$menu"
}

set_clip()
{
	if [ -n "$WAYLAND_DISPLAY" ]; then
		dep_check "wl-paste"
		_clip="$(wl-paste -n)"
	elif [ -n "$DISPLAY" ]; then
		dep_check "xclip"
		_clip="$(xclip -o)"
	fi

	verify_val "$_clip"
}

gen_opts()
{

	[ -s "$damb_pids" ] && while read -r pid; do
		if [ -d "/proc/${pid}" ]; then
			echo "  stop the ambiances"
			break;
		fi
	done < "$damb_pids"

	set_clip &&
		echo "$(gen_ico "$_clip") $_clip (from clipboard)"

	parse_keys
}

gen_menu()
{
	[ -s "$damb_conf" ] || die "empty, try damb save"
	key="$(gen_opts | wmenu -l 25)" || return 1

	case "$key" in
	*"stop the ambiances")
		kill_ambs ;;
	*"from clipboard)")
		set_clip
		play_amb "$_clip" ;;
	*)
		play_amb --loop "$(parse_keys getval "$key")" ;;
	esac
}

########
# MAIN #
########

[ -d "$damb_data" ] || mkdir -p "$damb_data"
[ -d "${damb_conf%/*}" ] || mkdir -p "${damb_conf%/*}"

set_menu
dep_check mpv

case "$1" in
rm)
	key="$(parse_keys | "$menu" -l 25)" || exit 1
	rm_key "$key"
	;;
save)
	[ "$#" -ne 3 ] &&
		die "invalid usage, try damb help"

	if [ -f "$3" ]; then
		cp "$3" "$damb_data"
		val="${damb_data}/${3##*/}"
	fi

	write_kv "$2" "${val:-$3}"
	;;
"")
	gen_menu
	;;
help)
	usage
	;;
*)
	die "invalid usage $*, try damb help"
	;;
esac