commit ed931dc9abbc0d214777b948b79fd032819a5ed0
parent 74bd311ebbab0db4e96fcd0a32f348e79c34cd3a
Author: Anders Damsgaard <anders@adamsgaard.dk>
Date: Tue, 11 Aug 2020 14:10:41 +0200
Split target handling into smaller functions and fix opening of man pages on disk
Diffstat:
M | .local/bin/plumb | | | 231 | +++++++++++++++++++++++++++++++++++++++++-------------------------------------- |
1 file changed, 120 insertions(+), 111 deletions(-)
diff --git a/.local/bin/plumb b/.local/bin/plumb
@@ -107,8 +107,125 @@ handle_calendar_invite() {
esac
}
-handle_target() {
+handle_file_type() {
+ mime_type="$(file -ib "$1")"
+ if [ "$verbose" = 1 ]; then
+ printf 'target is a file with mime type "%s"\n' "$mime_type"
+ fi
+ case "$mime_type" in
+ image/svg*)
+ detach inkscape "$1";;
+ image/*)
+ detach sxiv "$1";;
+ video/*|\
+ audio/*|\
+ application/ogg|\
+ application/octet-stream)
+ mpv "$1";;
+ text/html)
+ detach $BROWSER "$1";;
+ text/troff)
+ termopen mandoc -l "$1";;
+ text/*)
+ if [ "$(file -b "$1")" = "vCalendar calendar file" ]; then
+ handle_calendar_invite "$1"
+ elif regexmatch "$1" '[A-z0-9]\.[0-9]$'; then
+ termopen mandoc -l "$1"
+ else
+ termopen $PAGER "$1"
+ fi;;
+ *shellscript*)
+ $EDITOR "$1";;
+ application/*html*)
+ detach $BROWSER "$1";;
+ application/pdf*|application/postscript)
+ detach zathura "$1";;
+ application/*document*|\
+ application/msword*|\
+ application/*-excel*|\
+ application/*powerpoint*|\
+ application/zip)
+ detach libreoffice "$1";;
+ *)
+ die "file type $mime_type is not supported"
+ esac
+}
+handle_url() {
+ t="$1"
+ if [ "$verbose" = 1 ]; then
+ printf 'target is not a file\n'
+ fi
+ if regeximatch "$t" '\.onion'; then
+ prefix=torsocks
+ else
+ prefix=
+ fi
+
+ case "$t" in
+ *scholar.google.*)
+ t="$(printf '%s' "$t" | \
+ sed 's/.*scholar_url?url\=//;s/&hl\=.*//')";;
+ *the-cryosphere-discuss.net/tc-*/)
+ a="$(printf '%s' "$t" | \
+ sed 's/.*\(tc-[0-9]*-[0-9]*\).*/\1/')"
+ t="${t}${a}.pdf";;
+ *the-cryosphere.net/*/*/*/)
+ v="$(printf '%s' "$t" | \
+ sed 's,.*/\([0-9][0-9]\)/.*,\1,')"
+ n="$(printf '%s' "$t" | \
+ sed 's,.*/[0-9][0-9]/\([0-9][0-9][0-9][0-9]\)/.*,\1,')"
+ y="$(printf '%s' "$t" | \
+ sed 's,.*/\([0-9][0-9][0-9][0-9]\)/.*,\1,')"
+ t="${t}tc-${v}-${n}-${y}.pdf";;
+ esac
+
+ if regexmatch "$t" '^(0|1|3|7|9|g|I|h)' && \
+ [ "$(printf '%s' "$t" | awk -F'\t' '{print NF}')" -eq 4 ]; then
+ # construct uri from gopher item
+ type="$(printf '%s' "$t" | cut -c 1)"
+ path="$(printf '%s' "$t" | awk -F'\t' '{print $2}')"
+ host="$(printf '%s' "$t" | awk -F'\t' '{print $3}')"
+ port="$(printf '%s' "$t" | awk -F'\t' '{print $4}')"
+ t="gopher://${host}:${port}/${type}${path}"
+ fi
+
+ if regeximatch "$t" '\.(mp4|mkv|webm|avi|ogv|gif)$'; then
+ detach $prefix mpv "$t"
+ elif regeximatch "$t" '^radio://'; then
+ $prefix mpv "http${t##radio}"
+ elif regeximatch "$t" '\.(mp3|ogg|m3u)$'; then
+ $prefix mpv "$(fetch "$t")"
+ elif regeximatch "$t" '\.(png|jpg|jpeg|tif|bmp)$'; then
+ $prefix sxiv "$(fetch "$t")"
+ elif regeximatch "$t" '\.(pdf|ps|epub|djvu)$'; then
+ $prefix zathura "$(fetch "$t")"
+ elif regeximatch "$t" '(smbc-comics\.com|xkcd\.com|jspowerhour\.com|explosm\.net)'; then
+ cd /tmp && detach comic "$t"
+ elif regexmatch "$t" '^doi.*10\.[0-9]*\/'; then
+ detach $prefix $BROWSER "https://doi.org/${t}"
+ elif regexmatch "$t" '\.(txt|patch|diff|rst|md|vtt|vtv)$'; then
+ f="$(fetch "$t")"
+ termopen $PAGER "$f"
+ elif regeximatch "$t" '^gopher://'; then
+ termopen $prefix sacc "$t"
+ elif regexmatch "$t" '[A-z0-9]\.[0-9]$'; then
+ if [ -z "$TERM" ]; then
+ detach $TERMINAL mandoc -l "$(fetch "$t")"
+ else
+ mandoc -l "$(fetch "$t")"
+ fi
+ elif regeximatch "$t" '(youtube\.|youtu\.be|vimeo\.com|invidio\.us|invidious\.snopyta\.org)'; then
+ cd ~/tmp && detach $prefix mpv "$t"
+ elif regeximatch "$t" '^(http|https)://' || \
+ regeximatch "$t" '\.webp$'; then
+ detach $prefix $BROWSER "$t"
+ else
+ die "unknown target type: $t"
+ fi
+}
+
+handle_target() {
t="$1"
if [ "$verbose" = 1 ]; then
printf 'target: %s\n' "$t"
@@ -119,117 +236,9 @@ handle_target() {
fi
termopen cd $t; $SHELL
elif [ -e "$t" ]; then
- mime_type="$(file -ib "$t")"
- if [ "$verbose" = 1 ]; then
- printf 'target is a file with mime type "%s"\n' "$mime_type"
- fi
- case "$mime_type" in
- image/svg*)
- detach inkscape "$t";;
- image/*)
- detach sxiv "$t";;
- video/*|\
- audio/*|\
- application/ogg|\
- application/octet-stream)
- mpv "$t";;
- text/html)
- detach $BROWSER "$t";;
- text/troff)
- termopen mandoc -l "$t";;
- text/*)
- if [ "$(file -b "$t")" = "vCalendar calendar file" ]; then
- handle_calendar_invite "$t"
- else
- termopen $PAGER "$t"
- fi;;
- *shellscript*)
- $EDITOR "$t";;
- application/*html*)
- detach $BROWSER "$t";;
- application/pdf*|application/postscript)
- detach zathura "$t";;
- application/*document*|\
- application/msword*|\
- application/*-excel*|\
- application/*powerpoint*|\
- application/zip)
- detach libreoffice "$t";;
- *)
- die "file type $mime_type is not supported"
- esac
+ handle_file_type "$t"
else
-
- if [ "$verbose" = 1 ]; then
- printf 'target is not a file\n'
- fi
- if regeximatch "$t" '\.onion'; then
- prefix=torsocks
- else
- prefix=
- fi
-
- case "$t" in
- *scholar.google.*)
- t="$(printf '%s' "$t" | \
- sed 's/.*scholar_url?url\=//;s/&hl\=.*//')";;
- *the-cryosphere-discuss.net/tc-*/)
- a="$(printf '%s' "$t" | \
- sed 's/.*\(tc-[0-9]*-[0-9]*\).*/\1/')"
- t="${t}${a}.pdf";;
- *the-cryosphere.net/*/*/*/)
- v="$(printf '%s' "$t" | \
- sed 's,.*/\([0-9][0-9]\)/.*,\1,')"
- n="$(printf '%s' "$t" | \
- sed 's,.*/[0-9][0-9]/\([0-9][0-9][0-9][0-9]\)/.*,\1,')"
- y="$(printf '%s' "$t" | \
- sed 's,.*/\([0-9][0-9][0-9][0-9]\)/.*,\1,')"
- t="${t}tc-${v}-${n}-${y}.pdf";;
- esac
-
- if regexmatch "$t" '^(0|1|3|7|9|g|I|h)' && \
- [ "$(printf '%s' "$t" | awk -F'\t' '{print NF}')" -eq 4 ]; then
- # construct uri from gopher item
- type="$(printf '%s' "$t" | cut -c 1)"
- path="$(printf '%s' "$t" | awk -F'\t' '{print $2}')"
- host="$(printf '%s' "$t" | awk -F'\t' '{print $3}')"
- port="$(printf '%s' "$t" | awk -F'\t' '{print $4}')"
- t="gopher://${host}:${port}/${type}${path}"
- fi
-
- if regeximatch "$t" '\.(mp4|mkv|webm|avi|ogv|gif)$'; then
- detach $prefix mpv "$t"
- elif regeximatch "$t" '^radio://'; then
- $prefix mpv "http${t##radio}"
- elif regeximatch "$t" '\.(mp3|ogg|m3u)$'; then
- $prefix mpv "$(fetch "$t")"
- elif regeximatch "$t" '\.(png|jpg|jpeg|tif|bmp)$'; then
- $prefix sxiv "$(fetch "$t")"
- elif regeximatch "$t" '\.(pdf|ps|epub|djvu)$'; then
- $prefix zathura "$(fetch "$t")"
- elif regeximatch "$t" '(smbc-comics\.com|xkcd\.com|jspowerhour\.com|explosm\.net)'; then
- cd /tmp && detach comic "$t"
- elif regexmatch "$t" '^doi.*10\.[0-9]*\/'; then
- detach $prefix $BROWSER "https://doi.org/${t}"
- elif regexmatch "$t" '\.(txt|patch|diff|rst|md|vtt|vtv)$'; then
- f="$(fetch "$t")"
- termopen $PAGER "$f"
- elif regeximatch "$t" '^gopher://'; then
- termopen $prefix sacc "$t"
- elif regexmatch "$t" '[A-z]\.[0-9]$'; then
- if [ -z "$TERM" ]; then
- detach $TERMINAL mandoc -l "$(fetch "$t")"
- else
- mandoc -l "$(fetch "$t")"
- fi
- elif regeximatch "$t" '(youtube\.|youtu\.be|vimeo\.com|invidio\.us|invidious\.snopyta\.org)'; then
- cd ~/tmp && detach $prefix mpv "$t"
- elif regeximatch "$t" '^(http|https)://' || \
- regeximatch "$t" '\.webp$'; then
- detach $prefix $BROWSER "$t"
- else
- die "unknown target type: $t"
- fi
+ handle_url "$t"
fi
s="$?"
if [ "$s" -ne 0 ]; then