aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristopher R. Nelson <christopher.nelson@languidnights.com>2026-11-01 00:16:04 -0400
committerChristopher R. Nelson <christopher.nelson@languidnights.com>2026-11-01 00:16:04 -0400
commit7fb53e418a48b8baa7bcab89967a0755f2230162 (patch)
treee2e4ce1a04ac33650d5c7be06ebdc295ce9112c8
parent0bca7a7eb09f0e094aa721f75d23f771d20c6bf3 (diff)
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 <noreply@anthropic.com>
-rw-r--r--README.md3
-rw-r--r--src/menu.lisp98
-rw-r--r--src/package.lisp1
-rw-r--r--t/tests.lisp69
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 "&apos;" 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 " <item label=\"~A\">~% <action name=\"Execute\"><command>~A</command></action>~% </item>~%"
- (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 "<item label=\"~A\">~%" (xml-escape (menu-label pathname)))
+ (indent stream (1+ depth))
+ (format stream "<action name=\"Execute\"><command>~A</command></action>~%"
+ (xml-escape (build-command command pathname)))
+ (indent stream depth)
+ (format stream "</item>~%"))
+
+(defun render-empty (stream depth directory)
+ (indent stream depth)
+ (format stream "<separator label=\"~A\"/>~%"
+ (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 "<menu id=\"~A\" label=\"~A\">~%"
+ (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 "</menu>~%"))
+ (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 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>~%<openbox_pipe_menu>~%")
- (if files
- (dolist (f files)
- (render-item stream f (config-command config)))
- (format stream " <separator label=\"~A\"/>~%"
- (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 "</openbox_pipe_menu>~%")))
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 "<separator label=\"No wallpapers in" xml))
(check (not (search "<item" xml))))))
+
+(defun subdir (dir name)
+ (ensure-directories-exist (merge-pathnames (format nil "~A/" name) dir)))
+
+(defun render-to-string (dir)
+ (with-output-to-string (s)
+ (render-menu (make-config :directory (namestring dir)) s)))
+
+(deftest test-find-subdirectories
+ (with-temp-dir (dir)
+ (dolist (n '("zeta" "Alpha" ".hidden" "beta"))
+ (subdir dir n))
+ (touch dir "file.png")
+ (check-equal '("Alpha" "beta" "zeta")
+ (mapcar (lambda (d) (car (last (pathname-directory d))))
+ (find-subdirectories (namestring dir))))))
+
+(deftest test-render-submenus
+ (with-temp-dir (dir)
+ (touch dir "top.png")
+ (touch (subdir dir "nature") "forest.webp")
+ (touch (subdir dir "nature/deep_sea") "reef.png")
+ (let ((xml (render-to-string dir)))
+ (check (search "<menu id=\"wallpaper-switcher:" xml))
+ (check (search "label=\"nature\"" xml))
+ (check (search "label=\"deep__sea\"" xml))
+ (check (search "label=\"forest\"" xml))
+ (check (search "label=\"reef\"" xml))
+ ;; Directories come before files, and nesting is preserved.
+ (check (< (search "label=\"nature\"" xml) (search "label=\"top\"" xml)))
+ (check (< (search "label=\"deep__sea\"" xml)
+ (search "label=\"forest\"" xml)
+ (search "</menu>" 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 "<separator label=\"No wallpapers in ~A\"/>"
+ (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)))))))