-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepl.go
116 lines (103 loc) · 2.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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main
import (
"bufio"
"fmt"
"os"
"strings"
"time"
"github.com/souloutz/pokedex/internal/pokeapi"
)
const (
RESET = "\033[0m"
RED = "\033[31m"
GREEN = "\033[32m"
YELLOW = "\033[33m"
BLUE = "\033[34m"
MAGENTA = "\033[35m"
CYAN = "\033[36m"
GRAY = "\033[37m"
WHITE = "\033[97m"
)
type config struct {
pokeapiClient pokeapi.Client
nextLocationsURL *string
prevLocationsURL *string
caughtPokemon map[string]pokeapi.Pokemon
}
func startRepl(config *config) {
reader := bufio.NewScanner(os.Stdin)
fmt.Printf("%sStarting the Pokedex...\n", YELLOW)
time.Sleep(1 * time.Second)
for {
fmt.Printf("%sPokedex > %s", RED, RESET)
reader.Scan()
words := cleanInput(reader.Text())
if len(words) == 0 {
continue
}
commandName := words[0]
args := []string{}
if len(words) > 1 {
args = words[1:]
}
command, exists := getCommands()[commandName]
if exists {
err := command.callback(config, args...)
if err != nil {
fmt.Println(err)
}
continue
} else {
fmt.Println("Unknown command")
continue
}
}
}
func cleanInput(text string) []string {
words := strings.Fields(strings.ToLower(text))
return words
}
func getCommands() map[string]cliCommand {
return map[string]cliCommand{
"help": {
name: "help",
description: "Displays a help message",
callback: commandHelp,
},
"catch": {
name: "catch <pokemon_name>",
description: "Attempt to catch a Pokemon",
callback: commandCatch,
},
"inspect": {
name: "inspect <pokemon_name>",
description: "View details about a caught Pokemon",
callback: commandInspect,
},
"map": {
name: "map",
description: "Get the next page of location areas",
callback: commandMapf,
},
"mapb": {
name: "mapb",
description: "Get the previous page of location areas",
callback: commandMapb,
},
"explore": {
name: "explore <location_name>",
description: "Explore a location",
callback: commandExplore,
},
"pokedex": {
name: "pokedex",
description: "See all of your caught pokemon",
callback: commandPokedex,
},
"exit": {
name: "exit",
description: "Exit the Pokedex",
callback: commandExit,
},
}
}