From 060f4d68a22254300af3b5da0a291f584cf16737 Mon Sep 17 00:00:00 2001 From: hippoz <10706925-hippoz@users.noreply.gitlab.com> Date: Mon, 31 Oct 2022 19:42:15 +0200 Subject: [PATCH] initial commit --- plumb | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100755 plumb diff --git a/plumb b/plumb new file mode 100755 index 0000000..f820241 --- /dev/null +++ b/plumb @@ -0,0 +1,100 @@ +#!/bin/sh + +www() { + exec firefox "$1" +} + +img() { + exec feh "$1" +} + +music() { + exec mpv "$1" +} + +video() { + exec mpv "$1" +} + +pdf() { + exec firefox "$1" +} + +term() { + exec st -e /bin/sh -c "$1 ; exec \"${SHELL}\"" +} + +# https://unix.stackexchange.com/a/4991 +exists_in_path() { + command -v "$1" > /dev/null +} + +resolve_plumb_path() { + case "$1" in + /*) + echo "$1" + return 0 + ;; + *) + target_shell="bash" + + parent_process_children=$(xprop -id $(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2) _NET_WM_PID | cut -d' ' -f3 | xargs pstree -lpA) + temp="${parent_process_children##*${target_shell}(}" + shell_pid="${temp%%)--*}" + shell_pid="${shell_pid%?}" + shell_cwd="$(readlink /proc/${shell_pid}/cwd)" + final_path="${shell_cwd}/$1" + if [ -e "${final_path}" ]; then + echo "${final_path}" + return 0 + else + echo "" + return 1 + fi + ;; + esac +} + +do_plumb() { + case "$1" in + https://*) www "$1" ;; + http://*) www "$1" ;; + *.tif|*.TIF|*.jpg|*.JPG|*.jpeg|*.JPE?|*.gif|*.GIF|*.tiff?|*.TIFF?|*.ppm|*.PPM|*.bit|*.BIT|*.png|*.PNG|*.pgm|*.PGM|*.bmp|*.BMP|*.yuv|*.YUV|*.tga|*.TGA|*.webp|*.WEBP) + img "$(resolve_plumb_path ${1})" + ;; + *.mp3|*.MP3|*.ogg|*.OGG|*.flac|*.FLAC|*.wav|*.WAV|*.au|*.AU|*.mid|*.MID|*.mus|*.MUS|*.m3u|*.M3U|*.pls|*.PLS|*.plist|*.PLIST|*.opus|*.OPUS) + music "$(resolve_plumb_path ${1})" + ;; + *.mp4|*.MP4|*.avi|*.AVI|*.webm|*.WEBM) + video "$(resolve_plumb_path ${1})" + ;; + *.pdf|*.PDF) + pdf "$(resolve_plumb_path ${1})" + ;; + *) + target="${1}" + first=${target%% *} + case "${first}" in + \) | \( | \} | \] | \{ | \[) + term "${target}" + ;; + *) + if exists_in_path "${first}"; then + term "${target}" + else + exit 1 + fi + ;; + esac + ;; + esac +} + +target="$1" + +if [ $# -eq 0 ]; then + # no arguments supplied, use the x clipboard + target="$(xclip -o -)" +fi + +do_plumb "${target}"