Endless Parentheses

Ramblings on productivity and technical subjects.

profile for Malabarba on Stack Exchange

Using Paradox for Github notifications

A few weeks ago I noticed a new package on Melpa called github-notifier by Chunyang, which displays a count of your Github notifications on the mode-line. Instead of just installing the package like a normal person, I had an urge to try and see how hard it would be to write from scratch. Paradox already has a function for interacting with the Github API, so it’s just a matter of putting it to work.

The first thing you need to do is visit your Github tokens page, and edit the Paradox token to allow access to your notifications.

Next, define a command to visit your notifications, and a function to display a button in the mode-line.

(defun endless/visit-notifications ()
  (interactive)
  (endless/count-for-mode-line nil)
  (browse-url "https://github.com/notifications"))

(defvar endless/gh-mode-line nil)

(defun endless/count-for-mode-line (data)
  (setq endless/gh-mode-line
        (when data
          (format " GH-%s" (length data))))
  (force-mode-line-update))

(add-to-list 'global-mode-string
             '(endless/gh-mode-line
               (:propertize endless/gh-mode-line
                            local-map (keymap (mode-line keymap (mouse-1 . endless/visit-notifications)))
                            mouse-face mode-line-highlight)))

For convenience, I’ve also added visit-notifications to my launcher-map under n.

Next, write a function to query the Github API and pass the vector of results to count-for-mode-line.

;; If already installed, this does nothing.
(package-install 'paradox)
(autoload 'paradox--github-action "paradox-github")
(defun endless/check-gh-notifications ()
  "Check for github notifications and update the mode-line."
  (paradox--github-action "notifications"
    :reader (lambda ()
              (let ((json-false nil)
                    (json-array-type 'list))
                (json-read)))
    :callback #'endless/count-for-mode-line))

Finally, just set the timer to some convenient interval. I run it at every 30 seconds of idle time.

(defvar endless/gh-timer
  (when (bound-and-true-p paradox-github-token)
    (run-with-idle-timer 30 'repeat
                         #'endless/check-gh-notifications)))

And that’s all! Roughly 30 lines of code and you have a convenient notification system. Whenever you see that GH-2 show up at the corner of your mode-line, just click on it (or type C-x l n). That will remove the button and take you to Github. On Github you can navigate with j, k, and RET, and “mark as read” with m.

comments powered by Disqus