blob: ff3baf72ea2ed7c55a0e732fb06f732dfe64af09 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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)))
|