summary refs log tree commit diff
path: root/bin/urlopen
blob: 2b0479c172275728ffcd2922bbe1f0691d140c21 (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
#!/bin/bash
set -eu

# shellcheck disable=2034
description="url handler"

FUZL_PROMPT=urlopen
. libfuzl.sh

main() {
	case "$#" in
	1)
		fuzl_action_add "open" "" "open"
		fuzl_action_add "ask" "󰮫" "ask"
		fuzl_action_add "clipboard" "󰅌" "clipboard"
		fuzl_action_add "primary" "󱊁" "primary"
		fuzl_action_menu "$(get_text "$*")"
		;;
	2)
		local action

		action=${1:?missing action}
		shift
		eval "urlopen_$action" "'$(get_text "$*")'"
		;;
	esac
}

#
# helpers
#

get_text() {
	local text="$*"
	[ -z "$text" ] && read -r text
	printf '%s' "$text"
}

get_mimetype() {
	local text=${1:?missing url}
	case "$text" in
	http*) printf %s 'x-scheme-handler/http' ;;
	*) file -bdE --mime-type "$text" ;;
	esac
}
open_default() {
	local text=${1:?missing url}
	local application
	case "$(get_mimetype "$text")" in
	x-scheme-handler/http*) application=librewolf ;;
	esac
	case "$text" in
	*linear.app*) application=linear ;;
	*) application=librewolf ;;
	esac
	if [ -z "$application" ]; then
		gio open "$text"
	else
		desktop_file=$(rg -Lil -e "$application" -- /usr/share/applications/ /var/lib/flatpak/exports/share/applications/ ~/.local/share/applications/ | tail -1)
		gio launch "$desktop_file" "$text"
	fi
}
#
# actions
#

urlopen_ask() {
	local text=${1:?missing url}
	env porta open-uri "$text" ask
}

urlopen_open() {
	local text=${1:?missing url}
	open_default "$text"
}

urlopen_clipboard() {
	local text=${1:?missing url}
	wl-copy -n "$text"
	# fuzl_notification "copied to clipboard" "$text"
}

urlopen_primary() {
	local text=${1:?missing url}
	wl-copy -np "$text"
	# fuzl_notification "copied to primary" "$text"
}

main $@