Factor out the JSON string generation

This commit is contained in:
Gergely Polonkai 2021-03-11 06:53:21 +01:00
parent 5d97d022c7
commit bf9c74666d
No known key found for this signature in database
GPG Key ID: 2D2885533B869ED4
1 changed files with 14 additions and 15 deletions

View File

@ -64,8 +64,8 @@
'emacs-mule)
"Coding system of the file `org-clock-waybar-filename'.")
(defun org-clock-waybar--get-clocked-task-json (buffer)
"Save the currently clocked-in tasks data to BUFFER.
(defun org-clock-waybar--get-clocked-task-json ()
"Get the currently clocked-in tasks data as a stringified JSON object.
The output is in JSON format constructed in a way so Waybar can process it.
If there is no clocked in task, alt becomes empty and text will be set to the
@ -75,25 +75,24 @@ value of `org-clock-waybar-not-clocked-in-text'."
(title (when org-clock-current-task
(substring-no-properties org-clock-current-task)))
(output (json-new-object)))
(with-current-buffer buffer
(setq output (json-add-to-object
output
"text"
(if title
title
org-clock-waybar-not-clocked-in-text)))
(setq output (json-add-to-object output "alt" (if category category "")))
(setq output (json-add-to-object output "tooltip" ""))
(setq output (json-add-to-object output "class" ""))
(setq output (json-add-to-object output "percentage" ""))
(insert (json-encode output)))))
(setq output (json-add-to-object
output
"text"
(if title
title
org-clock-waybar-not-clocked-in-text)))
(setq output (json-add-to-object output "alt" (if category category "")))
(setq output (json-add-to-object output "tooltip" ""))
(setq output (json-add-to-object output "class" ""))
(setq output (json-add-to-object output "percentage" ""))
(json-encode output)))
(defun org-clock-waybar-save-task ()
"Save the current clocked in task to `org-clock-waybar-filename'."
(with-temp-buffer
(erase-buffer)
(set-buffer-file-coding-system org-clock-waybar-filename-coding-system)
(org-clock-waybar--get-clocked-task-json (current-buffer))
(insert (org-clock-waybar--get-clocked-task-json))
(write-file org-clock-waybar-filename)))
(defun org-clock-waybar-setup ()