diff options
| author | Craig Jennings <c@cjennings.net> | 2026-05-10 13:30:08 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2026-05-10 13:30:08 -0500 |
| commit | 6545605fa965d649defc0bb01e8c040afbbcc529 (patch) | |
| tree | c806b1bfb907991d958523d78ea2e8372666ce99 | |
| parent | 9688edf09086ae96559cf572b871b2cdb142c5be (diff) | |
| download | dotemacs-6545605fa965d649defc0bb01e8c040afbbcc529.tar.gz dotemacs-6545605fa965d649defc0bb01e8c040afbbcc529.zip | |
refactor(dirvish): extract cj/--wallpaper-program-for helper
`cj/set-wallpaper' had two parallel cond arms hardcoding the X11/Wayland dispatch and the success/failure messages inline. Lift the program-and-args choice into `cj/--wallpaper-program-for' -- a pcase from a display-server symbol to a (PROGRAM ARG...) list, or nil for unknown environments. The wrapper now: detect env, ask helper for the command, surface the right message (unknown server / executable missing / success). Adding a third backend (e.g. xdg-desktop-portal) becomes one pcase clause + one test.
| -rw-r--r-- | modules/dirvish-config.el | 40 | ||||
| -rw-r--r-- | tests/test-dirvish-config-wallpaper-program.el | 42 |
2 files changed, 68 insertions, 14 deletions
diff --git a/modules/dirvish-config.el b/modules/dirvish-config.el index c01bfafa..3fce616e 100644 --- a/modules/dirvish-config.el +++ b/modules/dirvish-config.el @@ -221,26 +221,38 @@ regardless of what file or subdirectory the point is on." (shell-quote-argument current-dir))))))) (message "Could not determine current directory.")))) +(defun cj/--wallpaper-program-for (env) + "Return the (PROGRAM PRE-FILE-ARG...) list for setting wallpaper under ENV. + +ENV is a display-server symbol: `x11' picks feh with --bg-fill, `wayland' +picks swww with the img subcommand. Any other value returns nil so the +caller can surface an \"unknown display server\" error. + +Pure helper used by `cj/set-wallpaper'." + (pcase env + ('x11 '("feh" "--bg-fill")) + ('wayland '("swww" "img")) + (_ nil))) + (defun cj/set-wallpaper () "Set the image at point as the desktop wallpaper. Uses feh on X11, swww on Wayland." (interactive) - (let ((file (expand-file-name (dired-file-name-at-point)))) + (let* ((file (expand-file-name (dired-file-name-at-point))) + (env (cond ((env-x11-p) 'x11) + ((env-wayland-p) 'wayland) + (t nil))) + (cmd (cj/--wallpaper-program-for env))) (cond - ((env-x11-p) - (if (executable-find "feh") - (progn - (call-process "feh" nil 0 nil "--bg-fill" file) - (message "Wallpaper set: %s (feh)" (file-name-nondirectory file))) - (message "feh not found"))) - ((env-wayland-p) - (if (executable-find "swww") - (progn - (call-process "swww" nil 0 nil "img" file) - (message "Wallpaper set: %s (swww)" (file-name-nondirectory file))) - (message "swww not found"))) + ((null cmd) + (message "Unknown display server (not X11 or Wayland)")) + ((not (executable-find (car cmd))) + (message "%s not found" (car cmd))) (t - (message "Unknown display server (not X11 or Wayland)"))))) + (apply #'call-process (car cmd) nil 0 nil + (append (cdr cmd) (list file))) + (message "Wallpaper set: %s (%s)" + (file-name-nondirectory file) (car cmd)))))) ;;; ---------------------------------- Dirvish ---------------------------------- diff --git a/tests/test-dirvish-config-wallpaper-program.el b/tests/test-dirvish-config-wallpaper-program.el new file mode 100644 index 00000000..556c1310 --- /dev/null +++ b/tests/test-dirvish-config-wallpaper-program.el @@ -0,0 +1,42 @@ +;;; test-dirvish-config-wallpaper-program.el --- Tests for the wallpaper command resolver -*- lexical-binding: t; -*- + +;;; Commentary: +;; `cj/--wallpaper-program-for' is the pure dispatch behind +;; `cj/set-wallpaper': given a display-server symbol it returns the +;; (PROGRAM . PRE-FILE-ARGS) cons that the interactive wrapper passes +;; to `call-process' alongside the wallpaper file path. The wrapper +;; handles environment detection (`env-x11-p' / `env-wayland-p'), the +;; `executable-find' check, and the user-visible message. + +;;; Code: + +(require 'ert) +(require 'package) + +(setq package-user-dir (expand-file-name "elpa" user-emacs-directory)) +(package-initialize) +(add-to-list 'load-path (expand-file-name "modules" user-emacs-directory)) +(add-to-list 'load-path (expand-file-name "elpa/dirvish-2.3.0/extensions" + user-emacs-directory)) +(require 'user-constants) +(require 'keybindings) +(require 'dirvish-config) + +(ert-deftest test-cj--wallpaper-program-for-x11 () + "Normal: x11 dispatches to feh with --bg-fill." + (should (equal (cj/--wallpaper-program-for 'x11) + '("feh" "--bg-fill")))) + +(ert-deftest test-cj--wallpaper-program-for-wayland () + "Normal: wayland dispatches to swww with the img subcommand." + (should (equal (cj/--wallpaper-program-for 'wayland) + '("swww" "img")))) + +(ert-deftest test-cj--wallpaper-program-for-unknown-returns-nil () + "Boundary: an unknown environment returns nil so the wrapper can fall back." + (should-not (cj/--wallpaper-program-for 'tty)) + (should-not (cj/--wallpaper-program-for nil)) + (should-not (cj/--wallpaper-program-for 'mac))) + +(provide 'test-dirvish-config-wallpaper-program) +;;; test-dirvish-config-wallpaper-program.el ends here |
