plumb - scripts - various script and utils
HTML git clone git://z3bra.org/scripts
DIR Log
DIR Files
DIR Refs
---
plumb (2907B)
---
1 #!/bin/sh
2
3 # all these have the same result:
4 #
5 # plumb -u https://z3bra.org/z3bra.png
6 # echo https://z3bra.org/z3bra.png | xsel; plumb
7 # echo https://z3bra.org/z3bra.png | plumb -s
8 # curl -s https://z3bra.org/z3bra.png | plumb -
9
10 usage() {
11 echo "usage: $(basename $0) [-hs] [-u uri]" >&2
12 }
13
14 justtype() {
15 dos2unix -q | tr -d ' ' |cut -d: -f2 | cut -d\; -f1
16 }
17
18 gophertype() {
19 file=$(basename "$1")
20 type=$(echo "$1" | cut -d/ -f4)
21 case $type in
22 9) printf 'application/octet-stream\n' ;;
23 I) printf 'image/%s\n' "${file##*.}" ;;
24 g) printf 'image/gif\n' ;;
25 *) printf 'text/plain\n' ;;
26 esac
27 }
28
29 pipe() {
30 [ -n "$proto" ] && curl -sSkL "$1" -o -
31 [ -z "$proto" ] && cat "$1"
32 }
33
34 getclip() {
35 wl-paste -p 2>/dev/null
36 }
37
38 # default mimetype
39 mime=''
40 clip="$(getclip)" # get uri from clipboard by default
41
42 while getopts "hsu:v-" OPT; do
43 case $OPT in
44 s) input="$(cat)" ;; # read uri from stdin
45 u) input="$OPTARG" ;; # pass uri as argument
46 v) verbose=1 ;;
47 h) usage; exit 0 ;;
48 *) usage; exit 1 ;;
49 esac
50 done
51
52 shift $((OPTIND-1))
53
54 [ $# -gt 0 ] && input="$1"
55
56 # read file *content* to stdin when uri is -
57 [ "$1" = "-" ] && {
58 input=$(mktemp -p /tmp plumb.XXXXXXXX)
59 cat > $input
60 }
61
62 uri="${input:-$clip}"
63
64 [ -z "$uri" ] && exit 1
65
66 proto="$(echo "$uri" | sed -n 's,^\([^:]*\)://.*,\1,p')"
67
68 if [ -z "$proto" ]; then
69 if [ -e "$uri" ]; then
70 mime=$(file -i "$uri" | justtype)
71 elif getent hosts "$uri" >/dev/null; then
72 # assume website if name is resolvable
73 mime="text/html"
74 else
75 uri="https://duckduckgo.com/?q=$uri"
76 fi
77 else
78 case $proto in
79 vmrc) mime=application/x-vmrc ;;
80 https|http) mime=$(curl -skI "$uri" | grep -ioE 'content-type:[^;]*' | justtype) ;;
81 gopher)
82 mime=$(gophertype "$uri")
83 uri=$(echo $uri | sed 's,gopher://,http://phroxy.z3bra.org/,')
84 ;;
85 telnet|ssh)
86 mime="$proto"
87 uri="${uri#${proto}://}"
88 host="${uri%%:*}"
89 port="${uri##*:}"
90 ;;
91 esac
92
93 [ -z "$mime" ] && mime=$(curl -sk "$uri" | cut -b 1-100 | file -i - | justtype)
94 fi
95
96 # verbose mode
97 [ -n "$verbose" ] && printf 'uri: %s\nproto: %s\nmime: "%s"\n' "$uri" "$proto" "$mime"
98
99 case "$mime" in
100 text/html) exec ${BROWSER:-firefox-nightly} "$uri" ;;
101 text/xml) exec ${BROWSER:-firefox-nightly} "$uri" ;;
102 image/gif) exec mplayer -loop 0 "$uri" ;;
103 audio/*) exec mpv -cache 512 "$uri" ;;
104 video/*) exec mpv -cache 512 "$uri" ;;
105 image/*) pipe "$uri" | img ;;
106 text/*)
107 if [ -n "$proto" ]; then
108 tmp=$(mktemp -p /tmp plumb.XXXXXXXX)
109 pipe "$uri" > $tmp
110 uri=$tmp
111 fi
112 tty -s && exec less "$uri" || exec st -e less "$uri"
113 ;;
114 application/octet-stream) exec $(tofi-run) "$uri" ;;
115 application/x-vmrc) exec vmrc "$uri" ;;
116 application/x-java-jnlp-file) exec javaws "$uri" ;;
117 application/vnd.openxmlformats-*) libreoffice "$uri" ;;
118 telnet) exec st -e telnet "$host" "${port:-proto}" ;;
119 ssh) exec st -e ssh -P "${port:-$proto}" "$host" ;;
120 *) exec ${BROWSER:-surf} "$uri" ;;
121 esac