summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFlorian Pritz <bluewind@xinu.at>2011-10-23 21:49:45 +0200
committerFlorian Pritz <bluewind@xinu.at>2011-10-23 21:49:45 +0200
commit77b7e116e2dff776c87313a4e095bcc02ed3e4f5 (patch)
treee12da87d2a7cc38d8948b75ff8afec097fd6f45c
parent5be4d3d391d153aa86037eddea66573485b5a4f0 (diff)
downloadbin-77b7e116e2dff776c87313a4e095bcc02ed3e4f5.tar.gz
bin-77b7e116e2dff776c87313a4e095bcc02ed3e4f5.tar.xz
add xo
Signed-off-by: Florian Pritz <bluewind@xinu.at>
-rwxr-xr-xxo61
1 files changed, 61 insertions, 0 deletions
diff --git a/xo b/xo
new file mode 100755
index 0000000..28f6e89
--- /dev/null
+++ b/xo
@@ -0,0 +1,61 @@
+#!/bin/bash
+#
+# A command line utility that mimics xdg-open
+# Requires Bash 4.0+
+#
+
+# Arg check
+(( $# )) || { printf "Usage: %s <file>\n" "${0##*/}" >&2; exit 1; }
+
+# Declare an associative array to keep track of our file types.
+# Index elements can be a full MIME type (e.g. image/png), just
+# the major MIME type (e.g. image) or a file extension (png).
+declare -A handler
+
+# To keep things clean, general programs should be declared for
+# groups of filetypes resulting in the same program being used
+# when a major MIME type won't correctly identify all filetypes.
+# openoffice.org documents are an example of this.
+doc=soffice
+image=gpicview
+video=mplayer
+default=${EDITOR:-vi} # Fallback -- should be a text editor
+
+handler[application/pdf]=evince
+handler[application/vnd.oasis.opendocument.text]=$doc
+handler[application/xml]=firefox
+handler[doc]=$doc
+handler[image]=$image
+handler[odb]=$doc
+handler[odf]=$doc
+handler[ods]=$doc
+handler[text/rtf]=$doc
+handler[video]=$video
+handler[mkv]=$video
+handler[xls]=$doc
+
+# Determine the MIME type via 'file' and assign it to an array
+# mimetype[0] = major (e.g. image)
+# mimetype[1] = minor (e.g. png)
+IFS='/' read -r -d ';' -a mimetype < <(file -bi "$1")
+
+# Determine the extension as a fallback method
+ext=${1//*.}
+
+# Try to open by exact MIME type
+if [[ -n ${handler[${mimetype[0]}/${mimetype[1]}]} ]]; then
+ ${handler[${mimetype[0]}/${mimetype[1]}]} "$@"
+
+# Try to open by major MIME type
+elif [[ -n ${handler[${mimetype[0]}]} ]]; then
+ ${handler[${mimetype[0]}]} "$@"
+
+# Try to open by extension
+elif [[ -n ${handler[$ext]} ]]; then
+ ${handler[$ext]} "$@"
+
+# Well, I'm out of ideas. Use the $default.
+else
+ $default "$@"
+fi
+