From bf9c74666d667c251518b1959f23dd5427c39b9c Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Thu, 11 Mar 2021 06:53:21 +0100 Subject: [PATCH] Factor out the JSON string generation --- org-clock-waybar.el | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/org-clock-waybar.el b/org-clock-waybar.el index 79ae241..ef084d7 100644 --- a/org-clock-waybar.el +++ b/org-clock-waybar.el @@ -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 task’s data to BUFFER. +(defun org-clock-waybar--get-clocked-task-json () + "Get the currently clocked-in task’s 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 ()