1
0
Fork 0
dotfiles/.config/zsh/functions/readwwwlog

48 lines
1.3 KiB
Bash
Executable File

#!/usr/bin/env zsh
# Open log files from paste services in a terminal. If the URL doesn't seem to
# 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
ret=$(( ${#o_help} ^ 1 ))
print -u $(( 1 + ${ret} )) "usage: ${0} [-h] <URL> …"
return ${ret}
fi
local -a urls=(${@})
local default_cmd="firefox"
local termopts=""
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 [[ ${TERMINAL} =~ "alacritty|xfce4-terminal" ]]; then
termopts="--title=${url}"
fi
if curl --silent --location --head ${url} | grep -q 'text/plain'; then
sleep 1 # dpaste.com workaround
local file="$(mktemp --suffix='.readwwwlog')"
curl --silent --location --output ${file} ${url}
local -a cmd=(less --LINE-NUMBERS --pattern='(error|fail):' ${file})
if [[ -o NO_INTERACTIVE ]]; then
cmd=(${TERMINAL} ${termopts} -e ${cmd})
fi
${cmd}
rm ${file}
else
${default_cmd} ${url}
fi
done