46 lines
1.4 KiB
Plaintext
46 lines
1.4 KiB
Plaintext
|
#!/usr/bin/env zsh
|
||
|
# Open URLs based on perl compatible regular expressions. Several commands can
|
||
|
# be specified with ; as separator. If the URL doesn't match anything or the
|
||
|
# commands are not found, use ${default_cmd}.
|
||
|
|
||
|
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
|
||
|
ret=$(( ${#o_help} ^ 1 ))
|
||
|
print -u $(( 1 + ${ret} )) "usage: ${0} [-h] <URL> …"
|
||
|
return ${ret}
|
||
|
fi
|
||
|
local -a urls=(${@})
|
||
|
|
||
|
local default_cmd="firefox"
|
||
|
autoload -U readwwwlog
|
||
|
zmodload zsh/pcre
|
||
|
|
||
|
local -A assignments=(
|
||
|
'.(mp4|m4v|mkv|avi|webm|flv|xvid|ogv|theora|mov)$' "mpv"
|
||
|
'.(ogg|flac|opus|m4a|wav|mp3|mid|aac)$' "mpv --force-window=yes"
|
||
|
'^https://media.ccc.de/v/[^/]*$' "mpv"
|
||
|
'^https?://(www.)?youtu(.be|be.com)/' "mpv"
|
||
|
'^(gemini|gopher)://' "kristall"
|
||
|
'.(log|txt)$' "readwwwlog"
|
||
|
'^https?://dpaste.com/' "readwwwlog"
|
||
|
)
|
||
|
|
||
|
local selected_cmd=${default_cmd}
|
||
|
for regex cmds in ${(kv)assignments}; do
|
||
|
if [[ ${urls[1]} -pcre-match ${regex} ]]; then
|
||
|
for cmd in ${(s/;/)cmds}; do
|
||
|
if type ${cmd} >& -; then
|
||
|
selected_cmd=${cmd}
|
||
|
break
|
||
|
fi
|
||
|
done
|
||
|
break
|
||
|
fi
|
||
|
done
|
||
|
|
||
|
${selected_cmd} ${urls}
|