blog // ~snubspreker

this is just a test

Toggle Polybar Visibility in Emacs

April 21, 2020 — ~snubspreker

Write a toggle

ErgoEmacs has a nifty post on something I have done before in numertous ways. This one just feels better. So I attempted something just for fun and this is the result.

(defun toggle-polybar ()
  "Toggle visibility of polybar.
Initial 'state (nil t) is pulled from xdotool."
  (interactive)
  (if (eq (shell-command "xdotool search --onlyvisible --name Polybar" nil nil) 0)
      (progn
    (shell-command "xdo hide -N Polybar" nil nil))
    (progn
      (shell-command "xdo show -N Polybar" nil nil))))

(global-set-key (kbd "C-c t p") #'toggle-polybar)

Originally I was using a plist to set the state, but that is not required since all linux commands return an exit code which you can trap and test. So the if statement first checks to see if polybar is visible. xdotool returns 0 or 1 zero being success. Test that return and execute the show or hide based on that result.

Here is the same functionality in bash. That might show better what is happening.

#!/bin/bash

xdotool search --onlyvisible --name Polybar; ec=$?

case $ec in
    0) xdo hide -N Polybar ;;
    1) xdo show -N Polybar ;;
esac

In anycase, I have been using both versions with no faults…yet.

tags: emacs elisp linux