commit 015a0d7576af65822b35b0deeb1f7d33a9b9d5ea parent fc7acb4ac8f794bfa26582db3c5579792de65385 Author: Anders Damsgaard <anders@adamsgaard.dk> Date: Wed, 5 Feb 2020 17:02:20 +0100 Combine xdg-open and plumb into single script Diffstat:
M | .local/bin/plumb | | | 232 | +++++++++++++++++++++++++++++++++++++++++++------------------------------------ |
T | .local/bin/xdg-open | | | 0 |
2 files changed, 127 insertions(+), 105 deletions(-)
diff --git a/.local/bin/plumb b/.local/bin/plumb @@ -1,121 +1,143 @@ #!/bin/sh -mkdir -p ~/tmp -cd ~/tmp || exit 42 +set -e + +version=0.1 + +show_help() { + echo "usage: ${0##*/} [OPTIONS] [FILE|URL]" + echo "will open FILE or URL" + echo "If no FILE or URL is specified, this program will expect them as stdin." + echo + echo "OPTIONS are one or more of the following:" + echo " -h, --help show this message" + echo " -v, --version show version and license information" + echo " -V, --verbose show verbose information" + echo " -- do not consider any following args as options" +} + +show_version() { + echo "${0##*/} version $version" + echo "Licensed under the ISC License" + echo "written by Anders Damsgaard, anders@adamsgaard.dk" + echo "https://src.adamsgaard.dk/dotfiles" +} die() { - printf '%s\n' "$1" >&2 + printf 'error: %s\n' "$1" >&2 exit 1 } -handle_uri() { - if [ -f "$1" ]; then - setsid "$TERMINAL" -e "$EDITOR $1" - exit 0; +detach() { + nohup "$@" >/dev/null 2>&1 & +} + +fetchtmp() { + if type -v hurl >/dev/null 2>&1; then + # TODO + return 0 + elif type -v curl >/dev/null 2>&1; then + # TODO + return 0 + else + die 'could not find hurl or curl' fi +} - uri="$1" +handle_target() { - # strip redirects - case "$uri" in - *scholar.google.*) - uri="$(echo "$uri" | sed 's/.*scholar_url?url\=//;s/&hl\=.*//')";; - esac - + if [ -e "$1" ]; then + mime_type="$(file -ib "$1")" + case "$mime_type" in + image/*) + detach sxiv "$1";; + video/*|audio/*) + mpv "$1";; + text/html) + detach $BROWSER "$1";; + text/*) + $EDITOR "$1";; + *shellscript*) + $EDITOR "$1";; + application/*html*) + detach $BROWSER "$1";; + application/pdf*) + detach zathura "$1";; + application/*document*|\ + application/msword*|\ + application/*-excel*|\ + application/*powerpoint*|\ + application/octet-stream|\ + application/zip) + detach libreoffice "$1";; + *) + die "file type $mime_type is not supported" + esac + exit $? - action="undefined" - case "$uri" in - *.png|*.PNG|*.jpg|*.JPG|*.jpeg|*.JPEG|*.tif|*.TIF|*.bmp|*.BMP|*.pdf|*.PDF|*.mp4|*.mkv|*.webm) - notify-send "${0##*/}" "download + xdg-open" - action="xdg-open" ;; - *youtube.com*|*youtu.be*) - notify-send "${0##*/}" "youtube-dl" - action="youtube-dl" ;; - *smbc-comics.com*|*xkcd.com*|*jspowerhour.com*) - notify-send "${0##*/}" "comic" - action="comic" ;; - 10.[0-9]*\/*) - uri="https://doi.org/$uri" - notify-send "${0##*/}" "open as doi" - action="open" ;; - gopher://*) - nohup $TERMINAL sacc "$uri" >/dev/null 2>&1 & - ;; - *) - action="$(printf "open\nopen (w3m)\nopen (tbb)\nxclip\nwebsearch\nmap\nwikipedia\nbookmark\ndownload\nhtml to pdf\nsci-hub\nscholarref\nmpv\nxdg-open\nyoutube-dl\nyoutube-dl-music\ndefine\nsendtext" | \ - dmenu -i -p "$(echo "$uri" | sed 's/.*:\/\///' | cut -c-80)")";; - esac + else - case "$action" in - "open") - nohup xdg-open "$uri" >/dev/null 2>&1 & ;; - "open (w3m)") - nohup $TERMINAL w3m "$uri" >/dev/null 2>&1 & ;; - "open (tbb)") - nohup tor-browser "$uri" >/dev/null 2>&1 & ;; - "xclip") - printf "%s" "$uri" | xclip - printf "%s" "$uri" | xclip -selection clipboard ;; - "bookmark") - description="$(echo "$(date), $hostname" | \ - dmenu -p "description:")" - nohup $TERMINAL bookmark "$uri" "$description" >/dev/null 2>&1 & ;; - "comic") - nohup comic "$uri" >/dev/null 2>&1 & ;; - "download") - if type -v curl >/dev/null 2>&1; then - echo "curl -sLO '$uri' >/dev/null" | at now - elif type -v wget >/dev/null 2>&1; then - echo "wget '$uri' >/dev/null" | at now - else - die 'Error: no download program found' - fi;; - "html to pdf") - out="$HOME/tmp/html_to_pdf_$(date +'%F-%T')".pdf && \ - wkhtmltopdf "$uri" "$out" && \ - notify-send "$(basename $out) complete" ;; - "websearch") - websearch "$uri";; - "map") - websearch "!map $uri";; - "wikipedia") - websearch "!wiki $uri";; - "sci-hub") - echo "shdl --tor-socks --notify --open --reference '$uri' >/dev/null" \ - | at now;; - "scholarref") - echo "getdoi '$uri' | getref --notify >> '$BIB'" | at now;; - "mpv") - nohup mpv -quiet "$uri" >/dev/null 2>&1 & ;; - "xdg-open") - mkdir -p open && cd open - f="$(curl -s --write-out "%{filename_effective}\n" -OL "$uri")" - nohup xdg-open "$f" >/dev/null 2>&1 & ;; - "youtube-dl") - echo "youtube-dl '$uri' >/dev/null" | at now;; - "youtube-dl-music") - nohup $TERMINAL -e youtube-dl-music "$uri" >/dev/null 2>&1 & ;; - "define") - define -g "$uri\n";; - "sendtext") - printf "%s\n" "$uri" | text.sh;; - *) - notify-send "${0##/*}" "Error: Action not understood" - exit 1 ;; - esac + case "$1" in + *scholar.google.*) + 1="$(printf '%s' "$1" | sed 's/.*scholar_url?url\=//;s/&hl\=.*//')";; + esac + + case "$1" in + *.mp4|*.mkv|*.webm|*.ogv|*.avi|*.ogg|*.ogv|*.gifv) + detach mpv "$1";; + *.png|*.jpg|*.jpeg|*.tif|*.bmp) + ;; + *.pdf|*.ps|*.epub|*.djvu) + ;; + *youtube.com*|*youtu.be*) + cd ~/tmp && detach youtube-dl "$1";; + *smbc-comics.com*|*xkcd.com*|*jspowerhour.com*) + cd ~/tmp && detach comic "$1";; + 10.[0-9]*\/*) + detach $BROWSER "https://doi.org/${1}" ;; + gopher://*) + detach $TERMINAL sacc "$1";; + http://*|https://*) + detach $BROWSER "$1";; + *) + die "unknown target type: $1" + esac + fi } +verbose=0 +while :; do + case "$1" in + -h|-\?|--help) + show_help + exit 0 + ;; + -v|--version) + show_version + exit 0 + ;; + -V|--verbose) + verbose=1 + ;; + --) # end all options + shift + break + ;; + -?*) + die 'error: Unknown option specified' + ;; + *) # No more options + break + esac + shift +done + if [ $# -lt 1 ]; then - handle_uri "$(cat)" + targets="$(cat)" + for f in "$targets"; do + handle_target "$f" + done else - if [ "$1" = "-c" ]; then - if [ $# -eq 2 ] && [ "$2" = "clipboard" ]; then - handle_uri "$(xclip -o -selection clipboard)" - else - handle_uri "$(xclip -o)" - fi - else - for uri in "$@"; do - handle_uri "$uri" - done - fi + for f in "$@"; do + handle_target "$f" + done fi diff --git a/.local/bin/xdg-open b/.local/bin/xdg-open