1
0
Fork 0

Move urlhandler to ~/.local/bin

It will probably never be called from the terminal, so it shouldn't be a
function.
This commit is contained in:
tastytea 2022-04-15 14:37:35 +02:00
parent 87415e588b
commit cf6d630911
Signed by: tastytea
SSH Key Fingerprint: SHA256:FBkvrOlhq5use1XEttyUGT4bUTDVA1ar9SgIc9P03cM
2 changed files with 47 additions and 48 deletions

View File

@ -1,47 +0,0 @@
#!/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=(${@})
autoload -U readwwwlog
zmodload zsh/pcre
local default_cmd="firefox"
local mpv="mpv --force-window=yes"
local -A assignments=(
'\.(mp4|m4v|mkv|avi|webm|flv|xvid|ogv|theora|mov|wmv)$' "${mpv};vlc"
'\.(ogg|flac|opus|m4a|wav|mp3|mid|aac|wma)$' "${mpv};vlc"
'^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
${(@s/ /)selected_cmd} ${urls}

View File

@ -1 +0,0 @@
../../.config/zsh/functions/urlhandler

47
.local/bin/urlhandler Executable file
View File

@ -0,0 +1,47 @@
#!/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=(${@})
autoload -U readwwwlog
zmodload zsh/pcre
local default_cmd="firefox"
local mpv="mpv --force-window=yes"
local -A assignments=(
'\.(mp4|m4v|mkv|avi|webm|flv|xvid|ogv|theora|mov|wmv)$' "${mpv};vlc"
'\.(ogg|flac|opus|m4a|wav|mp3|mid|aac|wma)$' "${mpv};vlc"
'^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
${(@s/ /)selected_cmd} ${urls}