diff --git a/.config/zsh/completions/_openwwwimg b/.config/zsh/completions/_openwwwimg new file mode 100644 index 0000000..8adbbe6 --- /dev/null +++ b/.config/zsh/completions/_openwwwimg @@ -0,0 +1,7 @@ +#compdef openwwwimg + +_arguments '-h[short help text]' '*:url:_urls' + +# Local Variables: +# mode: shell-script +# End: diff --git a/.config/zsh/functions/openwwwimg b/.config/zsh/functions/openwwwimg new file mode 100755 index 0000000..a7da496 --- /dev/null +++ b/.config/zsh/functions/openwwwimg @@ -0,0 +1,33 @@ +#!/usr/bin/env zsh +# Open images from URLs by downloading them and then opening them in +# ${image_viewer}. If downloading fails, open in ${default_cmd} instead. + +setopt LOCAL_OPTIONS ERR_RETURN NO_UNSET PIPE_FAIL + +zmodload zsh/zutil +local -a o_help=() +zparseopts -D -K -- h=o_help +if [[ ${#o_help} -ne 0 || ! -v 1 ]]; then + local ret=$(( ${#o_help} ^ 1 )) + print -u $(( 1 + ${ret} )) "usage: ${0} [-h] …" + return ${ret} +fi +local -a urls=(${@}) + +local default_cmd="firefox" +local image_viewer="viewnior" + +for url in ${urls}; do + if type ${image_viewer%% *} >& -; then + local file="$(mktemp --suffix='.openwwwimg')" + curl --silent --location --output ${file} ${url} + if [[ ${?} -eq 0 ]]; then + ${image_viewer} ${file} + else + ${default_cmd} ${url} + fi + rm -f ${file} + else + ${default_cmd} ${url} + fi +done diff --git a/.local/bin/urlhandler b/.local/bin/urlhandler index 3908bc8..abcda47 100755 --- a/.local/bin/urlhandler +++ b/.local/bin/urlhandler @@ -15,7 +15,7 @@ if [[ ${#o_help} -ne 0 || ! -v 1 ]]; then fi local -a urls=(${@}) -autoload -U readwwwlog +autoload -U readwwwlog openwwwimg zmodload zsh/pcre local default_cmd="firefox" @@ -29,6 +29,7 @@ local -A assignments=( '^(gemini|gopher)://' "kristall" '\.(log|txt)$' "readwwwlog" '^https?://(www\.)?(dpaste|pastebin|irccloud)\.com/' "readwwwlog" + '\.(webm|png|jpe?g|gif|svg|bmp|xcf)$' "openwwwimg" ) local selected_cmd=${default_cmd}