forked from brettbuddin/victor
/
shell.go
58 lines (44 loc) · 1.07 KB
/
shell.go
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
56
57
58
package victor
import (
"fmt"
"strings"
"github.com/edsrzf/fineline"
)
type Shell struct {
brain *Brain
}
func NewShell(name string) *Shell {
return &Shell{brain: NewBrain(name)}
}
func (self *Shell) Run() {
reader := fineline.NewLineReader(nil)
for {
command, err := reader.Read()
command = strings.TrimRight(command, "\n")
if err != nil {
break
}
msg := &TextMessage{
Body: command,
Send: func(text string) {
fmt.Println(text)
},
Reply: func(text string) {
fmt.Println("You: " + text)
},
}
switch command {
default:
self.brain.Receive(msg)
case "close", "exit":
self.brain.Shutdown()
return
}
}
}
func (self *Shell) Hear(expStr string, callback func(*TextMessage)) {
self.brain.Hear(expStr, callback)
}
func (self *Shell) Respond(expStr string, callback func(*TextMessage)) {
self.brain.Respond(expStr, callback)
}