在Emacs中显示天气
wttr.in is a console-oriented weather forecast service that supports various information representation methods like terminal-oriented ANSI-sequences for console HTTP clients (curl, httpie, or wget), HTML for web browsers, or PNG for graphical viewers.
在Emacs中,可以通过调用命令 curl -s "wttr.in/WuHan?format=4&M" 来获取单行输出的天气信息,为避免阻塞其他任务,需要采用异步调用的方式,通过定时器周期去更新天气。
(defvar +echo-bar--weather-string "") (defvar +echo-bar--weather-url "wttr.in/WuHan?format=4&M") (defun +echo-bar--weather-update () (let* ((output-buffer (generate-new-buffer "*weather-output*")) ;; Start the process, directing output to `output-buffer` (proc (start-process "weather-proc" output-buffer "curl" "-s" +echo-bar--weather-url))) ;; Process filter: append output to the process's associated buffer ;; (set-process-filter ;; proc ;; (lambda (p string) ;; (with-current-buffer (process-buffer p) ;; (insert string)))) ;; Process sentinel: called when the process changes state (e.g., finished) (set-process-sentinel proc (lambda (p event) (let ((buffer (process-buffer p))) (cond ((and (memq (process-status p) '(exit signal)) (/= (process-exit-status p) 0)) (message "Weather update error") (when (buffer-live-p buffer) (kill-buffer buffer))) ((cl-search "finished" event) (let ((result (replace-regexp-in-string "[ \t\n\r]+" "" (with-current-buffer buffer (buffer-string))))) (when (length< result 30) (setq +echo-bar--weather-string result))) (when (buffer-live-p buffer) (kill-buffer buffer))))))))) (run-at-time nil 1800 #'+echo-bar--weather-update)
成功获取的天气信息 WuHan:🌫🌡️+4°C🌬️↗1.7m/s ,可以显示到 mode-line 或者 echo-bar 中。