From e3e7ef423ad67bd8b155435cc1f8c85ff6b9d329 Mon Sep 17 00:00:00 2001
From: appeasementPolitik
<108810900+appeasementPolitik@users.noreply.github.com>
Date: Tue, 23 Jul 2024 11:19:28 +0200
Subject: [PATCH] Implement thetubster's changes (fast retry and bug fixes)
---
.local/bin/statusbar/sb-forecast | 21 +++++++++++++------
.local/bin/statusbar/sb-iplocate | 16 ++++++++++++---
.local/bin/statusbar/sb-moonphase | 25 ++++++++++++++++++-----
.local/bin/statusbar/sb-price | 34 ++++++++++++++++++++++---------
4 files changed, 72 insertions(+), 24 deletions(-)
diff --git a/.local/bin/statusbar/sb-forecast b/.local/bin/statusbar/sb-forecast
index 028c20f5..645e7a2f 100755
--- a/.local/bin/statusbar/sb-forecast
+++ b/.local/bin/statusbar/sb-forecast
@@ -7,7 +7,9 @@ url="${WTTRURL:-wttr.in}"
weatherreport="${XDG_CACHE_HOME:-$HOME/.cache}/weatherreport"
# Get a weather report from 'wttr.in' and save it locally.
-getforecast() { curl -sf "$url/$LOCATION" --output "$weatherreport" && touch "$weatherreport"; }
+getforecast() { { grep -q -m1 '^up$' /sys/class/net/w*/operstate || grep -q -m1 '^up$' /sys/class/net/e*/operstate; } &&
+ curl -sf "$url/$LOCATION" --output "$weatherreport" && touch "$weatherreport"
+}
# Forecast should be updated only once a day.
checkforecast() {
@@ -34,6 +36,7 @@ readfile() { weatherdata="$(cat "$weatherreport")" ;}
showweather() {
readfile
+ # shellcheck disable=SC2046,SC2183
printf "ā%s š„¶%s° š%s°\n" "$(getprecipchance)" $(getdailyhighlow)
}
@@ -45,12 +48,18 @@ case $BLOCK_BUTTON in
ā: Chance of rain/snow
š„¶: Daily low
š: Daily high" ;;
- 6) "$TERMINAL" -e "$EDITOR" "$0" ;;
+ 6) setsid -f "$TERMINAL" -e "$EDITOR" "$0" ;;
esac
+# shellcheck disable=SC2015
checkforecast && showweather ||
- ( flock -n 9 &&
- ( until { [ "$(cat /sys/class/net/w*/operstate)" = 'up' ] || [ "$(cat /sys/class/net/e*/operstate)" = 'up' ]; } &&
- getforecast; do sleep 60; done &&
- pkill -RTMIN+"${1:-5}" "${STATUSBAR:-dwmblocks}" ) &
+ ( flock -n 9 &&
+ ( tries=0; while [ $tries -ne 100 ]; do
+ getforecast && break ||
+ { tries=$((tries+1)); sleep .1; }
+ done
+ ! checkforecast &&
+ until getforecast; do sleep 60; done
+ pkill -RTMIN+"${1:-5}" "${STATUSBAR:-dwmblocks}"
+ ) &
echo ) 9>"${XDG_RUNTIME_DIR}/sb-forecast.lock"
diff --git a/.local/bin/statusbar/sb-iplocate b/.local/bin/statusbar/sb-iplocate
index 7911b503..a1606e11 100755
--- a/.local/bin/statusbar/sb-iplocate
+++ b/.local/bin/statusbar/sb-iplocate
@@ -7,12 +7,22 @@
ifinstalled "geoip" || exit 1
+getip() {
+ { grep -q -m1 '^up$' /sys/class/net/w*/operstate || grep -q -m1 '^up$' /sys/class/net/e*/operstate; } &&
+ curl -sf api.ipify.org --output "$ipfile"
+}
+
ipfile="$XDG_RUNTIME_DIR/iplocate"
addr=$(cat "$ipfile" 2>/dev/null) && addr=$(geoiplookup "$addr" 2>/dev/null) && rm "$ipfile" ||
( flock -n 9 &&
- ( until { [ "$(cat /sys/class/net/w*/operstate)" = 'up' ] || [ "$(cat /sys/class/net/e*/operstate)" = 'up' ]; } &&
- curl -sf api.ipify.org --output "$ipfile"; do sleep 60; done &&
- pkill -RTMIN+"${1:-27}" "${STATUSBAR:-dwmblocks}" ) &
+ ( tries=0; while [ $tries -ne 100 ]; do
+ getip && break ||
+ { tries=$((tries+1)); sleep .1; }
+ done
+ ! [ -f "$ipfile" ] &&
+ until getip; do sleep 60; done &&
+ pkill -RTMIN+"${1:-27}" "${STATUSBAR:-dwmblocks}"
+ ) &
echo; exit ) 9>"${XDG_RUNTIME_DIR}/sb-iplocate.lock"
name="${addr##*, }"
flag="$(grep "flag: $name" "${XDG_DATA_HOME:-$HOME/.local/share}/larbs/emoji")"
diff --git a/.local/bin/statusbar/sb-moonphase b/.local/bin/statusbar/sb-moonphase
index 3d77bf9e..145c1b76 100755
--- a/.local/bin/statusbar/sb-moonphase
+++ b/.local/bin/statusbar/sb-moonphase
@@ -4,11 +4,26 @@
moonfile="${XDG_DATA_HOME:-$HOME/.local/share}/moonphase"
-[ "$(stat -c %y "$moonfile" 2>/dev/null | cut -d' ' -f1)" = "$(date '+%Y-%m-%d')" ] ||
+checkmoon() {
+ [ "$(stat -c %y "$moonfile" 2>/dev/null |
+ cut -d' ' -f1)" = "$(date '+%Y-%m-%d')" ]
+}
+
+getmoon() { { grep -q -m1 '^up$' /sys/class/net/w*/operstate || grep -q -m1 '^up$' /sys/class/net/e*/operstate; } &&
+ curl -sf "wttr.in/?format=%m" --output "$moonfile" && touch "$moonfile"
+}
+
+checkmoon ||
( flock -n 9 &&
- ( until { [ "$(cat /sys/class/net/w*/operstate)" = 'up' ] || [ "$(cat /sys/class/net/e*/operstate)" = 'up' ]; } &&
- curl -sf "wttr.in/?format=%m" --output "$moonfile" && touch "$moonfile"; do sleep 60; done &&
- pkill -RTMIN+"${1:-17}" "${STATUSBAR:-dwmblocks}" ) &
+ ( tries=0; while [ $tries -ne 100 ]; do
+ # shellcheck disable=SC2015
+ getmoon && break ||
+ { tries=$((tries+1)); sleep .1; }
+ done
+ ! checkmoon &&
+ until getmoon; do sleep 60; done
+ pkill -RTMIN+"${1:-17}" "${STATUSBAR:-dwmblocks}"
+ ) &
echo; exit ) 9>"${XDG_RUNTIME_DIR}/sb-moonphase.lock"
icon="$(cat "$moonfile")"
@@ -37,5 +52,5 @@ case $BLOCK_BUTTON in
- š: Waning Gibbous
- š: Last Quarter
- š: Waning Crescent" ;;
- 6) "$TERMINAL" -e "$EDITOR" "$0" ;;
+ 6) setsid -f "$TERMINAL" -e "$EDITOR" "$0" ;;
esac
diff --git a/.local/bin/statusbar/sb-price b/.local/bin/statusbar/sb-price
index 29bd497f..68f5146f 100755
--- a/.local/bin/statusbar/sb-price
+++ b/.local/bin/statusbar/sb-price
@@ -24,39 +24,53 @@ interval="@14d" # History contained in chart preceded by '@' (7d = 7 days)
dir="${XDG_CACHE_HOME:-$HOME/.cache}/crypto-prices"
pricefile="$dir/$target-$denom"
chartfile="$dir/$target-$denom-chart"
-filestat="$(stat -c %x "$pricefile" 2>/dev/null)"
[ -d "$dir" ] || mkdir -p "$dir"
-updateprice() { curl -sf --fail-early $denom.$url/{1$target,$target$interval} --output "$pricefile" --output "$chartfile" && touch "$pricefile" "$chartfile"; }
+checkprice() {
+ [ "$(stat -c %y "$pricefile" 2>/dev/null |
+ cut -d' ' -f1)" = "$(date '+%Y-%m-%d')" ]
+}
-[ "${filestat%% *}" != "$(date '+%Y-%m-%d')" ] &&
- updateme="1"
+updateprice() { { grep -q -m1 '^up$' /sys/class/net/w*/operstate || grep -q -m1 '^up$' /sys/class/net/e*/operstate; } &&
+ curl -sf \
+ --fail-early "${denom}.${url}/1${target}" "${denom}.${url}/${target}${interval}" \
+ --output "$pricefile" --output "$chartfile" &&
+ touch "$pricefile" "$chartfile"
+}
+
+checkprice || updateme="1"
case $BLOCK_BUTTON in
1) setsid "$TERMINAL" -e less -Srf "$chartfile" ;;
2) notify-send -u low "$icon Updating..." "Updating $name price..." ; updateme="1" ; showupdate="1" ;;
- 3) uptime="$(date -d "$filestat" '+%D at %T' | sed "s|$(date '+%D')|Today|")"
+ 3) uptime="$(date -d "$(stat -c %x "$pricefile" 2>/dev/null)" '+%D at %T' | sed "s|$(date '+%D')|Today|")"
notify-send "$icon $name module" "\- Exact price: \$$(cat "$pricefile")
- Left click for chart of changes.
- Middle click to update.
- Shows š if updating prices.
- Last updated:
$uptime" ;;
- 6) "$TERMINAL" -e "$EDITOR" "$0" ;;
+ 6) setsid -f "$TERMINAL" -e "$EDITOR" "$0" ;;
esac
[ -n "$updateme" ] &&
if [ -n "$showupdate" ]; then
updateprice && notify-send "$icon Update complete." "$name price is now \$$(cat "$pricefile")"
else
+ # shellcheck disable=SC2015
[ -n "$4" ] &&
( flock -n 9 &&
- ( until { [ "$(cat /sys/class/net/w*/operstate)" = 'up' ] || [ "$(cat /sys/class/net/e*/operstate)" = 'up' ]; } &&
- updateprice; do sleep 60; done &&
- pkill -RTMIN+"$4" "${STATUSBAR:-dwmblocks}" ) &
+ ( tries=0; while [ $tries -ne 100 ]; do
+ updateprice && break ||
+ { tries=$((tries+1)); sleep .1; }
+ done
+ ! checkprice &&
+ until updateprice; do sleep 60; done
+ pkill -RTMIN+"$4" "${STATUSBAR:-dwmblocks}"
+ ) &
echo; exit ) 9>"${XDG_RUNTIME_DIR}/sb-price.lock" ||
- updateprice
+ updateprice
fi
[ -f "$pricefile" ] && printf "%s%s%0.2f" "$icon" "$symb" "$(cat "$pricefile")"