Skip to content

Commit

Permalink
add simple conditions in rules feature
Browse files Browse the repository at this point in the history
  • Loading branch information
yqrashawn committed Sep 24, 2018
1 parent bb20d67 commit a2cfdc8
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 16 deletions.
50 changes: 41 additions & 9 deletions src/karabiner_configurator/rules.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
[karabiner-configurator.keys :as keys]
[karabiner-configurator.froms :as froms]
[karabiner-configurator.tos :as tos]
[karabiner-configurator.conditions :as condis]
[karabiner-configurator.data :refer :all]
[karabiner-configurator.misc :refer :all]))

Expand Down Expand Up @@ -251,6 +252,13 @@
[result insert-simlayer])
result)]
result)))
(def current-in-rules-conditions nil)
(defn dcirc [condis]
(if (nil? condis)
(def current-in-rules-conditions nil)
(if (not (vector? condis))
(def current-in-rules-conditions [condis])
(def current-in-rules-conditions (pop (into [] (reverse condis)))))))

(defn generate
"generate one rule"
Expand All @@ -260,13 +268,38 @@
:manipulators
(into []
(flatten
(for [rule rules]
(let [[from to condition other-options] rule]
(do
(assert (and (nn? from) (or (nn? other-options) (nn? to))) (str "invalid rule: " des ", <from> or <to> is nil"))
(cond (and (nil? other-options) (nil? condition)) (parse-rule des from to)
(and (nil? other-options) (nn? condition)) (parse-rule des from to condition)
(nn? other-options) (parse-rule des from to condition other-options)))))))})
(let [processed-rules
(into [] (for [rule rules]
(if (or (keyword? rule) (and (vector? rule) (= (first rule) :condi)))
(do (dcirc rule) nil)
(if (nn? current-in-rules-conditions)
(let [[from to conditions other-options] rule
vector-conditions? (vector? conditions)
simple-set-variable? (and vector-conditions? (condis/is-simple-set-variable? conditions))
keyword-conditions? (keyword? conditions)
conditions
(cond (or simple-set-variable? keyword-conditions?)
(conj current-in-rules-conditions conditions)
vector-conditions?
(into [] (concat current-in-rules-conditions conditions))
:else
current-in-rules-conditions)]
(cond (nn? other-options)
[from to conditions other-options]
(nn? conditions)
[from to conditions]
:else
[from to]))
rule))))
cleanup-circ (dcirc nil)]
(for [rule processed-rules
:when (nn? rule)]
(let [[from to condition other-options] rule]
(do
(assert (and (nn? from) (or (nn? other-options) (nn? to))) (str "invalid rule: " des ", <from> or <to> is nil"))
(cond (and (nil? other-options) (nil? condition)) (parse-rule des from to)
(and (nil? other-options) (nn? condition)) (parse-rule des from to condition)
(nn? other-options) (parse-rule des from to condition other-options))))))))})
layer-result {:description "auto generated layer trigger key"
:manipulators (into [] (for [[layer-name layer-defination] (:layers conf-data)]
layer-defination))}
Expand All @@ -276,5 +309,4 @@
(defn parse-mains
"parse main section to final edn format, ready to convert to json"
[mains]
;; (into [] (generate mains))
(generate mains))
(generate mains))
2 changes: 1 addition & 1 deletion test/karabiner_configurator/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
(t/testing "generate vi-mode config"
(t/is (= (sut/generate vi-mode-example) vi-mode-result)))
(init-conf-data)
(t/testing "generate vi-mode config"
(t/testing "generate launch-mode config"
(t/is (= (sut/generate launch-mode-example) launch-mode-result))))
;; (sut/parse launch-mode-example)
23 changes: 17 additions & 6 deletions test/karabiner_configurator/rules_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,14 @@
:rules [[:i :us :q-mode]
[:o :squirrel :q-mode]]}
{:des "tab-mode"
:rules [[:h "/usr/local/bin/chunkc tiling::window --warp west" :chunkwm-move-mode]
[:h "/usr/local/bin/chunkc tiling::window --use-temporary-ratio 0.03 --adjust-window-edge west" :chunkwm-scale-mode]
[:h "/usr/local/bin/chunkc tiling::window --focus west" :tab-mode]]}])

:rules [:chunkwm-move-mode
[:h "/usr/local/bin/chunkc tiling::window --warp west"]
:chunkwm-scale-mode
[:h "/usr/local/bin/chunkc tiling::window --use-temporary-ratio 0.03 --adjust-window-edge west"]
:tab-mode
[:h "/usr/local/bin/chunkc tiling::window --focus west"]
[:condi :chunkwm-move-mode :chunkwm-scale-mode]
[:l "/usr/local/bin/chunkc tiling::window --focus east"]]}])

(def result [{:description "auto generated layer trigger key",
:manipulators [{:type "basic",
Expand Down Expand Up @@ -277,9 +281,16 @@
:conditions [{:name "tab-mode",
:value 1,
:type "variable_if"}],
:type "basic"}
{:from {:key_code "l"},
:to
[{:shell_command
"/usr/local/bin/chunkc tiling::window --focus east"}],
:conditions
[{:name "chunkwm-scale-mode", :value 1, :type "variable_if"}
{:name "chunkwm-move-mode", :value 1, :type "variable_if"}],
:type "basic"}]}])


(t/deftest generate-mains
(init-conf-data)
(update-conf-data {:applications {:safari ["^com\\.apple\\.Safari$"]
Expand Down Expand Up @@ -347,4 +358,4 @@


(t/testing
(t/is (= (sut/parse-mains example-mains) result))))
(t/is (= (sut/parse-mains example-mains) result))))

0 comments on commit a2cfdc8

Please sign in to comment.