Add urlhandler
This commit is contained in:
parent
1c77fccff6
commit
f4ca07c8be
|
@ -0,0 +1,7 @@
|
|||
#compdef urlhandler
|
||||
|
||||
_arguments '-h[short help text]' '*:url:_urls'
|
||||
|
||||
# Local Variables:
|
||||
# mode: shell-script
|
||||
# End:
|
|
@ -0,0 +1,45 @@
|
|||
#!/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}
|
|
@ -0,0 +1,9 @@
|
|||
[Desktop Entry]
|
||||
Type=Application
|
||||
Version=1.0
|
||||
Name=urlhandler
|
||||
Comment=Open URLs with different commands
|
||||
TryExec=/home/tastytea/.config/zsh/functions/urlhandler
|
||||
Exec=/home/tastytea/.config/zsh/functions/urlhandler %u
|
||||
Terminal=false
|
||||
MimeType=x-scheme-handler/unknown;x-scheme-handler/about;x-scheme-handler/https;x-scheme-handler/http;text/html;
|
Loading…
Reference in New Issue