Skip to content

Commit f5da1f6

Browse files
authored
Implement async dwmblocks modules + fast retry for 5s: LukeSmithxyz#1296
1 parent 87f57a9 commit f5da1f6

File tree

4 files changed

+83
-20
lines changed

4 files changed

+83
-20
lines changed

.local/bin/statusbar/sb-forecast

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ url="${WTTRURL:-wttr.in}"
77
weatherreport="${XDG_CACHE_HOME:-$HOME/.cache}/weatherreport"
88

99
# Get a weather report from 'wttr.in' and save it locally.
10-
getforecast() { timeout --signal=1 2s curl -sf "$url/$LOCATION" > "$weatherreport" || exit 1; }
10+
getforecast() { { [ "$(cat /sys/class/net/w*/operstate)" = 'up' ] || [ "$(cat /sys/class/net/e*/operstate)" = 'up' ]; } &&
11+
curl -sf "$url/$LOCATION" --output "$weatherreport" && touch "$weatherreport"
12+
}
1113

1214
# Forecast should be updated only once a day.
1315
checkforecast() {
14-
[ -s "$weatherreport" ] && [ "$(stat -c %y "$weatherreport" 2>/dev/null |
16+
[ "$(stat -c %y "$weatherreport" 2>/dev/null |
1517
cut -d' ' -f1)" = "$(date '+%Y-%m-%d')" ]
1618
}
1719

@@ -34,6 +36,7 @@ readfile() { weatherdata="$(cat "$weatherreport")" ;}
3436

3537
showweather() {
3638
readfile
39+
# shellcheck disable=SC2046,SC2183
3740
printf "☔%s 🥶%s° 🌞%s°\n" "$(getprecipchance)" $(getdailyhighlow)
3841
}
3942

@@ -48,6 +51,15 @@ case $BLOCK_BUTTON in
4851
6) setsid -f "$TERMINAL" -e "$EDITOR" "$0" ;;
4952
esac
5053

51-
checkforecast || getforecast
52-
53-
showweather
54+
# shellcheck disable=SC2015
55+
checkforecast && showweather ||
56+
( flock -n 9 &&
57+
( tries=0; while [ $tries -ne 50 ]; do
58+
getforecast && break ||
59+
{ tries=$((tries+1)); sleep .1; }
60+
done
61+
! checkforecast &&
62+
until getforecast; do sleep 60; done
63+
pkill -RTMIN+"${1:-5}" "${STATUSBAR:-dwmblocks}"
64+
) &
65+
echo ) 9>"${XDG_RUNTIME_DIR}/sb-forecast.lock"

.local/bin/statusbar/sb-iplocate

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,25 @@
55
#
66
# https://www.maketecheasier.com/ip-address-geolocation-lookups-linux/
77

8-
set -e
8+
ifinstalled "geoip" || exit 1
99

10-
ifinstalled "geoip"
11-
addr="$(geoiplookup "$(curl -sfm 1 ifconfig.me 2>/dev/null)")"
10+
getip() {
11+
{ [ "$(cat /sys/class/net/w*/operstate)" = 'up' ] || [ "$(cat /sys/class/net/e*/operstate)" = 'up' ]; } &&
12+
curl -sf api.ipify.org --output "$ipfile"
13+
}
14+
15+
ipfile="$XDG_RUNTIME_DIR/iplocate"
16+
addr=$(cat "$ipfile" 2>/dev/null) && addr=$(geoiplookup "$addr" 2>/dev/null) && rm "$ipfile" ||
17+
( flock -n 9 &&
18+
( tries=0; while [ $tries -ne 50 ]; do
19+
getip && break ||
20+
{ tries=$((tries+1)); sleep .1; }
21+
done
22+
! [ -f "$ipfile" ] &&
23+
until getip; do sleep 60; done &&
24+
pkill -RTMIN+"${1:-27}" "${STATUSBAR:-dwmblocks}"
25+
) &
26+
echo; exit ) 9>"${XDG_RUNTIME_DIR}/sb-iplocate.lock"
1227
name="${addr##*, }"
1328
flag="$(grep "flag: $name" "${XDG_DATA_HOME:-$HOME/.local/share}/larbs/emoji")"
1429
flag="${flag%% *}"

.local/bin/statusbar/sb-moonphase

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,27 @@
44

55
moonfile="${XDG_DATA_HOME:-$HOME/.local/share}/moonphase"
66

7-
[ -s "$moonfile" ] && [ "$(stat -c %y "$moonfile" 2>/dev/null | cut -d' ' -f1)" = "$(date '+%Y-%m-%d')" ] ||
8-
{ curl -sf "wttr.in/?format=%m" > "$moonfile" || exit 1 ;}
7+
checkmoon() {
8+
[ "$(stat -c %y "$moonfile" 2>/dev/null |
9+
cut -d' ' -f1)" = "$(date '+%Y-%m-%d')" ]
10+
}
11+
12+
getmoon() { { [ "$(cat /sys/class/net/w*/operstate)" = 'up' ] || [ "$(cat /sys/class/net/e*/operstate)" = 'up' ]; } &&
13+
curl -sf "wttr.in/?format=%m" --output "$moonfile" && touch "$moonfile"
14+
}
15+
16+
checkmoon ||
17+
( flock -n 9 &&
18+
( tries=0; while [ $tries -ne 50 ]; do
19+
# shellcheck disable=SC2015
20+
getmoon && break ||
21+
{ tries=$((tries+1)); sleep .1; }
22+
done
23+
! checkmoon &&
24+
until getmoon; do sleep 60; done
25+
pkill -RTMIN+"${1:-17}" "${STATUSBAR:-dwmblocks}"
26+
) &
27+
echo; exit ) 9>"${XDG_RUNTIME_DIR}/sb-moonphase.lock"
928

1029
icon="$(cat "$moonfile")"
1130

.local/bin/statusbar/sb-price

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,24 @@ interval="@14d" # History contained in chart preceded by '@' (7d = 7 days)
2424
dir="${XDG_CACHE_HOME:-$HOME/.cache}/crypto-prices"
2525
pricefile="$dir/$target-$denom"
2626
chartfile="$dir/$target-$denom-chart"
27-
filestat="$(stat -c %x "$pricefile" 2>/dev/null)"
2827

2928
[ -d "$dir" ] || mkdir -p "$dir"
3029

31-
updateprice() { curl -sf -m 1 --fail-early $denom.$url/{1$target,$target$interval} --output "$pricefile" --output "$chartfile" ||
32-
rm -f "$pricefile" "$chartfile" ;}
30+
checkprice() {
31+
[ "$(stat -c %y "$pricefile" 2>/dev/null |
32+
cut -d' ' -f1)" = "$(date '+%Y-%m-%d')" ]
33+
}
3334

34-
[ "${filestat%% *}" != "$(date '+%Y-%m-%d')" ] &&
35-
updateme="1"
35+
updateprice() { { [ "$(cat /sys/class/net/w*/operstate)" = 'up' ] || [ "$(cat /sys/class/net/e*/operstate)" = 'up' ]; } &&
36+
curl -sf --fail-early $denom.$url/{1$target,$target$interval} --output "$pricefile" --output "$chartfile" && touch "$pricefile" "$chartfile"
37+
}
38+
39+
checkprice || updateme="1"
3640

3741
case $BLOCK_BUTTON in
3842
1) setsid "$TERMINAL" -e less -Srf "$chartfile" ;;
3943
2) notify-send -u low "$icon Updating..." "Updating $name price..." ; updateme="1" ; showupdate="1" ;;
40-
3) uptime="$(date -d "$filestat" '+%D at %T' | sed "s|$(date '+%D')|Today|")"
44+
3) uptime="$(date -d "$(stat -c %x "$pricefile" 2>/dev/null)" '+%D at %T' | sed "s|$(date '+%D')|Today|")"
4145
notify-send "$icon $name module" "\- <b>Exact price: \$$(cat "$pricefile")</b>
4246
- Left click for chart of changes.
4347
- Middle click to update.
@@ -48,9 +52,22 @@ case $BLOCK_BUTTON in
4852
esac
4953

5054
[ -n "$updateme" ] &&
51-
updateprice "$target" &&
52-
[ -n "$showupdate" ] &&
53-
notify-send "$icon Update complete." "$name price is now
54-
\$$(cat "$pricefile")"
55+
if [ -n "$showupdate" ]; then
56+
updateprice && notify-send "$icon Update complete." "$name price is now \$$(cat "$pricefile")"
57+
else
58+
# shellcheck disable=SC2015
59+
[ -n "$4" ] &&
60+
( flock -n 9 &&
61+
( tries=0; while [ $tries -ne 50 ]; do
62+
updateprice && break ||
63+
{ tries=$((tries+1)); sleep .1; }
64+
done
65+
! checkprice &&
66+
until updateprice; do sleep 60; done
67+
pkill -RTMIN+"$4" "${STATUSBAR:-dwmblocks}"
68+
) &
69+
echo; exit ) 9>"${XDG_RUNTIME_DIR}/sb-price.lock" ||
70+
updateprice
71+
fi
5572

5673
[ -f "$pricefile" ] && printf "%s%s%0.2f" "$icon" "$symb" "$(cat "$pricefile")"

0 commit comments

Comments
 (0)