diff --git a/.config/zsh/completions/_urlhandler b/.config/zsh/completions/_urlhandler new file mode 100644 index 0000000..8bca644 --- /dev/null +++ b/.config/zsh/completions/_urlhandler @@ -0,0 +1,7 @@ +#compdef urlhandler + +_arguments '-h[short help text]' '*:url:_urls' + +# Local Variables: +# mode: shell-script +# End: diff --git a/.config/zsh/functions/urlhandler b/.config/zsh/functions/urlhandler new file mode 100755 index 0000000..760f0eb --- /dev/null +++ b/.config/zsh/functions/urlhandler @@ -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] …" + 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} diff --git a/.local/share/applications/urlhandler.desktop b/.local/share/applications/urlhandler.desktop new file mode 100644 index 0000000..b2824ca --- /dev/null +++ b/.local/share/applications/urlhandler.desktop @@ -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;