From fe198c960fb85850acbe819e12ca465cd6091bc3 Mon Sep 17 00:00:00 2001 From: HelionSmoker <82761116+HelionSmoker@users.noreply.github.com> Date: Sun, 12 Feb 2023 12:24:10 +0200 Subject: [PATCH 1/3] Rewrite sb-forecast --- .local/bin/statusbar/sb-forecast | 52 ++++++++++++++++++++------------ 1 file changed, 33 insertions(+), 19 deletions(-) diff --git a/.local/bin/statusbar/sb-forecast b/.local/bin/statusbar/sb-forecast index 9744ea4..59f0497 100755 --- a/.local/bin/statusbar/sb-forecast +++ b/.local/bin/statusbar/sb-forecast @@ -1,24 +1,41 @@ #!/bin/sh -# Displays todays precipication chance (ā˜”) and daily low (šŸ„¶) and high (šŸŒž). +# Displays today's precipication chance (ā˜”), and daily low (šŸ„¶) and high (šŸŒž). # Usually intended for the statusbar. -# If we have internet, get a weather report from wttr.in and store it locally. -# You could set up a shell alias to view the full file in a pager in the -# terminal if desired. This function will only be run once a day when needed. -weatherreport="${XDG_CACHE_HOME:-$HOME/.cache}/weatherreport" -getforecast() { curl -sf "wttr.in/$LOCATION" > "$weatherreport" || exit 1 ;} +weather_report="${XDG_CACHE_HOME:-$HOME/.cache}/weatherreport" -# Some very particular and terse stream manipulation. We get the maximum -# precipitation chance and the daily high and low from the downloaded file and -# display them with coresponding emojis. -showweather() { printf "%s" "$(sed '16q;d' "$weatherreport" | - grep -wo "[0-9]*%" | sort -rn | sed "s/^/ā˜”/g;1q" | tr -d '\n')" -sed '13q;d' "$weatherreport" | grep -o "m\\([-+]\\)*[0-9]\\+" | sed 's/+//g' | sort -n -t 'm' -k 2n | sed -e 1b -e '$!d' | tr '\n|m' ' ' | awk '{print " šŸ„¶" $1 "Ā°","šŸŒž" $2 "Ā°"}' ;} +# Get a weather report from 'wttr.in' and save it locally. +get_forecast() { curl -sf "wttr.in/$LOCATION" > "$weather_report" || exit 1; } + +# Forecast should be updated only once a day +check_forecast() { + [ -s "$weather_report" ] && [ "$(stat -c %y "$weather_report" 2>/dev/null | + cut -d' ' -f1)" = "$(date '+%Y-%m-%d')" ] +} + +get_precip_chance() { + sed '16q;d' "$weather_report" | # Extract line 16 from file + grep -wo "[0-9]*%" | # Find a sequence of digits followed by '%' + sort -rn | # Sort in descending order + head -1q # Extract first line +} + +get_daily_high_low() { + sed '13q;d' "$weather_report" | # Extract line 13 from file + grep -o "m\\([-+]\\)*[0-9]\\+" | # Find temperatures in the format "m" + sed 's/[+m]//g' | # Remove '+' and 'm' + sort -n -k 2n | # Sort in ascending order + sed -e 1b -e '$!d' # Extract the first and last lines +} + +show_weather() { + printf "ā˜”%s šŸ„¶%sĀ° šŸŒž%sĀ°\n" "$(get_precip_chance)" $(get_daily_high_low) +} case $BLOCK_BUTTON in - 1) setsid -f "$TERMINAL" -e less -Srf "$weatherreport" ;; - 2) getforecast && showweather ;; + 1) setsid -f "$TERMINAL" -e less -Srf "$weather_report" ;; + 2) get_forecast && show_weather ;; 3) notify-send "šŸŒˆ Weather module" "\- Left click for full forecast. - Middle click to update forecast. ā˜”: Chance of rain/snow @@ -27,9 +44,6 @@ case $BLOCK_BUTTON in 6) "$TERMINAL" -e "$EDITOR" "$0" ;; esac -# The test if our forcecast is updated to the day. If it isn't download a new -# weather report from wttr.in with the above function. -[ -s "$weatherreport" ] && [ "$(stat -c %y "$weatherreport" 2>/dev/null | cut -d' ' -f1)" = "$(date '+%Y-%m-%d')" ] || - getforecast +check_forecast || get_forecast -showweather +show_weather From d8f386d5123de66db7449bf1152ed86d84caaf3b Mon Sep 17 00:00:00 2001 From: HelionSmoker <82761116+HelionSmoker@users.noreply.github.com> Date: Sun, 12 Feb 2023 13:33:34 +0200 Subject: [PATCH 2/3] Switch to kebab-case for file name --- .local/bin/statusbar/sb-forecast | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.local/bin/statusbar/sb-forecast b/.local/bin/statusbar/sb-forecast index 59f0497..8aab144 100755 --- a/.local/bin/statusbar/sb-forecast +++ b/.local/bin/statusbar/sb-forecast @@ -3,7 +3,7 @@ # Displays today's precipication chance (ā˜”), and daily low (šŸ„¶) and high (šŸŒž). # Usually intended for the statusbar. -weather_report="${XDG_CACHE_HOME:-$HOME/.cache}/weatherreport" +weather_report="${XDG_CACHE_HOME:-$HOME/.cache}/weather-report" # Get a weather report from 'wttr.in' and save it locally. get_forecast() { curl -sf "wttr.in/$LOCATION" > "$weather_report" || exit 1; } From de4b34cd32bd74c251ef13c28abfca32b959a873 Mon Sep 17 00:00:00 2001 From: Luke Smith Date: Sun, 12 Feb 2023 09:00:23 -0500 Subject: [PATCH 3/3] read file only once --- .local/bin/statusbar/sb-forecast | 33 +++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/.local/bin/statusbar/sb-forecast b/.local/bin/statusbar/sb-forecast index 8aab144..e28f22f 100755 --- a/.local/bin/statusbar/sb-forecast +++ b/.local/bin/statusbar/sb-forecast @@ -3,39 +3,42 @@ # Displays today's precipication chance (ā˜”), and daily low (šŸ„¶) and high (šŸŒž). # Usually intended for the statusbar. -weather_report="${XDG_CACHE_HOME:-$HOME/.cache}/weather-report" +weatherreport="${XDG_CACHE_HOME:-$HOME/.cache}/weatherreport" # Get a weather report from 'wttr.in' and save it locally. -get_forecast() { curl -sf "wttr.in/$LOCATION" > "$weather_report" || exit 1; } +getforecast() { curl -sf "wttr.in/$LOCATION" > "$weatherreport" || exit 1; } -# Forecast should be updated only once a day -check_forecast() { - [ -s "$weather_report" ] && [ "$(stat -c %y "$weather_report" 2>/dev/null | +# Forecast should be updated only once a day. +checkforecast() { + [ -s "$weatherreport" ] && [ "$(stat -c %y "$weatherreport" 2>/dev/null | cut -d' ' -f1)" = "$(date '+%Y-%m-%d')" ] } -get_precip_chance() { - sed '16q;d' "$weather_report" | # Extract line 16 from file +getprecipchance() { + echo "$weatherdata" | sed '16q;d' | # Extract line 16 from file grep -wo "[0-9]*%" | # Find a sequence of digits followed by '%' sort -rn | # Sort in descending order head -1q # Extract first line } -get_daily_high_low() { - sed '13q;d' "$weather_report" | # Extract line 13 from file +getdailyhighlow() { + echo "$weatherdata" | sed '13q;d' | # Extract line 13 from file grep -o "m\\([-+]\\)*[0-9]\\+" | # Find temperatures in the format "m" sed 's/[+m]//g' | # Remove '+' and 'm' sort -n -k 2n | # Sort in ascending order sed -e 1b -e '$!d' # Extract the first and last lines } -show_weather() { - printf "ā˜”%s šŸ„¶%sĀ° šŸŒž%sĀ°\n" "$(get_precip_chance)" $(get_daily_high_low) +readfile() { weatherdata="$(cat "$weatherreport")" ;} + +showweather() { + readfile + printf "ā˜”%s šŸ„¶%sĀ° šŸŒž%sĀ°\n" "$(getprecipchance)" $(getdailyhighlow) } case $BLOCK_BUTTON in - 1) setsid -f "$TERMINAL" -e less -Srf "$weather_report" ;; - 2) get_forecast && show_weather ;; + 1) setsid -f "$TERMINAL" -e less -Srf "$weatherreport" ;; + 2) getforecast && showweather ;; 3) notify-send "šŸŒˆ Weather module" "\- Left click for full forecast. - Middle click to update forecast. ā˜”: Chance of rain/snow @@ -44,6 +47,6 @@ case $BLOCK_BUTTON in 6) "$TERMINAL" -e "$EDITOR" "$0" ;; esac -check_forecast || get_forecast +checkforecast || getforecast -show_weather +showweather