about summary refs log tree commit diff stats
path: root/bin/opener
diff options
context:
space:
mode:
Diffstat (limited to 'bin/opener')
-rwxr-xr-xbin/opener41
1 files changed, 41 insertions, 0 deletions
diff --git a/bin/opener b/bin/opener
new file mode 100755
index 0000000..755da51
--- /dev/null
+++ b/bin/opener
@@ -0,0 +1,41 @@
+#!/bin/sh
+# opener: opens files according to its contents
+# takes one path as its argument, launches the appropriate program for it
+if ! [ -f "$1" ]; then
+	printf 'file %s does not exist!\n' "$1" >/dev/stderr
+	return 1
+fi
+FILEPATH="$1"
+FILEMIME="$(file -ib "$FILEPATH")"
+echo() {
+	printf '%s\n' "$*"
+}
+case "$FILEMIME" in
+	#ebooks
+	application/epub*|application/pdf|application/postscript|image/vnd.djvu)
+		if [ -n "$READER" ]; then
+			$READER "$FILEPATH">/dev/null 2>&1 &
+		else
+			zathura "$FILEPATH" &
+		fi ;;
+	#videos
+	video/*)
+		mpv --input-ipc-server=/tmp/mpv-socket$(date +%s) --quiet "$FILEPATH" >/dev/null 2>&1 & ;;
+	#images
+	image/*)
+		sxiv -a "$FILEPATH">/dev/null 2>&1 & ;;
+	#audio
+	audio/*)
+		mpv --input-ipc-server=/tmp/mpv-socket$(date +%s) "$FILEPATH" >/dev/null 2>&1 & ;;
+	#text
+	text/*)
+		if [ -n "$EDITOR" ]; then
+			$EDITOR "$FILEPATH">/dev/null 2>&1 &
+		else
+			vi "$FILEPATH" &
+		fi ;;
+	#catchall
+	*)
+		echo "file $(basename "$FILEPATH") could not be opened. its type is $FILEMIME, go tell ensa that opener didn't work" >/dev/stderr
+		return 1 ;;
+esac