diff options
| author | Christopher R. Nelson <christopher.nelson@languidnights.com> | 2026-10-31 23:51:41 -0400 |
|---|---|---|
| committer | Christopher R. Nelson <christopher.nelson@languidnights.com> | 2026-10-31 23:51:41 -0400 |
| commit | 0bca7a7eb09f0e094aa721f75d23f771d20c6bf3 (patch) | |
| tree | 380f0f0d02a504e00bcbdb9c13ca3ba25ca370dc /src/main.lisp | |
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 <noreply@anthropic.com>
Diffstat (limited to 'src/main.lisp')
| -rw-r--r-- | src/main.lisp | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/src/main.lisp b/src/main.lisp new file mode 100644 index 0000000..27a7dbb --- /dev/null +++ b/src/main.lisp @@ -0,0 +1,67 @@ +(in-package #:wallpaper-switcher) + +(defparameter *usage* + "Usage: wallpaper-switcher [options] + +Print an Openbox pipe menu listing the wallpapers in a directory. + +Options: + -c, --config FILE Read settings from FILE + (default: $XDG_CONFIG_HOME/wallpaper-switcher/config) + -d, --directory DIR Wallpaper directory (default: ~/.wallpapers) + -e, --command CMD Shell command to set a wallpaper; %f is the file + -h, --help Show this help +") + +(defun parse-args (args) + "Return a plist of the options in ARGS. Signals an error on bad input." + (let ((opts '())) + (loop while args + do (let ((arg (pop args))) + (flet ((value () + (or (pop args) + (error "option ~A needs a value" arg)))) + (cond ((member arg '("-h" "--help") :test #'string=) + (setf (getf opts :help) t)) + ((member arg '("-c" "--config") :test #'string=) + (setf (getf opts :config) (value))) + ((member arg '("-d" "--directory") :test #'string=) + (setf (getf opts :directory) (value))) + ((member arg '("-e" "--command") :test #'string=) + (setf (getf opts :command) (value))) + (t (error "unknown argument ~S" arg)))))) + opts)) + +(defun resolve-config (opts) + "Defaults, then the config file, then command-line options." + (let* ((explicit (getf opts :config)) + (path (if explicit (expand-path explicit) (default-config-path)))) + (when (and explicit (not (probe-file path))) + (error "config file ~A does not exist" path)) + (let ((config (load-config path))) + (when (getf opts :directory) + (setf (config-directory config) (getf opts :directory))) + (when (getf opts :command) + (setf (config-command config) (getf opts :command))) + config))) + +(defun run (args) + "Do the work for ARGS and return an exit code. Safe to call from a REPL." + (handler-case + (let ((opts (parse-args args))) + (if (getf opts :help) + (write-string *usage*) + ;; Warnings go to stderr; stdout must contain only the menu XML. + (handler-bind ((warning (lambda (w) + (format *error-output* "wallpaper-switcher: ~A~%" w) + (muffle-warning w)))) + (render-menu (resolve-config opts)))) + (finish-output) + 0) + (error (e) + (format *error-output* "wallpaper-switcher: ~A~%" e) + 1))) + +(defun main () + "Entry point for the built executable." + (uiop:quit (run (uiop:command-line-arguments)))) |
