commit a71d78a8d38174ed65b51dbdf96d8698ed154202
parent 30059c7573ef92f5715cf135d67afa4e1f0ac529
Author: hhvn <dev@hhvn.uk>
Date: Sat, 3 Jul 2021 21:26:24 +0100
.s/b/{plumb,urlhand,urlhand2}: centralize url/file handling into one 'plumb' program
Diffstat:
3 files changed, 161 insertions(+), 35 deletions(-)
diff --git a/.scripts/bin/plumb b/.scripts/bin/plumb
@@ -0,0 +1,122 @@
+#!/bin/sh
+
+echo(){
+ printf '%s\n' "$*"
+}
+
+prompt(){
+ prompt_var="$1"
+ shift
+
+ stty >/dev/null && {
+ printf '%s: ' "$*"
+ read -r $prompt_var
+ } || {
+ prompt_var=$(</dev/null dmenu -i -p "$*")
+ }
+}
+
+matches(){
+ echo "$arg" | grep -Ei "$@" >/dev/null
+}
+
+fmatches(){
+ file "$arg" | grep -Ei "$@" >/dev/null
+}
+
+rand(){
+ base64 </dev/urandom | tr / + | head -c$1
+}
+
+plumb(){
+ [ "$1" = "gui" ] && {
+ gui=1
+ shift
+ }
+ [ $norun -eq 1 ] && return
+ export norun=1
+ [ $gui -eq 1 ] &&
+ sh -c "$*" "$arg" &
+ [ $gui -eq 0 ] &&
+ ${TERMINAL:-st} sh -c "$*" "$arg" &
+}
+
+_mktemp(){
+ _mktemp_file="/tmp/$(rand 10)-$1"
+ while [ -f $_mktemp_file ]
+ do
+ _mktemp_file="/tmp/$(rand 10)-$1"
+ done
+}
+
+for arg in "$@"
+do
+ herbe "arg: $arg"
+ norun=0
+ gui=0
+
+ # network
+ matches "^magnet:" &&
+ plumb 'addtorrent $0'
+
+ matches "^(gopher|http)s?://.*\.(mkv|mp4|avi|webm|ogg|ogv|gifv|mp3|mp4|opus|flac|ape|m3u|m3u8)$" ||
+ matches "^https?://.*youtu.*be" &&
+ plumb 'mpv --pause $0'
+
+ matches "^(gopher|http)s?://.*\.(jpe?g|gif|tiff?|ppm|bit|bmp|png|xpm)$" &&
+ plumb gui 'netimg $0'
+
+ matches "^https?://" &&
+ plumb gui 'xprop -id $(cat /tmp/tabbed.xid) >/dev/null && surf -e $(cat /tmp/tabbed.xid) $0 || >/tmp/tabbed.xid tabbed -c surf -e $(sleep 1 && cat /tmp/tabbed.xid) $0'
+
+ matches "^gophers?://(.*/1(/|$)|[^/]*$)" &&
+ plumb 'cgo $0'
+
+ matches "^gophers?://" &&
+ plumb 'curl $0 | vim -'
+
+ matches "^mailto:" &&
+ plumb 'neomutt $(echo "$0" | sed "s~^mailto:~~")'
+
+ matches "^finger://" &&
+ plumb 'finger $(echo "$0" | sed "s~^finger://~~")'
+
+ matches "^ssh://" &&
+ plumb 'ssh $(echo "$0" | sed "s~^ssh://~~;s~:[0-9]*~~")'
+
+ matches "^git://" &&
+ plumb 'git clone $0'
+
+ # local files now
+ matches ".*\.(epub|ps|eps|pdf|dvi|djvu)$" ||
+ fmatches "pdf|postscript" &&
+ plumb gui 'zathura $0'
+
+ matches ".*\.(mkv|mp4|avi|webm|ogg|ogv|gifv|mp3|mp4|opus|flac|ape|m3u|m3u8)$" ||
+ fmatches "video|matroska|audio|sound" &&
+ plumb 'mpv --pause $0'
+
+ matches ".*\.(jpe?g|gif|tiff?|ppm|bit|bmp|png|xpm)$" ||
+ fmatches "image" &&
+ plumb gui 'sxiv $0'
+
+ matches ".*\.h$" &&
+ [ -f "/usr/include/$arg" ] &&
+ plumb 'vim /usr/include/$0'
+
+ matches ".*\.h$" &&
+ [ -f "/usr/local/include/$arg" ] &&
+ plumb 'vim /usr/local/include/$0'
+
+ matches "/.*\.[0-9]p?$" &&
+ fmatches "(roff|preprocessor)" &&
+ plumb 'man $0'
+
+ matches "[^/].*\.[0-9]p?$" &&
+ fmatches "(roff|preprocessor)" &&
+ plumb 'man ./$0'
+
+ matches ".*" &&
+ plumb 'vim $0'
+done
+wait
diff --git a/.scripts/bin/urlhand b/.scripts/bin/urlhand
@@ -4,39 +4,43 @@ links=$(grep -Eo '[A-Za-z][A-Za-z]*://[^ ]*' < /dev/stdin 2>/dev/null | sed 's/[
[ $(echo "$links" | wc -l) -eq 1 ] && link=$links || link=$(printf 'ALL\n%s\n' "$links" | dmenu -i)
[ "$link" = "" ] && exit 1
-hand=$(printf "mpvms video
-mpvms
-mpv --pause
-popupmpv
-youtube-dl --add-metadata -ic
-youtube-dl --add-metadata -xic
-$(xprop -id $(cat /tmp/tabbed.xid) >/dev/null && echo "surf -e $(cat /tmp/tabbed.xid)")
->/tmp/tabbed.xid tabbed -cr2 surf -e ''
-toxclip
-netimg
-netpdf
-popupcurl
-popupcgo
-popupw3m
-gpgimport
-torsocks mpvms video
-torsocks mpvms
-torsocks mpv --pause
-torsocks popupmpv
-torsocks youtube-dl --add-metadata -ic
-torsocks youtube-dl --add-metadata -xic
-torsocks $(xprop -id $(cat /tmp/tabbed.xid) >/dev/null && echo "surf -e $(cat /tmp/tabbed.xid)")
-torsocks >/tmp/tabbed.xid tabbed -cr2 surf -e ''
-torsocks netimg
-torsocks netpdf
-torsocks popupcurl
-torsocks popupcgo
-torsocks popupw3m
-torsocks gpgimport" | dmenu -i)
+# hand=$(printf "mpvms video
+# mpvms
+# mpv --pause
+# popupmpv
+# youtube-dl --add-metadata -ic
+# youtube-dl --add-metadata -xic
+# $(xprop -id $(cat /tmp/tabbed.xid) >/dev/null && echo "surf -e $(cat /tmp/tabbed.xid)")
+# >/tmp/tabbed.xid tabbed -cr2 surf -e ''
+# toxclip
+# netimg
+# netpdf
+# popupcurl
+# popupcgo
+# popupw3m
+# gpgimport
+# torsocks mpvms video
+# torsocks mpvms
+# torsocks mpv --pause
+# torsocks popupmpv
+# torsocks youtube-dl --add-metadata -ic
+# torsocks youtube-dl --add-metadata -xic
+# torsocks $(xprop -id $(cat /tmp/tabbed.xid) >/dev/null && echo "surf -e $(cat /tmp/tabbed.xid)")
+# torsocks >/tmp/tabbed.xid tabbed -cr2 surf -e ''
+# torsocks netimg
+# torsocks netpdf
+# torsocks popupcurl
+# torsocks popupcgo
+# torsocks popupw3m
+# torsocks gpgimport" | dmenu -i)
+
+# [ "$link" = "ALL" ] && {
+# for u in $links
+# do
+# nohup sh -c "$hand $u" >/dev/null &
+# done
+# } || nohup sh -c "$hand $link" >/dev/null &
[ "$link" = "ALL" ] && {
- for u in $links
- do
- nohup sh -c "$hand $u" >/dev/null &
- done
-} || nohup sh -c "$hand $link" >/dev/null &
+ plumb $links
+} || plumb "$link"
diff --git a/.scripts/bin/urlhand2 b/.scripts/bin/urlhand2
@@ -1,3 +1,3 @@
#!/bin/sh
-echo "$@" | $(dirname $0)/urlhand
+plumb "$@"