plumber/plumb

101 lines
2.4 KiB
Text
Raw Normal View History

2022-10-31 19:42:15 +02:00
#!/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}"