-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepl.go
101 lines (92 loc) · 1.89 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
package main
import (
"bufio"
"fmt"
"os"
"pokedex-go/pokeapi"
"strings"
)
type config struct {
pokeApiClient pokeapi.Client
nextLocation *string
prevLocation *string
caughtPokemon map[string]pokeapi.Pokemon
}
type cliCommand struct {
name string
description string
callback func(*config, []string) error
}
func startRepl(cfg *config) {
reader := bufio.NewScanner(os.Stdin)
for {
fmt.Print("Pokedex > ")
reader.Scan()
words := cleanInput(reader.Text())
if len(words) == 0 {
continue
}
commandName := words[0]
parameters := words[1:]
command, exists := getCommands()[commandName]
if exists {
err := command.callback(cfg, parameters)
if err != nil {
fmt.Println(err)
}
continue
} else {
fmt.Println("Unknown command")
continue
}
}
}
func cleanInput(txt string) []string {
output := strings.ToLower(txt)
words := strings.Fields(output)
return words
}
func getCommands() map[string]cliCommand {
return map[string]cliCommand{
"help": {
name: "help",
description: "Displays a help message",
callback: Help,
},
"map": {
name: "map",
description: "Get next page of locations",
callback: commandMap,
},
"mapb": {
name: "mapb",
description: "Get previous page of locations",
callback: commandMap2,
},
"explore": {
name: "explore <location_name>",
description: "Explore a location",
callback: Explore,
},
"catch": {
name: "catch <pokemon_name>",
description: "Attempt to catch a Pokemon",
callback: Catch,
},
"inspect": {
name: "inspect <pokemon_name>",
description: "Inspect a Pokemon",
callback: Inspect,
},
"pokedex": {
name: "pokedex",
description: "Lists caught Pokemon",
callback: Pokedex,
},
"exit": {
name: "exit",
description: "Exits the Pokedex",
callback: Exit,
},
}
}