From d658f06f3927bf23b69bca2eb53e24bbfb5de112 Mon Sep 17 00:00:00 2001 From: Luca Bilke Date: Mon, 3 Apr 2023 12:49:26 +0200 Subject: [PATCH] improve sb-forecast --- .local/bin/statusbar/sb-forecast | 34 +++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/.local/bin/statusbar/sb-forecast b/.local/bin/statusbar/sb-forecast index 3fa62fd74..373dc6399 100755 --- a/.local/bin/statusbar/sb-forecast +++ b/.local/bin/statusbar/sb-forecast @@ -1,19 +1,35 @@ #!/bin/sh +# NOTE: This script takes its dwmblocks update signal as an argument so that it can refresh itself (defaults to 5) # Options can be found at https://wttr.in/:help weatherfile="$XDG_CACHE_HOME/weatherreport" emojifile="$XDG_CACHE_HOME/weatheremoji" -url="wttr.in/?FA" +url="wttr.in/$LOCATION" +weatheropts="?FA" +emojiopts="?FA&format=%c" getforecast() { - curl -sf4 "$url" >$weatherfile || exit 1 - curl -sf4 "$url&format=%c" | sed 's/☀️//;s/☁️//;s/⛅️//;s/⛈//;s/✨//;s/❄️//;s/🌦//;s/🌧//;s/🌨//;s/🌩//;s/🌫//;' >$emojifile || exit 1 + curl -sf "$url$weatheropts" >"$weatherfile" || exit 1 + curl -sf "$url$emojiopts" | sed 's/☀️//;s/☁️//;s/⛅️//;s/⛈//;s/✨//;s/❄️//;s/🌦//;s/🌧//;s/🌨//;s/🌩//;s/🌫//;' | cut -d ' ' -f 1 >"$emojifile" || exit 1 } -showweather() { - emoji="$(printf "\033[12m%s\033[10m" $(cut -d ' ' -f 1 $emojifile))" - sed '16q;d' $weatherfile | grep -wo "[0-9]*%" | sort -rn | sed "s/^/$emoji /g;1q" | tr -d '\n' - sed '13q;d' $weatherfile | grep -o "m\\([-+]\\)*[0-9]\\+" | sed 's/+//g' | sort -n -t 'm' -k 2n | sed -e 1b -e '$!d' | tr '\n|m' ' ' | awk '{print " \033[34;11m\033[0m " $1 "°","\033[31;11m\033[0m " $2 "°"}' +fresh() { + [ -s "$weatherfile" ] && [ "$(stat -c %y "$weatherfile" 2>/dev/null | cut -d' ' -f1)" = "$(date '+%Y-%m-%d')" ] } -getforecast -showweather +readfile() { + weather="$(cat "$weatherfile")" + precipitation="$(echo "$weather" | sed '16q;d' | grep -wo "[0-9]*%" | sort -rn | head -1q)" + highlow="$(echo "$weather" | sed '13q;d' | grep -o "m\\([-+]\\)*[0-9]\\+" | sed 's/[+m]//g' | sort -g | sed -e 1b -e '$!d')" + emoji="$(cat "$emojifile")" +} +output() { + readfile + printf "\033[12m%s\033[10m %s \033[34;11m\033[0m %s° \033[31;11m\033[0m %s°\n" "$emoji" "$precipitation" $highlow +} + +if fresh; then + output +else + getforecast && pkill -RTMIN+${1:-5} dwmblocks & + printf " Getting Weather\n" +fi