Skip to content

Commit

Permalink
Merge pull request #31 from emiln/broaden-syntax
Browse files Browse the repository at this point in the history
Broaden allowed syntax for DSL
  • Loading branch information
emiln committed Sep 24, 2015
2 parents 667c9f4 + b76d913 commit 38e2ec1
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
37 changes: 24 additions & 13 deletions src/cljck/io.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(ns cljck.io
(:gen-class)
(:require
[cljck.utils :refer [multi-functions]]
[cljck.io
[keyboard :refer [press]]
[mouse :refer [click move-to mouse-pointer scroll-down scroll-up]]]
Expand All @@ -20,58 +21,68 @@
The argument is expected to be a vector like [:move-to 100 100] or [:click].
Dispatches on first."
first)

(defmethod process-event :click
(comp name first))

(defmethod process-event :default
[exp]
(throw (IllegalArgumentException.
(with-out-str
(println "The following expression wasn't recognized:")
(clojure.pprint/pprint exp)
(println "No such command:" (first exp))
(println "Valid commands:" (multi-functions process-event))
(println "Check the API in the README for help.")))))

(defmethod process-event "click"
[_]
(click :left))

(defmethod process-event :if
(defmethod process-event "if"
[[_ condition then else]]
(if (process-event condition)
(process-event then)
(process-event else)))

(defmethod process-event :move-to
(defmethod process-event "move-to"
[[_ x y]]
(move-to x y))

(defmethod process-event :pointer-near
(defmethod process-event "pointer-near"
[[_ x y distance]]
(let [[mouse-x mouse-y] (mouse-pointer)]
(and (< (- x distance) mouse-x (+ x distance))
(< (- y distance) mouse-y (+ y distance)))))

(defmethod process-event :press
(defmethod process-event "press"
[[_ key-string]]
(press key-string))

(defmethod process-event :repeat
(defmethod process-event "repeat"
[[_ n & commands]]
(dotimes [_ n]
(doseq [command commands]
(process-event command))))

(defmethod process-event :repeatedly
(defmethod process-event "repeatedly"
[[_ & commands]]
(loop []
(doseq [command commands]
(process-event command))
(recur)))

(defmethod process-event :scroll-down
(defmethod process-event "scroll-down"
[[_ & amount]]
(apply scroll-down amount))

(defmethod process-event :scroll-up
(defmethod process-event "scroll-up"
[[_ & amount]]
(apply scroll-up amount))

(defmethod process-event :wait
(defmethod process-event "wait"
[[_ miliseconds]]
(<!! (timeout miliseconds)))

(defmethod process-event :when
(defmethod process-event "when"
[[_ condition & body]]
(when (process-event condition)
(apply process-event body)))
Expand Down
10 changes: 10 additions & 0 deletions src/cljck/utils.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
(ns cljck.utils)

(defn multi-functions
"Returns a sorted list of the functions contained in the multimethod."
[multimethod]
(->> multimethod
(.getMethodTable)
(map first)
(remove (partial = :default))
(sort)))

0 comments on commit 38e2ec1

Please sign in to comment.