/
repl.clj
55 lines (49 loc) · 1.45 KB
/
repl.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
(ns jubot.adapter.repl
"Jubot adapter for REPL."
(:require
[jubot.adapter.util :refer :all]
[jubot.redef :refer :all]
[com.stuartsierra.component :as component]))
(def ^{:doc "User name. (default value is \"nobody\")"}
username (or (getenv* "USER") "nobody"))
(defn process-output
"Process output to REPL.
Params
this - REPL adapter.
:name - Bot's name.
s - Output text to REPL.
option
:as - Overwrite bot's name if you specify this option.
"
[this s & {:keys [as] :as option}]
(let [name (or as (:name this))]
(println* (str name "=> " s))))
(defn process-input
"Process input from REPL.
Params
this - REPL adapter.
:name - Bot's name.
:handler - A handler function.
s - Input text from REPL.
"
[{:keys [name handler] :as this} s]
(let [option {:user username :channel nil}]
(some->> s
(parse-text name)
(merge option)
handler
(process-output this))))
(defrecord ReplAdapter [name handler in out]
component/Lifecycle
(start [this]
(if (and in out)
this
(do (println ";; start repl adapter. bot name is" name)
(assoc this
:in (partial process-input this)
:out (partial process-output this)))))
(stop [this]
(if-not (and in out)
this
(do (println ";; stop repl adapter")
(assoc this :in nil :out nil)))))