dotfiles/.local/bin/urlhandler

54 lines
2.0 KiB
Plaintext
Raw Normal View History

#!/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
2022-04-17 07:14:45 +02:00
local ret=$(( ${#o_help} ^ 1 ))
print -u $(( 1 + ${ret} )) "usage: ${0} [-h] <URL> …"
return ${ret}
fi
local -a urls=(${@})
2022-04-18 06:27:56 +02:00
autoload -U readwwwlog openwwwimg
zmodload zsh/pcre
local default_cmd="firefox"
local mpv="mpv --force-window=yes"
local -A assignments=(
2022-04-23 15:53:15 +02:00
'\.(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"
2022-04-23 15:48:08 +02:00
'\.(log|txt)$' "readwwwlog"
'^https?://(www\.)?(dpaste|pastebin|irccloud)\.com/' "readwwwlog"
'^https?://bpa\.st/' "readwwwlog"
'^https?://sprunge\.us/' "readwwwlog"
'^https?://paste\.debian\.net/' "readwwwlog"
'^https://paste\.centos\.org/' "readwwwlog"
2022-04-23 15:48:08 +02:00
'\.(webm|png|jpe?g|gif|svg|bmp|xcf)(\?.+)?$' "openwwwimg"
2022-04-23 15:53:15 +02:00
'(^magnet:|\.torrent$)' "transmission-remote-gtk"
)
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}