From 0bca7a7eb09f0e094aa721f75d23f771d20c6bf3 Mon Sep 17 00:00:00 2001 From: "Christopher R. Nelson" Date: Sat, 31 Oct 2026 23:51:41 -0400 Subject: Initial wallpaper-switcher pipe menu Common Lisp tool that prints an Openbox pipe menu of the .png/.webp files in ~/.wallpapers, with each item setting the wallpaper via swaybg. Directory, command template and extensions are configurable through ~/.config/wallpaper-switcher/config or command-line options. Co-Authored-By: Claude Opus 5.5 --- src/menu.lisp | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 src/menu.lisp (limited to 'src/menu.lisp') diff --git a/src/menu.lisp b/src/menu.lisp new file mode 100644 index 0000000..4bae123 --- /dev/null +++ b/src/menu.lisp @@ -0,0 +1,94 @@ +(in-package #:wallpaper-switcher) + +;;; Finding wallpapers + +(defun wallpaper-p (pathname extensions) + (let ((type (pathname-type pathname))) + (and type (member type extensions :test #'string-equal)))) + +(defun find-wallpapers (directory extensions) + "Image files directly inside DIRECTORY (not subdirectories), sorted by +name. Returns NIL if DIRECTORY does not exist." + (let ((dir (uiop:ensure-directory-pathname (expand-path directory)))) + (when (uiop:directory-exists-p dir) + (sort (remove-if-not (lambda (p) (wallpaper-p p extensions)) + (uiop:directory-files dir)) + #'string-lessp + :key #'file-namestring)))) + +;;; Escaping + +(defun shell-quote (string) + "Quote STRING as one POSIX shell word: wrap it in single quotes and write +each embedded ' as '\\''." + (with-output-to-string (out) + (write-char #\' out) + (loop for c across string + do (if (char= c #\') + (write-string "'\\''" out) + (write-char c out))) + (write-char #\' out))) + +(defun xml-escape (string) + (with-output-to-string (out) + (loop for c across string + do (case c + (#\& (write-string "&" out)) + (#\< (write-string "<" out)) + (#\> (write-string ">" out)) + (#\" (write-string """ out)) + (#\' (write-string "'" out)) + (t (write-char c out)))))) + +(defun menu-label (pathname) + "Filename without its extension. Openbox reads _ in a label as a keyboard +shortcut marker, so each _ is doubled to show it as-is." + (let ((name (or (pathname-name pathname) (file-namestring pathname)))) + (with-output-to-string (out) + (loop for c across name + do (write-char c out) + (when (char= c #\_) (write-char c out)))))) + +;;; Building the Execute command + +(defun expand-template (template file) + "Replace %f in TEMPLATE with FILE shell-quoted, and %% with %." + (with-output-to-string (out) + (loop with i = 0 + with n = (length template) + while (< i n) + do (let ((c (char template i))) + (if (and (char= c #\%) (< (1+ i) n)) + (let ((next (char template (1+ i)))) + (case next + (#\f (write-string (shell-quote file) out) (incf i 2)) + (#\% (write-char #\% out) (incf i 2)) + (t (write-char c out) (incf i)))) + (progn (write-char c out) (incf i))))))) + +(defun build-command (template pathname) + "The command line for one menu item. Openbox splits Execute commands +like a shell does but does not run them through one, so the expanded +template is passed to `sh -c` as a single quoted argument." + (format nil "sh -c ~A" + (shell-quote (expand-template template (uiop:native-namestring pathname))))) + +;;; Rendering + +(defun render-item (stream pathname command) + (format stream " ~% ~A~% ~%" + (xml-escape (menu-label pathname)) + (xml-escape (build-command command pathname)))) + +(defun render-menu (config &optional (stream *standard-output*)) + "Write the Openbox pipe menu XML for CONFIG to STREAM." + (let ((files (find-wallpapers (config-directory config) + (config-extensions config)))) + (format stream "~%~%") + (if files + (dolist (f files) + (render-item stream f (config-command config))) + (format stream " ~%" + (xml-escape (format nil "No wallpapers in ~A" + (expand-path (config-directory config)))))) + (format stream "~%"))) -- cgit v1.2.3