dotfiles/.config/zsh/functions/openwwwimg

34 lines
899 B
Plaintext
Raw Normal View History

2022-04-18 06:27:56 +02:00
#!/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] <URL> …"
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