-
Notifications
You must be signed in to change notification settings - Fork 0
/
repl.go
75 lines (63 loc) · 1.25 KB
/
repl.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
func startRepl(cfg *config) {
scanner := bufio.NewScanner(os.Stdin)
for {
fmt.Print("pokedex> ")
scanner.Scan()
text := scanner.Text()
cleanedInput := cleanInput(text)
if len(cleanedInput) == 0 {
continue
}
commandName := cleanedInput[0]
availableCommands := getCommands()
command, ok := availableCommands[commandName]
if !ok {
fmt.Println("Invalid commands")
continue
}
err := command.callback(cfg)
if err != nil {
fmt.Println(err)
}
}
}
type cliCommand struct {
name string
description string
callback func(*config) error
}
func getCommands() map[string]cliCommand {
return map[string]cliCommand{
"help": {
name: "help",
description: "displays help message",
callback: callbackHelp,
},
"map": {
name: "map",
description: "get location near",
callback: callbackMap,
},
"map_back": {
name: "map",
description: "get previous location",
callback: callbackMapPrevious,
},
"exit": {
name: "exit",
description: "exit cli menu",
callback: callbackExit,
},
}
}
func cleanInput(str string) []string {
lowered := strings.ToLower(str)
return strings.Fields(lowered)
}