-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepl.go
More file actions
108 lines (95 loc) · 2.28 KB
/
Copy pathrepl.go
File metadata and controls
108 lines (95 loc) · 2.28 KB
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
package main
import (
"bufio"
"fmt"
"os"
"strings"
"github.com/rjt5412/pokedexcli/internal/pokeapi"
)
type cliCommand struct {
name string
description string
callback func(*config, ...string) error
}
type config struct {
pokeapiClient pokeapi.Client
nextLocationURL *string
previousLocationURL *string
caughtPokemon map[string]pokeapi.Pokemon
}
func getCommands() map[string]cliCommand {
return map[string]cliCommand{
"help": {
name: "help",
description: "Display the help message",
callback: commandHelp,
},
"exit": {
name: "exit",
description: "Exit the pokedex",
callback: commandExit,
},
"map": {
name: "map",
description: "Display the names of next 20 location areas in the pokemon world based on current location",
callback: commandMapf,
},
"mapb": {
name: "mapb",
description: "Display the names of previous 20 locations based on current location",
callback: commandMapb,
},
"explore": {
name: "explore <location_name>",
description: "Explore a location",
callback: commandExplore,
},
"catch": {
name: "catch <pokemon_name>",
description: "Attempt to catch a pokemon and add it to your pokedex",
callback: commandCatch,
},
"inspect": {
name: "inspect <pokemon_name>",
description: "Inspects a pokemon to reveal more info about it",
callback: commandInspect,
},
"pokedex": {
name: "pokedex <pokemon_name>",
description: "Shows a list of all pokemon you have caught so far...",
callback: commandPokedex,
},
}
}
func startRepl(cfg *config) {
reader := bufio.NewScanner(os.Stdin)
commands := getCommands()
for {
fmt.Print("Pokedex >")
reader.Scan()
cleaned := cleanInput(reader.Text())
if len(cleaned) == 0 {
continue
}
commandName := cleaned[0]
args := []string{}
if len(cleaned) > 1 {
args = cleaned[1:]
}
command, ok := commands[commandName]
if ok {
err := command.callback(cfg, args...)
if err != nil {
fmt.Println(err)
}
} else {
fmt.Println("Invalid command")
commands["help"].callback(cfg, args...)
}
}
}
func cleanInput(input string) []string {
lowered := strings.ToLower(input)
words := strings.Fields(lowered)
return words
}