From 8754aa143424701e248c80c8ecd123888958d628 Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Wed, 10 Mar 2021 18:38:32 +0100 Subject: [PATCH] Initial, untested version --- org-clock-waybar.el | 59 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 org-clock-waybar.el diff --git a/org-clock-waybar.el b/org-clock-waybar.el new file mode 100644 index 0000000..24bafa4 --- /dev/null +++ b/org-clock-waybar.el @@ -0,0 +1,59 @@ +;;; org-clock-waybar --- Summary + +;;; Commentary: + +;;; Code: + +(require 'xdg) +(require 'org-clock) + +(defgroup org-clock-waybar nil + "Send current clocked task to a JSON file for Waybar visualization" + :group 'emacs) + +(defcustom org-clock-waybar-filename + (expand-file-name ".waybar-current-task.json" (xdg-cache-home)) + "Name of the file to save task data to." + :type 'string + :group 'org-clock-waybar) + +(defconst org-clock-waybar-filename-coding-system + (if (coding-system-p 'utf-8-emacs) + 'utf-8-emacs + 'emacs-mule) + "Coding system of the file `org-clock-waybar-filename'.") + +(defun org-clock-waybar--get-clocked-task-json (buffer) + "Save the current clocked-in task’s title and category to BUFFER. + +The output is in JSON format. If there is no clocked in task, both title and +category will be a JSON null value." + (let ((category (when org-clock-current-task + (get-text-property 0 'org-category org-clock-current-task))) + (title (when org-clock-current-task + (substring-no-properties org-clock-current-task)))) + (with-current-buffer buffer + (insert "{\"title\":") + (insert (if title (json-encode-string title) "null")) + (insert ",\"category\":") + (insert (if category (json-encode-string category) "null")) + (insert "}")))) + +(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)) + (write-file org-clock-waybar-filename))) + +(defun org-clock-waybar-setup () + "Setup org-clock-waybar. + +It adds `org-clock-waybar-save-task' to both `org-clock-in-hook' and +`org-clock-out-hook'." + (add-hook 'org-clock-in-hook #'org-clock-waybar-save-task) + (add-hook 'org-clock-out-hook #'org-clock-waybar-save-task)) + +(provide 'org-clock-waybar) +;;; org-clock-waybar.el ends here