diff options
| author | Přemysl Eric Janouch <p@janouch.name> | 2025-11-20 20:49:14 +0100 |
|---|---|---|
| committer | Přemysl Eric Janouch <p@janouch.name> | 2025-11-20 21:16:30 +0100 |
| commit | 3e39cc5660e58ea868608ed396abe2db25611b78 (patch) | |
| tree | 77d3ba093b43bc245aae6e4710f74de8382ed8f1 /sdn-open | |
| parent | 977d1a7120ae66ba40abb770bb00e99bc23d6b0b (diff) | |
| download | sdn-3e39cc5660e58ea868608ed396abe2db25611b78.tar.gz sdn-3e39cc5660e58ea868608ed396abe2db25611b78.tar.xz sdn-3e39cc5660e58ea868608ed396abe2db25611b78.zip | |
Add and integrate sdn-open
Originally I thought that not supporting %cd would be an issue,
making this kind of utility unclean.
It turns out the desire to launch xdg-open quickly is stronger.
Diffstat (limited to 'sdn-open')
| -rwxr-xr-x | sdn-open | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/sdn-open b/sdn-open new file mode 100755 index 0000000..7252488 --- /dev/null +++ b/sdn-open @@ -0,0 +1,52 @@ +#!/bin/sh -e +# sdn-open: an opener for sdn that makes use of Midnight Commander configuration +# to make more kinds of files directly openable + +if [ "$#" -ne 1 ] +then + echo "Usage: $0 FILE" >&2 + exit 2 +fi + +# This handles both MC_DATADIR and odd installation locations. +datadir= +if command -v mc >/dev/null +then datadir=$(mc --datadir | sed 's/ (.*)$//') +fi + +config= +for dir in "$HOME"/.config/mc "$datadir" /etc/mc +do + if [ -n "$dir" -a -f "$dir/mc.ext.ini" ] + then + config=$dir/mc.ext.ini + break + fi +done + +# This is often used in %env{} expansion, so let's be on the same page. +export PAGER=${PAGER:-less} + +export MC_EXT_FILENAME=$(realpath "$1") +export MC_EXT_BASENAME=$(basename "$1") +export MC_EXT_CURRENTDIR=$(dirname "$MC_EXT_FILENAME") +output=$(sdn-mc-ext <"$config" "$(file -Lbz "$1")" \ + "$MC_EXT_FILENAME" "$MC_EXT_BASENAME" "$MC_EXT_CURRENTDIR" Open || :) +kind=$(echo "$output" | sed -n 1p) +command=$(echo "$output" | sed -n 2p) + +case "$kind" in +cd) + # These mostly enter virtual filesystems, which we do not understand. + xdg-open "$MC_EXT_FILENAME" + ;; +'') + if [ -n "$command" ] + then eval "$command" + else xdg-open "$MC_EXT_FILENAME" + fi + ;; +*) + echo "Unsupported: $kind" >&2 + exit 1 +esac |
