From 43091854f9c1be35988482cb3d6b9c1d7fdf9d10 Mon Sep 17 00:00:00 2001 From: Lucas Date: Mon, 6 Apr 2020 23:51:03 +0000 Subject: [PATCH] Add plumb script for opening files --- bin/Makefile | 2 +- bin/plumb.sh | 89 +++++++++++++++++++++++++++ xdg-config-dir/herbstluftwm/autostart | 5 +- 3 files changed, 94 insertions(+), 2 deletions(-) create mode 100644 bin/plumb.sh diff --git a/bin/Makefile b/bin/Makefile index 41e826e..a7f6742 100644 --- a/bin/Makefile +++ b/bin/Makefile @@ -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 diff --git a/bin/plumb.sh b/bin/plumb.sh new file mode 100644 index 0000000..8ce6b1b --- /dev/null +++ b/bin/plumb.sh @@ -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 +# . + +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 diff --git a/xdg-config-dir/herbstluftwm/autostart b/xdg-config-dir/herbstluftwm/autostart index 8d7e869..a2ed7af 100755 --- a/xdg-config-dir/herbstluftwm/autostart +++ b/xdg-config-dir/herbstluftwm/autostart @@ -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" \