dotfiles/.config/zsh/functions/readwwwlog

53 lines
1.5 KiB
Plaintext
Raw Normal View History

2022-04-15 05:13:22 +02:00
#!/usr/bin/env zsh
2022-04-15 07:01:34 +02:00
# Open log files from paste services in a terminal. If the URL doesn't seem to
2022-04-15 05:13:22 +02:00
# be plain text, open in ${default_cmd} instead
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 06:59:04 +02:00
local ret=$(( ${#o_help} ^ 1 ))
2022-04-15 05:13:22 +02:00
print -u $(( 1 + ${ret} )) "usage: ${0} [-h] <URL> …"
return ${ret}
fi
local -a urls=(${@})
local default_cmd="firefox"
2022-04-15 06:09:36 +02:00
local termopts=""
2022-04-15 05:13:22 +02:00
for url in ${urls}; do
if [[ ${url} =~ 'dpaste\.com' && ! ${url[-4,-1]} == ".txt" ]]; then
url+=".txt"
fi
if [[ ${url} =~ 'pastebin\.com' && ! ${url} =~ '/raw/' ]]; then
url="https://pastebin.com/raw/${url##*/}"
fi
if [[ ${url} =~ 'irccloud\.com' && ! ${url} =~ '/raw/' ]]; then
local id=${${(@s:/:)url}[4]}
url="https://www.irccloud.com/pastebin/raw/${id}"
fi
2022-04-15 05:13:22 +02:00
2022-04-15 06:09:36 +02:00
if [[ ${TERMINAL} =~ "alacritty|xfce4-terminal" ]]; then
termopts="--title=${url}"
fi
2022-04-15 05:13:22 +02:00
if curl --silent --location --head ${url} | grep -q 'text/plain'; then
sleep 1 # dpaste.com workaround
local file="$(mktemp --suffix='.readwwwlog')"
2022-04-15 05:13:22 +02:00
curl --silent --location --output ${file} ${url}
local -a cmd=(less --LINE-NUMBERS --ignore-case \
--pattern='(error|fail):' ${file})
if [[ -o NO_INTERACTIVE ]]; then
cmd=(${TERMINAL} ${termopts} -e ${cmd})
fi
${cmd}
2022-04-15 05:13:22 +02:00
rm ${file}
else
${default_cmd} ${url}
fi
done