-
Notifications
You must be signed in to change notification settings - Fork 0
/
repl.go
102 lines (86 loc) · 1.93 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
func startrepl(cf *config) {
read := bufio.NewScanner(os.Stdin)
for {
fmt.Printf("Poke-it >")
read.Scan()
input := cleanText(read.Text())
if len(input) == 0 {
continue
}
command := input[0]
args := []string{}
if len(input) > 1 {
args = input[1:]
}
value, exist := commandIndex()[command]
if !exist {
fmt.Println("The command doesnt exist")
fmt.Println("")
fmt.Println(commandHelp(cf))
continue
}
value.callback(cf, args...)
fmt.Println("")
}
}
func cleanText(text string) []string {
Lower := strings.ToLower(text) // streamling input
words := strings.Fields(Lower)
return words
}
type commands struct {
method string
description string
callback func(*config, ...string) error
}
func commandIndex() map[string]commands {
return map[string]commands{
"help": {
method: "help",
description: "displays cli commands",
callback: commandHelp,
},
"map": {
method: "map",
description: "displays Location Areas",
callback: commandMap,
},
"mapb": {
method: "mapb",
description: "displays previous Location Areas",
callback: commandMapb,
},
"catch": {
method: "catch",
description: "Catches the pokemon present in the area",
callback: commandCatch,
},
"inspect": {
method: "inspect",
description: "displays info of previously caught pokemon",
callback: commandInspect,
},
"explore": {
method: "explore",
description: "displays pokemons in the Location Area",
callback: commandExplore,
},
"pokedex": {
method: "pokedex",
description: "displays all the pokemons caught",
callback: commandPokedex,
},
"exit": {
method: "exit",
description: "exits cli",
callback: commandExit,
},
}
}