aboutsummaryrefslogtreecommitdiff
path: root/src/main.lisp
blob: 27a7dbb0b599e3bc50bc6f852235f9e312fb5881 (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
(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))))