commit 05f10372469fafdbb8c66f4446146688ce093068
parent 8e160885714395bfc4ba5f6bc2f1745bb7085abb
Author: Anders Damsgaard <anders@adamsgaard.dk>
Date:   Mon, 30 Sep 2019 16:01:42 +0200
Add custom xdg-open
Diffstat:
| A | .local/bin/xdg-open | | | 99 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | 
1 file changed, 99 insertions(+), 0 deletions(-)
diff --git a/.local/bin/xdg-open b/.local/bin/xdg-open
@@ -0,0 +1,99 @@
+#!/bin/sh
+set -e
+
+version=0.1
+
+show_help() {
+	echo "usage: ${0##*/} [OPTIONS] [FILE|URL]"
+	echo "will open FILE or URL"
+	echo "If no FILE or URL is specified, this program will expect them as stdin."
+	echo
+	echo "OPTIONS are one or more of the following:"
+	echo "   -h, --help          show this message"
+	echo "   -v, --version       show version and license information"
+	echo "   -V, --verbose       show verbose information"
+	echo "   --                  do not consider any following args as options"
+}
+
+show_version() {
+	echo "${0##*/} version $version"
+	echo "Licensed under the GNU Public License, v3+"
+	echo "written by Anders Damsgaard, anders@adamsgaard.dk"
+	echo "https://src.adamsgaard.dk/dotfiles"
+}
+
+die() {
+	printf '%s\n' "$1" >&2
+	exit 1
+}
+
+handle_target() {
+	if [ -e "$1" ]; then
+		mime_type="$(file -ib "$1")"
+		case "$mime_type" in
+			image/*)
+				sxiv "$1" >/dev/null 2>&1 &
+				return 0;;
+			video/*)
+				mpv "$1" >/dev/null 2>&1 &
+				return 0;;
+			text/html)
+				$BROWSER "$1" >/dev/null 2>&1 &
+				return 0;;
+			text/*)
+				$EDITOR "$1"
+				return 0;;
+			application/*html*)
+				$BROWSER "$1" >/dev/null 2>&1 &
+				return 0;;
+			application/pdf)
+				zathura "$1" >/dev/null 2>&1 &
+				return 0;;
+			application/*)
+				libreoffice "$1" >/dev/null 2>&1 &
+				return 0;;
+			*)
+				(>2 printf "File type %s not supported\n" $mime_type)
+				exit 1;;
+		esac
+	else
+		$BROWSER "$1";
+	fi
+}
+
+verbose=0
+while :; do
+	case "$1" in
+		-h|-\?|--help)
+			show_help
+			exit 0
+			;;
+		-v|--version)
+			show_version
+			exit 0
+			;;
+		-V|--verbose)
+			verbose=1
+			;;
+		--) # end all options
+			shift
+			break
+			;;
+		-?*)
+			die 'Error: Unknown option specified'
+			;;
+		*)  # No more options
+			break
+	esac
+	shift
+done
+
+if [ $# -lt 1 ]; then
+	target="$(cat)"
+	handle_target "$target"
+else
+	for f in "$@"; do
+		handle_target "$f"
+	done
+fi
+