Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Broaden allowed syntax for DSL #31

Merged
merged 3 commits into from
Sep 24, 2015
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 32 additions & 13 deletions src/cljck/io.clj
Original file line number Diff line number Diff line change
Expand Up @@ -20,58 +20,77 @@

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))

(defn valid-commands
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not really an IO-related function, and it should really have a docstring.

[]
(->> process-event
(.getMethodTable)
(map first)
(remove (partial = :default))
(sort)
(clojure.string/join ", ")))

(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:" (valid-commands))
(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