forked from nathany/looper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.go
55 lines (46 loc) · 795 Bytes
/
input.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
package main
import (
"io"
"log"
"strings"
"github.com/bobappleyard/readline"
)
type Command int
const (
Unknown Command = iota
Help
Exit
RunAll
)
func CommandParser() <-chan Command {
commands := make(chan Command, 1)
go func() {
for {
in, err := readline.String("")
if err == io.EOF { // Ctrl+D
commands <- Exit
break
} else if err != nil {
log.Fatal(err)
}
commands <- NormalizeCommand(in)
readline.AddHistory(in)
}
}()
return commands
}
func NormalizeCommand(in string) (c Command) {
command := strings.ToLower(strings.TrimSpace(in))
switch command {
case "exit", "e", "x", "quit", "q":
c = Exit
case "all", "a", "":
c = RunAll
case "help", "h", "?":
c = Help
default:
UnknownCommand(command)
c = Unknown
}
return c
}