From 839ef7c4ef10e873f68a14ba48db4306f52dd8e7 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Tue, 4 Nov 2025 10:28:20 -0600 Subject: feat:debug: Add debugging utilities for wttrin Introduce debugging features to assist with wttrin display issues. New utility functions enable developers to view raw weather data with line numbers, facilitate easy debugging of header parsing. Includes debug mode toggle functions to save raw weather responses to timestamped files for bug reporting and diagnostics. Updated URL-building tests to confirm correct URL formats with additional 'F' parameter, ensuring comprehensive coverage for various input scenarios. --- debug-wttrin.el | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ wttrin.el | 49 ++++++++++++++++++++++++++++++++----------------- 2 files changed, 84 insertions(+), 17 deletions(-) create mode 100644 debug-wttrin.el diff --git a/debug-wttrin.el b/debug-wttrin.el new file mode 100644 index 0000000..8b8798b --- /dev/null +++ b/debug-wttrin.el @@ -0,0 +1,52 @@ +;;; debug-wttrin.el --- Debug helper for wttrin -*- lexical-binding: t -*- +;; +;; This file provides utilities for debugging wttrin display issues. +;; +;;; Commentary: +;; +;; To enable debug mode: +;; (setq wttrin-debug t) +;; +;; This will save raw weather responses to timestamped files in your +;; temp directory for bug reports. +;; +;; To view raw data with line numbers for development: +;; M-x debug-wttrin-show-raw RET RET +;; +;;; Code: + +(require 'wttrin) + +(defun debug-wttrin-show-raw (location) + "Fetch and display raw wttr.in data for LOCATION with line numbers. +This is useful for debugging header parsing issues." + (interactive "sLocation: ") + (let ((raw-string (wttrin--get-cached-or-fetch location))) + (with-current-buffer (get-buffer-create "*wttrin-debug*") + (erase-buffer) + (insert raw-string) + (goto-char (point-min)) + (let ((line-num 1)) + (while (not (eobp)) + (beginning-of-line) + (insert (format "%2d: " line-num)) + (setq line-num (1+ line-num)) + (forward-line 1))) + (goto-char (point-min)) + (switch-to-buffer (current-buffer))))) + +(defun debug-wttrin-enable () + "Enable wttrin debug mode. +Raw weather data will be saved to timestamped files for bug reports." + (interactive) + (setq wttrin-debug t) + (message "Wttrin debug mode enabled. Raw data will be saved to: %s" temporary-file-directory)) + +(defun debug-wttrin-disable () + "Disable wttrin debug mode." + (interactive) + (setq wttrin-debug nil) + (message "Wttrin debug mode disabled")) + +(provide 'debug-wttrin) +;;; debug-wttrin.el ends here diff --git a/wttrin.el b/wttrin.el index ab65ff3..8fbaaeb 100644 --- a/wttrin.el +++ b/wttrin.el @@ -102,6 +102,13 @@ units (default)." :group 'wttrin :type 'boolean) +(defcustom wttrin-debug nil + "If non-nil, save raw weather data to timestamped files for debugging. +Raw data files are saved to `temporary-file-directory' with names like +wttrin-debug-YYYYMMDD-HHMMSS.txt for bug reports." + :group 'wttrin + :type 'boolean) + (defvar wttrin--cache (make-hash-table :test 'equal) "Cache for weather data: cache-key -> (timestamp . data).") @@ -209,12 +216,30 @@ Weather data is displayed in a read-only buffer with the following keybindings: :height ,wttrin-font-height)) (buffer-face-mode t)) +(defun wttrin--save-debug-data (location-name raw-string) + "Save RAW-STRING to a timestamped debug file for LOCATION-NAME. +Returns the path to the saved file." + (let* ((timestamp (format-time-string "%Y%m%d-%H%M%S")) + (filename (format "wttrin-debug-%s.txt" timestamp)) + (filepath (expand-file-name filename temporary-file-directory))) + (with-temp-file filepath + (insert (format "Location: %s\n" location-name)) + (insert (format "Timestamp: %s\n" (format-time-string "%Y-%m-%d %H:%M:%S"))) + (insert (format "wttrin-unit-system: %s\n" wttrin-unit-system)) + (insert "\n--- Raw Response ---\n\n") + (insert raw-string)) + (message "Debug data saved to: %s" filepath) + filepath)) + (defun wttrin--display-weather (location-name raw-string) "Display weather data RAW-STRING for LOCATION-NAME in weather buffer." + ;; Save debug data if enabled + (when wttrin-debug + (wttrin--save-debug-data location-name raw-string)) + (if (or (null raw-string) (string-match "ERROR" raw-string)) (message "Cannot retrieve weather data. Perhaps the location was misspelled?") - (let ((buffer (get-buffer-create (format "*wttr.in*"))) - date-time-stamp location-info) + (let ((buffer (get-buffer-create (format "*wttr.in*")))) (switch-to-buffer buffer) ;; Temporarily allow editing (in case mode is already active) @@ -222,24 +247,14 @@ Weather data is displayed in a read-only buffer with the following keybindings: (erase-buffer) (insert (xterm-color-filter raw-string)) - ;; rearrange header information - (goto-char (point-min)) - (forward-line 4) - (setq date-time-stamp (buffer-substring-no-properties - (line-beginning-position) (line-end-position))) + ;; Remove verbose Location: coordinate line (goto-char (point-min)) - (forward-line 6) - (setq location-info (buffer-substring-no-properties - (line-beginning-position) (line-end-position))) - (goto-char (point-min)) - (forward-line 8) - (delete-region (point-min) (line-beginning-position)) - - (insert "\n" location-info "\n" date-time-stamp "\n\n\n") + (while (re-search-forward "^\\s-*Location:.*\\[.*\\].*$" nil t) + (delete-region (line-beginning-position) (1+ (line-end-position)))) - ;; provide user instructions + ;; Add user instructions at the bottom (goto-char (point-max)) - (insert "\nPress: [g] to query another location [r] to refresh [q] to quit") + (insert "\n\nPress: [g] to query another location [r] to refresh [q] to quit") ;; align buffer to top (goto-char (point-min))) -- cgit v1.2.3