|
1 | 1 | (ns karabiner-configurator.core
|
2 | 2 | (:require
|
3 | 3 | [cheshire.core :as json]
|
4 |
| - [clojure.string :as string] |
5 | 4 | [clojure.java.shell :as shell]
|
6 |
| - [karabiner-configurator.modifiers :as modifiers] |
7 |
| - [karabiner-configurator.misc :refer :all] |
8 |
| - [karabiner-configurator.data :refer :all] |
9 |
| - [karabiner-configurator.layers :as layers] |
| 5 | + [clojure.string :as string] |
| 6 | + [clojure.tools.cli :as cli] |
| 7 | + [environ.core :refer [env]] |
| 8 | + [karabiner-configurator.data :as d] |
10 | 9 | [karabiner-configurator.froms :as froms]
|
11 |
| - [karabiner-configurator.tos :as tos] |
12 |
| - [karabiner-configurator.rules :as rules] |
| 10 | + [karabiner-configurator.layers :as layers] |
| 11 | + [karabiner-configurator.misc :refer [load-edn load-json massert]] |
| 12 | + [karabiner-configurator.modifiers :as modifiers] |
13 | 13 | [karabiner-configurator.profiles :as profiles]
|
14 |
| - [clojure.edn :as edn] |
15 |
| - [me.raynes.fs :as fs] |
16 |
| - [clojure.tools.cli :as cli] |
17 |
| - [environ.core :refer [env]]) |
| 14 | + [karabiner-configurator.rules :as rules] |
| 15 | + [karabiner-configurator.tos :as tos] |
| 16 | + [me.raynes.fs :as fs]) |
18 | 17 | (:gen-class))
|
19 | 18 |
|
20 | 19 | ;; helper function
|
21 | 20 | (defn update-static-conf
|
22 | 21 | "Helper function to update conf-data from reading rules"
|
23 | 22 | [key conf]
|
24 |
| - (if (nn? conf) |
25 |
| - (assoc-conf-data key conf))) |
| 23 | + (when (some? conf) |
| 24 | + (d/assoc-conf-data key conf))) |
26 | 25 |
|
27 | 26 | (defn check-edn-syntax
|
28 | 27 | "Call joker to check syntax of karabiner.edn"
|
29 | 28 | [path]
|
30 |
| - (let [;; intel mac |
31 |
| - joker-bin1 "/usr/local/opt/joker/bin/joker" |
32 |
| - ;; arm mac |
33 |
| - joker-bin2 "/opt/homebrew/opt/joker/bin/joker" |
34 |
| - ;; nix |
35 |
| - joker-bin3 (str (System/getenv "HOME") "/.nix-profile/bin/joker") |
36 |
| - ;; fallback to which joker |
37 |
| - joker-bin (cond (fs/exists? joker-bin1) joker-bin1 |
38 |
| - (fs/exists? joker-bin2) joker-bin2 |
39 |
| - (fs/exists? joker-bin3) joker-bin3 |
40 |
| - :else (-> (shell/sh "which" "joker") |
41 |
| - :out |
42 |
| - (string/trim-newline) |
43 |
| - (str "/bin/joker")))] |
44 |
| - (shell/sh joker-bin "--lint" path))) |
| 29 | + (let [sys-env (into {} (System/getenv))] |
| 30 | + (shell/sh "joker" "--lint" path |
| 31 | + :env (merge |
| 32 | + sys-env |
| 33 | + {"PATH" |
| 34 | + (str "/etc/profiles/per-user/" (System/getenv "USER") "/bin:" ;; nix profile |
| 35 | + "/run/current-system/sw/bin:" ;; nix darwin multiuser |
| 36 | + "/opt/homebrew/bin:" ;; arm homebrew |
| 37 | + "/usr/local/bin:" ;; homebrew |
| 38 | + (get sys-env "PATH"))})))) |
45 | 39 |
|
46 | 40 | (defn exit [status & [msg]]
|
47 |
| - (if msg (println msg)) |
48 |
| - (if (not (env :is-dev)) (System/exit status))) |
| 41 | + (when msg (println msg)) |
| 42 | + (when-not (env :is-dev) (System/exit status))) |
49 | 43 |
|
50 | 44 | ;; paths
|
51 | 45 | (defn json-config-file-path
|
|
73 | 67 | (defn parse-edn
|
74 | 68 | "Init conf data and return new rules based on karabiner.edn (main section in edn file)"
|
75 | 69 | [conf]
|
76 |
| - (init-conf-data) |
77 |
| - (let [{:keys [applications devices keyboard-type input-sources tos froms modifiers layers simlayers raws main simlayer-threshold templates profiles]} conf] |
| 70 | + (d/init-conf-data) |
| 71 | + (let [{:keys [applications devices keyboard-type input-sources tos froms modifiers layers simlayers ;; raws |
| 72 | + main simlayer-threshold templates profiles]} conf] |
78 | 73 | (if (nil? profiles)
|
79 |
| - (profiles/parse-profiles (:profiles conf-data)) |
| 74 | + (profiles/parse-profiles (:profiles d/conf-data)) |
80 | 75 | (profiles/parse-profiles profiles))
|
81 | 76 | (update-static-conf :applications applications)
|
82 | 77 | (update-static-conf :devices devices)
|
83 | 78 | (update-static-conf :keyboard-type keyboard-type)
|
84 | 79 | (update-static-conf :input-sources input-sources)
|
85 | 80 | (update-static-conf :templates templates)
|
86 |
| - (if (number? simlayer-threshold) |
| 81 | + (when (number? simlayer-threshold) |
87 | 82 | (update-static-conf :simlayer-threshold simlayer-threshold))
|
88 | 83 | (modifiers/parse-modifiers modifiers)
|
89 | 84 | (layers/parse-layers layers)
|
|
103 | 98 | (:profiles karabiner-config)]
|
104 | 99 | {(keyword (:name json-profile))
|
105 | 100 | json-profile}))]
|
106 |
| - (doseq [[profile-k profile-v] customized-profiles] |
| 101 | + (doseq [[profile-k _] customized-profiles] |
107 | 102 | (let [profile-name-str (name profile-k)]
|
108 | 103 | (massert
|
109 |
| - (nn? (profile-k user-profiles)) |
| 104 | + (some? (profile-k user-profiles)) |
110 | 105 | (format
|
111 | 106 | "Can't find profile named \"%s\" in karabiner.json, please create a profile named \"%s\" using the Karabiner-Elements.app."
|
112 | 107 | profile-name-str
|
|
143 | 138 | "Root function to parse karabiner.edn and update karabiner.json."
|
144 | 139 | [path & [dry-run dry-run-all]]
|
145 | 140 | (let [edn-syntax-err (:err (check-edn-syntax path))]
|
146 |
| - (if (> (count edn-syntax-err) 0) |
147 |
| - (do (println "Syntax error in config:") |
148 |
| - (println edn-syntax-err) |
149 |
| - (exit 1)))) |
| 141 | + (when (> (count edn-syntax-err) 0) |
| 142 | + (println "Syntax error in config:") |
| 143 | + (println edn-syntax-err) |
| 144 | + (exit 1))) |
150 | 145 | (update-to-karabiner-json (parse-edn (load-edn path)) dry-run dry-run-all))
|
151 | 146 |
|
152 | 147 | (defn open-log-file []
|
153 | 148 | (shell/sh "open" (log-file)))
|
154 | 149 | ;; cli stuff
|
155 | 150 |
|
156 |
| -(defn help-message [options-summary] |
| 151 | +(defn help-message [_] |
157 | 152 | (->> ["GokuRakuJoudo -- karabiner configurator"
|
158 | 153 | ""
|
159 | 154 | "goku will read config file and update `Goku` profile in karabiner.json"
|
|
233 | 228 | ;; main
|
234 | 229 | (defn -main
|
235 | 230 | [& args]
|
236 |
| - (let [{:keys [action options exit-message ok? config dry-run dry-run-all]} (validate-args args)] |
237 |
| - (if exit-message |
| 231 | + (let [{:keys [action ;; options |
| 232 | + exit-message ok? config dry-run dry-run-all]} (validate-args args)] |
| 233 | + (when exit-message |
238 | 234 | (case action
|
239 | 235 | "run" (do (parse (or config (edn-config-file-path)) dry-run dry-run-all)
|
240 | 236 | (exit (if ok? 0 1) exit-message))
|
|
0 commit comments