Add plumb script for opening files

This commit is contained in:
Lucas 2020-04-06 23:51:03 +00:00
parent 2395ee7cbd
commit 43091854f9
3 changed files with 94 additions and 2 deletions

View File

@ -15,7 +15,7 @@
PREFIX = $(HOME)
MANPREFIX = $(PREFIX)/local
BIN = ZZZ browser credentials fetch flac2ogg imgresize invidious \
pstsrv riot-web-ui rfcopen screenshot sekrit tor-browser \
plumb pstsrv riot-web-ui rfcopen screenshot sekrit tor-browser \
w3m-copy-link
MAN1 = sekrit.1

89
bin/plumb.sh Normal file
View File

@ -0,0 +1,89 @@
#!/bin/sh
# o
# Written in 2020 by Lucas
# CC0 1.0 Universal/Public domain - No rights reserved
#
# To the extent possible under law, the author(s) have dedicated all
# copyright and related and neighboring rights to this software to the
# public domain worldwide. This software is distributed without any
# warranty. You should have received a copy of the CC0 Public Domain
# Dedication along with this software. If not, see
# <http://creativecommons.org/publicdomain/zero/1.0/>.
usage()
{
printf "Usage: %s [-T] [URI|file]\n" "${0##*/}">&2
exit 1
}
err()
{
printf "%s: %s\n" "${0##*/}" "$*" >&2
exit 1
}
check_required_program()
{
command -v "$1" >/dev/null 2>&1 || err "$1: program not found"
}
check_required_program fetch
Tflag=
while getopts T flag; do
case $flag in
T) Tflag=-T
;;
*) usage
;;
esac
done
shift $((OPTIND - 1))
[ $# -le 1 ] || usage
target=
if [ $# -eq 1 ]; then
target=$1
else
IFS= read -r target
fi
[ -n "$target" ] || usage
case $target in
http://* | https://*)
file=$(fetch $Tflag "$target") || err "couldn't fetch $target"
;;
*) [ -f "$target" ] || err "can't handle $uri"
file=$target
;;
esac
mimetype=$(file -bi "$file")
case $mimetype in
audio/*)
check_required_program mpv
mpv --no-video "$file"
;;
image/*)
check_required_program sxiv
sxiv "$file"
;;
text/html)
check_required_program w3m
check_required_program xterm
xterm -e w3m -T text/html "$file"
;;
text/plain | application/x-shellscript)
check_required_program ${PAGER:-less}
check_required_program xterm
xterm -e ${PAGER:-less} "$file"
;;
video/*)
check_required_program mpv
mpv "$file"
;;
*) err "don't know how to open $mimetype"
;;
esac

View File

@ -29,7 +29,10 @@ hc keybind $Mod-Shift-r reload
hc keybind $Mod-Shift-c close
# programs
hc keybind $Mod-g spawn plumb
hc keybind $Mod-g spawn sh -c 'xclip -o | plumb'
hc keybind $Mod-Control-g spawn sh -c 'xclip -sel clip -o | plumb'
hc keybind $Mod-Shift-g spawn sh -c 'xclip -o | plumb -T'
hc keybind $Mod-Control-Shift-g spawn sh -c 'xclip -sel clip -o | plumb -T'
hc keybind $Mod-p spawn dmenu_run \
-p run -fn "$hlwm_face" \
-nb "$hlwm_background" -nf "$hlwm_foreground" \