54 lines
1.4 KiB
Clojure
Executable file
54 lines
1.4 KiB
Clojure
Executable file
#!/usr/bin/env -S bb
|
|
(ns toggle-displays
|
|
(:require [clojure.edn :as edn]
|
|
[clojure.java.io :as io]
|
|
[clojure.java.shell :refer [sh]]
|
|
[babashka.fs :as fs]))
|
|
|
|
(def state-file
|
|
(fs/path
|
|
(System/getenv "XDG_RUNTIME_DIR")
|
|
(str (fs/file-name (or (System/getProperty "babashka.file") *file*)) ".state")))
|
|
|
|
(defn get-state!
|
|
[]
|
|
(if (fs/exists? state-file)
|
|
(some->>
|
|
(->> state-file
|
|
fs/unixify
|
|
io/reader
|
|
java.io.PushbackReader.
|
|
edn/read))
|
|
(some->>
|
|
(sh "journalctl" "--user" "--unit" "kanshi.service" "--reverse" "--lines" "1")
|
|
;; {:exit 0, :out "Mar 06 07:39:48 aegir kanshi[3318847]: configuration for profile 'kvm-1' applied\n", :err ""}
|
|
:out
|
|
(re-find #"configuration for profile '(.+?)' applied")
|
|
(second)
|
|
(keyword))))
|
|
|
|
(defn save-state!
|
|
[state]
|
|
(spit (fs/unixify state-file) state))
|
|
|
|
(defn apply-state!
|
|
[state]
|
|
(sh "kanshictl" "switch" (name state)))
|
|
|
|
(defn next-state
|
|
[state]
|
|
(let [modes (apply concat (repeat 2 [:kvm-left
|
|
:kvm-right
|
|
:single]))]
|
|
(nth modes (inc (.indexOf modes state)))))
|
|
|
|
(defn -main
|
|
[& [toggle?]]
|
|
(when toggle?
|
|
(let [state (get-state!)
|
|
state* (next-state state)]
|
|
(apply-state! state*)
|
|
(save-state! state*))))
|
|
|
|
(when (= *file* (System/getProperty "babashka.file"))
|
|
(apply -main *command-line-args*))
|