From 7fb53e418a48b8baa7bcab89967a0755f2230162 Mon Sep 17 00:00:00 2001 From: "Christopher R. Nelson" Date: Sun, 1 Nov 2026 00:16:04 -0400 Subject: Show subdirectories as nested submenus Each subdirectory of the wallpaper directory becomes an inline Openbox submenu, recursively, listed before the files at that level. A directory with no wallpapers and no subdirectories gets the same "No wallpapers in ..." header as an empty root. Hidden directories are skipped, and symlinks pointing back up the tree are not followed. Co-Authored-By: Claude Opus 5.5 --- README.md | 3 +- src/menu.lisp | 98 +++++++++++++++++++++++++++++++++++++++++++++----------- src/package.lisp | 1 + t/tests.lisp | 69 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 151 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index f43ecd8..0ee66af 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,8 @@ Optional. Put it in `$XDG_CONFIG_HOME/wallpaper-switcher/config` (usually `~/.config/wallpaper-switcher/config`): ``` -# Where to look (~ and $HOME are expanded). Subdirectories are not searched. +# Where to look (~ and $HOME are expanded). Subdirectories become submenus; +# hidden ones (starting with .) are skipped. directory = ~/.wallpapers # Shell command run when an item is picked. %f is the file (already quoted diff --git a/src/menu.lisp b/src/menu.lisp index 4bae123..da99ab0 100644 --- a/src/menu.lisp +++ b/src/menu.lisp @@ -16,6 +16,23 @@ name. Returns NIL if DIRECTORY does not exist." #'string-lessp :key #'file-namestring)))) +(defun directory-name (directory) + "The last component of DIRECTORY, e.g. \"nature\" for /walls/nature/." + (let ((last (car (last (pathname-directory directory))))) + (if (stringp last) last (namestring directory)))) + +(defun hidden-p (name) + (and (plusp (length name)) (char= (char name 0) #\.))) + +(defun find-subdirectories (directory) + "Non-hidden subdirectories directly inside DIRECTORY, sorted by name." + (let ((dir (uiop:ensure-directory-pathname (expand-path directory)))) + (when (uiop:directory-exists-p dir) + (sort (remove-if (lambda (d) (hidden-p (directory-name d))) + (uiop:subdirectories dir)) + #'string-lessp + :key #'directory-name)))) + ;;; Escaping (defun shell-quote (string) @@ -40,14 +57,21 @@ each embedded ' as '\\''." (#\' (write-string "'" out)) (t (write-char c out)))))) +(defun escape-mnemonic (name) + "Openbox reads _ in a label as a keyboard shortcut marker, so each _ is +doubled to show it as-is." + (with-output-to-string (out) + (loop for c across name + do (write-char c out) + (when (char= c #\_) (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)))))) + "Menu label for PATHNAME: a file's name without its extension, or a +directory's last component." + (escape-mnemonic + (if (uiop:directory-pathname-p pathname) + (directory-name pathname) + (or (pathname-name pathname) (file-namestring pathname))))) ;;; Building the Execute command @@ -75,20 +99,56 @@ template is passed to `sh -c` as a single quoted argument." ;;; Rendering -(defun render-item (stream pathname command) - (format stream " ~% ~A~% ~%" - (xml-escape (menu-label pathname)) - (xml-escape (build-command command pathname)))) +(defun indent (stream depth) + (loop repeat (* 2 depth) do (write-char #\Space stream))) + +(defun render-item (stream depth pathname command) + (indent stream depth) + (format stream "~%" (xml-escape (menu-label pathname))) + (indent stream (1+ depth)) + (format stream "~A~%" + (xml-escape (build-command command pathname))) + (indent stream depth) + (format stream "~%")) + +(defun render-empty (stream depth directory) + (indent stream depth) + (format stream "~%" + (xml-escape (format nil "No wallpapers in ~A" + (uiop:native-namestring directory))))) + +(defun submenu-id (directory) + "Openbox needs a unique id for every inline submenu; the path is one." + (format nil "wallpaper-switcher:~A" (uiop:native-namestring directory))) + +(defun render-directory (stream depth directory config visited) + "Write DIRECTORY's contents at DEPTH: a submenu for each subdirectory, +then an item for each wallpaper. A directory with neither gets a +\"No wallpapers in ...\" header. VISITED holds the truenames already +rendered on this branch, so a symlink pointing back up the tree is skipped +rather than recursed into forever." + (let ((subdirs (remove-if (lambda (d) (member (truename d) visited :test #'equal)) + (find-subdirectories (namestring directory)))) + (files (find-wallpapers (namestring directory) (config-extensions config)))) + (when (and (null subdirs) (null files)) + (render-empty stream depth directory)) + (dolist (d subdirs) + (indent stream depth) + (format stream "~%" + (xml-escape (submenu-id d)) + (xml-escape (menu-label d))) + (render-directory stream (1+ depth) d config (cons (truename d) visited)) + (indent stream depth) + (format stream "~%")) + (dolist (f files) + (render-item stream depth f (config-command config))))) (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)))) + (let ((root (uiop:ensure-directory-pathname + (expand-path (config-directory 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)))))) + (if (uiop:directory-exists-p root) + (render-directory stream 1 root config (list (truename root))) + (render-empty stream 1 root)) (format stream "~%"))) diff --git a/src/package.lisp b/src/package.lisp index 3d36565..dcf297e 100644 --- a/src/package.lisp +++ b/src/package.lisp @@ -14,6 +14,7 @@ #:expand-path ;; menu #:find-wallpapers + #:find-subdirectories #:shell-quote #:xml-escape #:menu-label diff --git a/t/tests.lisp b/t/tests.lisp index 6baf5ed..bdb109f 100644 --- a/t/tests.lisp +++ b/t/tests.lisp @@ -138,3 +138,72 @@ extensions = .PNG, jpg webp"))) (render-menu (make-config :directory (namestring dir)) s)))) (check (search "" xml :from-end t))) + (check (not (search "No wallpapers" xml)))))) + +(deftest test-render-empty-subdirectory + (with-temp-dir (dir) + (touch dir "top.png") + (let ((empty (subdir dir "empty"))) + (touch empty "readme.txt") + (let ((xml (render-to-string dir))) + (check (search "label=\"empty\"" xml)) + (check (search (format nil "" + (uiop:native-namestring empty)) + xml)))))) + +(deftest test-render-root-with-only-subdirectories + (with-temp-dir (dir) + (touch (subdir dir "only") "a.png") + (let ((xml (render-to-string dir))) + (check (search "label=\"a\"" xml)) + (check (not (search "No wallpapers" xml)))))) + +(deftest test-render-skips-hidden-directories + (with-temp-dir (dir) + (touch (subdir dir ".thumbnails") "a.png") + (let ((xml (render-to-string dir))) + (check (not (search "thumbnails" xml))) + (check (search "No wallpapers in" xml))))) + +(deftest test-render-symlink-loop + (with-temp-dir (dir) + (let ((sub (subdir dir "sub"))) + (touch sub "a.png") + (uiop:run-program (list "ln" "-s" (uiop:native-namestring dir) + (uiop:native-namestring (merge-pathnames "loop" sub)))) + (let ((xml (render-to-string dir))) + (check (search "label=\"a\"" xml)) + (check (not (search "label=\"loop\"" xml))))))) -- cgit v1.2.3