-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.go
41 lines (34 loc) · 894 Bytes
/
cli.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
package fsmcli
import (
"bufio"
"fmt"
"os"
"github.com/go-carrot/fsm"
)
func Start(stateMachine fsm.StateMachine, startState string) {
// Create Emitter
emitter := &CommandLineEmitter{}
// Create Traverser
traverser := &CachedTraverser{
Data: make(map[string]interface{}, 0),
}
traverser.SetCurrentState(startState)
traverser.SetUUID("CLI-USER")
// Get Start State
currentState := stateMachine[startState](emitter, traverser)
currentState.EntryAction()
// Prep Reader
reader := bufio.NewReader(os.Stdin)
for {
// Read Input from User
fmt.Print("-> ")
text, _ := reader.ReadString('\n')
text = text[:len(text)-1]
// Pass Input to State
currentState = stateMachine[traverser.CurrentState()](emitter, traverser)
newState := currentState.Transition(text)
newState.EntryAction()
currentState = newState
traverser.SetCurrentState(currentState.Slug)
}
}