summary refs log tree commit diff
path: root/bin/urlopen
diff options
context:
space:
mode:
Diffstat (limited to 'bin/urlopen')
-rwxr-xr-xbin/urlopen89
1 files changed, 89 insertions, 0 deletions
diff --git a/bin/urlopen b/bin/urlopen
new file mode 100755
index 0000000..2b0479c
--- /dev/null
+++ b/bin/urlopen
@@ -0,0 +1,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 $@