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/config.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/config.lisp')
| -rw-r--r-- | src/config.lisp | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/src/config.lisp b/src/config.lisp new file mode 100644 index 0000000..ff3baf7 --- /dev/null +++ b/src/config.lisp @@ -0,0 +1,85 @@ +(in-package #:wallpaper-switcher) + +;;; Configuration +;;; +;;; The config file is plain "key = value" lines; blank lines and lines +;;; starting with # are ignored. Recognised keys: +;;; +;;; directory = ~/.wallpapers +;;; command = pkill -x swaybg; swaybg -m fill -i %f & +;;; extensions = png webp +;;; +;;; COMMAND is a shell command. %f becomes the shell-quoted path of the +;;; wallpaper and %% becomes a literal %. The command runs under `sh -c`, +;;; so you can use ;, &&, & and so on. + +(defparameter *default-directory* "~/.wallpapers") + +(defparameter *default-command* "pkill -x swaybg; swaybg -m fill -i %f &" + "Kill any running swaybg, then start a new one in the background. +swaybg runs until it is killed, so without the pkill each pick would stack +another swaybg process on top of the last one.") + +(defparameter *default-extensions* '("png" "webp")) + +(defstruct config + (directory *default-directory* :type string) + (command *default-command* :type string) + (extensions *default-extensions* :type list)) + +(defun default-config () + (make-config)) + +(defun default-config-path () + "$XDG_CONFIG_HOME/wallpaper-switcher/config, or ~/.config/... if unset." + (uiop:xdg-config-home "wallpaper-switcher/config")) + +(defun expand-path (string) + "Expand a leading ~ and $HOME / ${HOME} in STRING." + (let* ((home (string-right-trim "/" (namestring (user-homedir-pathname)))) + (s (cond ((string= string "~") home) + ((uiop:string-prefix-p "~/" string) + (concatenate 'string home (subseq string 1))) + (t string)))) + (dolist (var '("${HOME}" "$HOME") s) + (loop for pos = (search var s) + while pos + do (setf s (concatenate 'string (subseq s 0 pos) home + (subseq s (+ pos (length var))))))))) + +(defun split-words (string) + "Split STRING on whitespace and commas, dropping empty pieces." + (remove "" (uiop:split-string string :separator '(#\Space #\Tab #\,)) + :test #'string=)) + +(defun parse-config-string (string &optional (config (default-config))) + "Apply the settings in STRING on top of CONFIG and return CONFIG. +Unknown keys and malformed lines produce a warning and are skipped." + (loop for raw in (uiop:split-string string :separator '(#\Newline)) + for lineno from 1 + for line = (string-trim '(#\Space #\Tab #\Return) raw) + unless (or (string= line "") (char= (char line 0) #\#)) + do (let ((eq (position #\= line))) + (if (null eq) + (warn "config line ~D: expected key = value, got ~S" lineno line) + (let ((key (string-downcase + (string-trim '(#\Space #\Tab) (subseq line 0 eq)))) + (value (string-trim '(#\Space #\Tab) (subseq line (1+ eq))))) + (cond ((string= key "directory") + (setf (config-directory config) value)) + ((string= key "command") + (setf (config-command config) value)) + ((string= key "extensions") + (setf (config-extensions config) + (mapcar (lambda (e) + (string-downcase (string-left-trim "." e))) + (split-words value)))) + (t (warn "config line ~D: unknown key ~S" lineno key))))))) + config) + +(defun load-config (&optional (path (default-config-path))) + "Read PATH if it exists; otherwise return the defaults." + (let ((config (default-config))) + (if (and path (probe-file path)) + (parse-config-string (uiop:read-file-string path) config) + config))) |
