Skip to content

Commit

Permalink
fix: Remove flickering and fix when fetching more than one location
Browse files Browse the repository at this point in the history
  • Loading branch information
josegpt committed Feb 27, 2022
1 parent cad349f commit 7924448
Showing 1 changed file with 30 additions and 13 deletions.
43 changes: 30 additions & 13 deletions display-wttr.el
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,14 @@ It should not be set directly, but is instead updated by the
`display-wttr' function.")
;;;###autoload(put 'display-wttr-string 'risky-local-variable t)

(defvar display-wttr-list nil
"List of wttr unprocessed results.
In this way a flash is avoided when updating
`display-wttr-string'.
It should not be set directly, but is
instead updated by the `display-wttr' function.")
;;;###autoload(put 'display-wttr-list 'risky-local-variable t)

(defvar display-wttr-timer nil
"Timer used by wttr")
;;;###autoload(put 'display-wttr-timer 'risky-local-variable t)
Expand All @@ -150,14 +158,30 @@ It should not be set directly, but is instead updated by the
display-wttr-location
display-wttr-format))

(defun display-wttr-update (proc string)
(defun display-wttr-sentinel (process event)
"Update `display-wttr-string' only when the fetcher is done."
(when (string= "finished\n" event)
(setq display-wttr-string (string-join display-wttr-list " "))))

(defun display-wttr-filter (proc string)
"Update the `display-wttr' info in the mode line."
(setq display-wttr-string
(concat display-wttr-string
(string-join (split-string string) " ") " "))
(add-to-list 'display-wttr-list
(string-join (split-string string) " ") t)
(run-hooks 'display-wttr-hook)
(force-mode-line-update 'all))

(defun display-wttr-update ()
"Creates a new background process to update wttr string."
(make-process
:name "display-wttr"
:command `("sh" "-c"
,(format "%s %s %s"
display-wttr-fetch-executable
display-wttr-fetch-options
(display-wttr-fetch-url)))
:filter 'display-wttr-filter
:sentinel 'display-wttr-sentinel ))

(defun display-wttr-event-handler ()
"Updates wttr in mode line.
Calcalutes and sets up the timer for the next update of wttr with
Expand Down Expand Up @@ -193,6 +217,7 @@ control the number of seconds between updates by customizing
;; Cancel timer if any is running
(and display-wttr-timer (cancel-timer display-wttr-timer))
(setq display-wttr-string "")
(setq display-wttr-list nil)
(or global-mode-string (setq global-mode-string '("")))
(when display-wttr-mode
(or (memq 'display-wttr-string global-mode-string)
Expand All @@ -202,15 +227,7 @@ control the number of seconds between updates by customizing
(setq display-wttr-timer
(run-at-time t display-wttr-interval
'display-wttr-event-handler))
;; Call background process to update string
(make-process
:name "display-wttr"
:command `("sh" "-c"
,(format "%s %s %s"
display-wttr-fetch-executable
display-wttr-fetch-options
(display-wttr-fetch-url)))
:filter 'display-wttr-update)))
(display-wttr-update)))

(provide 'display-wttr)
;;; display-wttr.el ends here

0 comments on commit 7924448

Please sign in to comment.