From cf6d630911305ae992e96b808f3136965267ca84 Mon Sep 17 00:00:00 2001 From: tastytea Date: Fri, 15 Apr 2022 14:37:35 +0200 Subject: [PATCH] Move urlhandler to ~/.local/bin It will probably never be called from the terminal, so it shouldn't be a function. --- .config/zsh/functions/urlhandler | 47 ------------------------------- .local/bin/urlhandler | 48 +++++++++++++++++++++++++++++++- 2 files changed, 47 insertions(+), 48 deletions(-) delete mode 100755 .config/zsh/functions/urlhandler mode change 120000 => 100755 .local/bin/urlhandler diff --git a/.config/zsh/functions/urlhandler b/.config/zsh/functions/urlhandler deleted file mode 100755 index 975b22c..0000000 --- a/.config/zsh/functions/urlhandler +++ /dev/null @@ -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] …" - 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} diff --git a/.local/bin/urlhandler b/.local/bin/urlhandler deleted file mode 120000 index e8a67bd..0000000 --- a/.local/bin/urlhandler +++ /dev/null @@ -1 +0,0 @@ -../../.config/zsh/functions/urlhandler \ No newline at end of file diff --git a/.local/bin/urlhandler b/.local/bin/urlhandler new file mode 100755 index 0000000..975b22c --- /dev/null +++ b/.local/bin/urlhandler @@ -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] …" + 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}