forked from gergely/org-clock-waybar
		
	
		
			
				
	
	
		
			60 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			EmacsLisp
		
	
	
	
	
	
			
		
		
	
	
			60 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			EmacsLisp
		
	
	
	
	
	
| ;;; 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
 |