2 Commits

Author SHA1 Message Date
73918c2b39 Add instructions for configuring for the Eww bar 2023-10-27 05:56:59 +02:00
d77835d97b Redesign README to become an Org document
Why would we use markdown in the Emacs world, right?
2023-10-27 05:47:59 +02:00
3 changed files with 138 additions and 199 deletions

104
README.md
View File

@@ -1,104 +0,0 @@
# org-clock-waybar Export the currently clocked-in task to be displayed on Waybar
## Installation
Put `org-clock-waybar.el` somewhere in your `load-path`, and `(require 'org-clock-waybar)`.
MELPA version may come soon.
You can set the file to be written by customizing `org-clock-waybar-filename`; it defaults to
`$XDG_CACHE_HOME/waybar-current-task.json` (`$XDG_CACHE_HOME` defaults to `$HOME/.cache` on XDG
compatible systems, like Linux.)
### Quelpa
If you only have [`quelpa`](https://github.com/quelpa/quelpa) installed:
```elisp
(quelpa
'(org-clock-waybar
:fetcher git
:url "https://gitea.polonkai.eu/gergely/org-clock-waybar.git"))
```
or, if you have [`quelpa-use-package`](https://github.com/quelpa/quelpa-use-package) installed,
too:
```elisp
(quelpa-use-package org-clock-waybar
:quelpa (org-clock-waybar
:fetcher git
:url "https://gitea.polonkai.eu/gergely/org-clock-waybar.git"))
```
## Waybar configuration
To add the current task to Waybar, add this snippet to your config:
```json
"custom/org": {
"format": " {}",
"return-type": "json",
"restart-interval": 5,
"exec": "cat /home/yourusername/.cache/waybar-current-task.json"
}
```
Then, add `custom/org` to `modules-left`/`modules-center`/`module-right` if your bars
configuration. You can find a minimal working configuration in the [`examples`](./examples)
directory.
### Additional Configuration
You can also display an icon specific to the clocked tasks category with the `format-icons` key:
```json
"custom/org": {
"format": "{icon} {}",
"return-type": "json",
"restart-interval": 1,
"format-icons": {
"refile": "",
"ToDo": "",
},
"exec": "cat /home/yourusername/.cache/waybar-current-task.json"
}
```
If you use Emacs as a daemon (e.g. starting it as `emacs --daemon` or calling `(server-start)`),
you can change the `exec` command to invoke `emacsclient` directly. Note that, since Emacsclient
cant actually write stuff to the terminal, it will output an Emacs string full of backslashes
(see [this Emacs SE answer for details](https://emacs.stackexchange.com/a/28668/507)); thus, you
have to pipe the output through `jq fromjson`.
If you run emacs in this mode you can also eval commands on click, middle click or scroll.:
```json
"custom/org": {
"format": "{icon} {}",
"return-type": "json",
"restart-interval": 1,
"format-icons": {
"refile": "",
"ToDo": "",
},
"exec": "emacsclient --eval '(org-clock-waybar-ouptut-task)' | jq fromjson --unbuffered --compact-output",
"on-click": "emacsclient --eval '(org-clock-out)'",
"on-middle-click": "emacsclient --eval '(org-clock-in-last)'",
},
```
If you want the taskbar to show nothing or some other content if the emacs server is not running then you need to write a short bash script to catch when the emacsclient command returns a non-zero exit code. An example of this is:
```bash
#!/bin/bash
json=$(emacsclient --eval '(org-clock-waybar-ouptut-task)' 2> /dev/null)
status=$?
[ $status -eq 0 ] && echo $(echo $json | jq fromjson --unbuffered --compact-output) || echo ""
```
## Customization
To see a list of configurable parts, use `M-x customize-group <RET> org-clock-waybar`.

119
README.org Normal file
View File

@@ -0,0 +1,119 @@
#+TITLE: org-clock-waybar Export the currently clocked-in task to be displayed on Waybar
* Installation
Put ~org-clock-waybar.el~ somewhere in your ~load-path~, and ~(require 'org-clock-waybar)~.
MELPA version may come soon.
** Quelpa
If you only have [[https://github.com/quelpa/quelpa][quelpa]] installed:
#+begin_src emacs-lisp
(quelpa
'(org-clock-waybar
:fetcher git
:url "https://gitea.polonkai.eu/gergely/org-clock-waybar.git"))
#+end_src
or, if you have [[https://github.com/quelpa/quelpa-use-package][quelpa-use-package] installed,
too:
#+begin_src emacs-lisp
(quelpa-use-package org-clock-waybar
:quelpa (org-clock-waybar
:fetcher git
:url "https://gitea.polonkai.eu/gergely/org-clock-waybar.git"))
#+end_src
* Customization
To see a list of configurable parts, use ~M-x customize-group <RET> org-clock-waybar~.
You can set the file to be written by customizing ~org-clock-waybar-filename~; it
defaults to ~$XDG_CACHE_HOME/waybar-current-task.json~ (~$XDG_CACHE_HOME~ defaults
to ~$HOME/.cache~ on XDG compatible systems, like Linux.)
* Waybar configuration
To add the current task to Waybar, add this snippet to your config:
#+begin_src json
"custom/org": {
"format": " {}",
"return-type": "json",
"restart-interval": 5,
"exec": "cat /home/yourusername/.cache/waybar-current-task.json"
}
#+end_src
Then, add ~custom/org~ to your ~modules-left~, ~modules-center~, or ~module-right~
section in your bars configuration. You can find a minimal working
configuration in the [[./examples][examples]] directory.
** Additional Configuration
You can also display an icon specific to the clocked tasks category with the ~format-icons~ key:
#+begin_src json
"custom/org": {
"format": "{icon} {}",
"return-type": "json",
"restart-interval": 1,
"format-icons": {
"refile": "",
"ToDo": "",
},
"exec": "cat /home/yourusername/.cache/waybar-current-task.json"
}
#+end_src
If you use Emacs as a daemon (e.g. starting it as ~emacs --daemon~ or calling
~(server-start)~), you can change the ~exec~ command to invoke ~emacsclient~ directly.
Note that, since Emacsclient cant actually write stuff to the terminal, it will
output an Emacs string full of backslashes (see [[https://emacs.stackexchange.com/a/28668/507][this Emacs SE answer for
details]]); thus, you have to pipe the output through ~jq fromjson~.
If you run emacs in this mode you can also eval commands on click, middle click or scroll.:
#+begin_src json
"custom/org": {
"format": "{icon} {}",
"return-type": "json",
"restart-interval": 1,
"format-icons": {
"refile": "",
"ToDo": "",
},
"exec": "emacsclient --eval '(org-clock-waybar-ouptut-task)' | jq fromjson --unbuffered --compact-output",
"on-click": "emacsclient --eval '(org-clock-out)'",
"on-middle-click": "emacsclient --eval '(org-clock-in-last)'",
},
#+end_src
If you want the taskbar to show nothing or some other content if the emacs server is not running then you need to write a short bash script to catch when the emacsclient command returns a non-zero exit code. An example of this is:
#+begin_src bash
#!/bin/bash
json=$(emacsclient --eval '(org-clock-waybar-ouptut-task)' 2> /dev/null)
status=$?
[ $status -eq 0 ] && echo $(echo $json | jq fromjson --unbuffered --compact-output) || echo ""
#+end_src
* Eww bar configuration
Waybar is still popular, but it also seems [[https://elkowar.github.io/eww/][Eww]] is also gaining popularity.
#+begin_src yuck
(defpoll current-org-task :interval "1s" :initial "{}" "cat /home/yourusername/.cache/waybar-current-task.json")
(defwidget current-org-task-widget []
{current-org-task.text})
(defwidget bar []
(box
:orientation "h"
(current-org-task-widget)))
#+end_src

View File

@@ -1,4 +1,3 @@
;;; ... -*- lexical-binding: t -*-
;;; org-clock-waybar.el --- Summary
;; Copyright (C) 2021 Gergely Polonkai
@@ -61,81 +60,30 @@
:type 'string
:group 'org-clock-waybar)
(defcustom org-clock-waybar-text-function
nil
"Function to generate the title text.
The function must return a single string.
When nil, `org-clock-waybar-get-task-title' is used."
:type 'function
:group 'org-clock-waybar)
(defcustom org-clock-waybar-alt-function
nil
"Function to generate the alternative text.
The function must return a single string.
When nil, `org-clock-waybar-get-task-category' is used."
:type 'function
:group 'org-clock-waybar)
(defcustom org-clock-waybar-class-function
nil
"Function to generate the class.
The function must either return a string, or a list of strings.
When nil, `org-clock-waybar-get-tags' is used."
:type 'function
:group 'org-clock-waybar)
(defcustom org-clock-waybar-tooltip-function
nil
"Function to generate the tooltip.
The function must return a string.
When nil, `org-clock-waybar-get-tooltip' is used."
:type 'function
:group 'org-clock-waybar)
(defcustom org-clock-waybar-percentage-function
nil
"Function to generate the percentage text.
When nil, the percentage text will be an empty string."
:type 'function
: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'.")
(defsubst org-clock-waybar-get-task-title ()
(defsubst org-clock-waybar--get-task-title ()
"Get the title of TASK."
(if (org-clocking-p)
(substring-no-properties org-clock-current-task)
org-clock-waybar-not-clocked-in-text))
(when (org-clocking-p) (substring-no-properties org-clock-current-task)))
(defsubst org-clock-waybar-get-task-category ()
(defsubst org-clock-waybar--get-task-category ()
"Get the category of TASK."
(when (org-clocking-p) (get-text-property 0 'org-category org-clock-current-task)))
(defun org-clock-waybar--list-of-strings-p (object)
"Return t if OBJECT is a list of strings."
(not (null (delq nil
(mapcar (lambda (x) (and (stringp x) x)) object)))))
(defun org-clock-waybar-get-tooltip ()
(defun org-clock-waybar--get-tooltip ()
"The default tooltip to send to waybar."
(when (org-clocking-p)
(let ((clocked-time (org-clock-get-clocked-time)))
(format "%s: %s (%s)"
(org-clock-waybar-get-task-category)
(org-clock-waybar-get-task-title)
(org-clock-waybar--get-task-category)
(org-clock-waybar--get-task-title)
(org-duration-from-minutes clocked-time)))))
(defun org-clock-waybar-get-tags ()
(defun org-clock-waybar--get-tags ()
"Get the tags of the currently clocked-in task."
(when (org-clocking-p)
(or (org-with-point-at org-clock-marker (org-get-tags))
@@ -143,47 +91,23 @@ When nil, the percentage text will be an empty string."
))
(defun org-clock-waybar--get-clocked-task-json ()
"Get the currently clocked-in task's data as a stringified JSON object.
"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
value of `org-clock-waybar-not-clocked-in-text'."
(let* ((text-func (or 'org-clock-waybar-text-function
'org-clock-waybar-get-task-title))
(text (funcall text-func))
(alt-func (or 'org-clock-waybar-alt-function
'org-clock-waybar-get-task-category))
(alt (funcall alt-func))
(tooltip-func (or 'org-clock-waybar-tooltip-function
'org-clock-waybar-get-tooltip))
(tooltip (funcall tooltip-func))
(class-func (or 'org-clock-waybar-class-function
'org-clock-waybar-get-tags))
(class (funcall class-func))
(percentage (when (fboundp 'org-clock-waybar-percentage-function)
(funcall 'org-clock-waybar-percentage-function)))
(let* ((category (org-clock-waybar--get-task-category))
(title (org-clock-waybar--get-task-title))
(tooltip (org-clock-waybar--get-tooltip))
(output (json-new-object)))
(or (null title)
(stringp title)
(error "Title must be a string (org-clock-waybar)!"))
(or (null alt)
(stringp alt)
(error "Alt text must be a string (org-clock-waybar)!"))
(or (null tooltip)
(stringp tooltip)
(error "Tooltip must be a string (org-clock-waybar)!"))
(or (null class)
(stringp class)
(org-clock-waybar--list-of-strings-p class)
(error "Class must be a string or a list of strings (org-clock-waybar)!"))
(or (null percentage)
(stringp percentage)
(error "Percentage must be a string (org-clock-waybar)!"))
(setq output (json-add-to-object output "text" (or text "")))
(setq output (json-add-to-object output "alt" (or alt "")))
(setq output (json-add-to-object
output
"text"
(or title org-clock-waybar-not-clocked-in-text)))
(setq output (json-add-to-object output "alt" (or category "")))
(setq output (json-add-to-object output "tooltip" (or tooltip "")))
(setq output (json-add-to-object output "class" (or class "")))
(setq output (json-add-to-object output "percentage" (or percentage "")))
(setq output (json-add-to-object output "class" (or (org-clock-waybar--get-tags) "")))
(setq output (json-add-to-object output "percentage" ""))
(json-encode output)))
(defun org-clock-waybar-save-task ()
@@ -194,7 +118,7 @@ value of `org-clock-waybar-not-clocked-in-text'."
(insert (org-clock-waybar--get-clocked-task-json))
(write-file org-clock-waybar-filename)))
(defun org-clock-waybar-output-task ()
(defun org-clock-waybar-ouptut-task ()
"Output the current task in JSON format Waybar can understand.
This function is ought to be used via Emacsclient: