From 30fe3ab230a66f98dd8ece253bf79c61c2fbf6e1 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Fri, 20 Feb 2026 07:06:47 -0600 Subject: fix: mode-line: validate API response before updating display Reject empty or malformed wttr.in responses to prevent blank icon and tooltip. Fix tooltip lambda to treat empty string as falsy. Add debug logging for nil-location and invalid-response paths. --- wttrin.el | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) (limited to 'wttrin.el') diff --git a/wttrin.el b/wttrin.el index 2eed429..d6e9e90 100644 --- a/wttrin.el +++ b/wttrin.el @@ -476,12 +476,22 @@ This creates headroom to avoid frequent cleanups." ;;; Mode-line weather display +(defun wttrin--mode-line-valid-response-p (weather-string) + "Return non-nil if WEATHER-STRING looks like a valid mode-line response. +Expected format: \"Location: emoji temp conditions\", +e.g., \"Paris: ☀️ +61°F Clear\"." + (and (stringp weather-string) + (not (string-empty-p weather-string)) + (string-match-p ":" weather-string))) + (defun wttrin--mode-line-fetch-weather () "Fetch weather for favorite location and update mode-line display. Uses wttr.in custom format for concise weather with emoji." (when (featurep 'wttrin-debug) (wttrin--debug-log "mode-line-fetch: Starting fetch for %s" wttrin-favorite-location)) - (when wttrin-favorite-location + (if (not wttrin-favorite-location) + (when (featurep 'wttrin-debug) + (wttrin--debug-log "mode-line-fetch: No favorite location set, skipping")) (let* ((location wttrin-favorite-location) ;; Custom format: location + emoji + temp + conditions ;; %l=location, %c=weather emoji, %t=temp, %C=conditions @@ -501,7 +511,10 @@ Uses wttr.in custom format for concise weather with emoji." (let ((trimmed-data (string-trim data))) (when (featurep 'wttrin-debug) (wttrin--debug-log "mode-line-fetch: Received data = %S" trimmed-data)) - (wttrin--mode-line-update-display trimmed-data)) + (if (wttrin--mode-line-valid-response-p trimmed-data) + (wttrin--mode-line-update-display trimmed-data) + (when (featurep 'wttrin-debug) + (wttrin--debug-log "mode-line-fetch: Invalid response, keeping previous display")))) (when (featurep 'wttrin-debug) (wttrin--debug-log "mode-line-fetch: No data received (network error)")))))))) @@ -532,9 +545,11 @@ e.g., \"Paris: ☀️ +61°F Clear\"." (setq wttrin-mode-line-string (propertize (concat " " emoji-with-font) 'help-echo (lambda (_window _object _pos) - (or wttrin--mode-line-tooltip-data + (let ((tip wttrin--mode-line-tooltip-data)) + (if (and tip (not (string-empty-p tip))) + tip (format "Weather for %s\nClick to refresh" - wttrin-favorite-location))) + wttrin-favorite-location)))) 'mouse-face 'mode-line-highlight 'local-map wttrin--mode-line-map))) (force-mode-line-update t) -- cgit v1.2.3