-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepl.go
More file actions
134 lines (111 loc) · 2.9 KB
/
repl.go
File metadata and controls
134 lines (111 loc) · 2.9 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"bufio"
"fmt"
"os"
"strings"
"github.com/opsmonkey/pokedexcli/internal/pokeapi"
)
type config struct {
pokeapiClient *pokeapi.Client
Previous *string
Next *string
pokedex map[string]pokeapi.Pokemon
}
func (c *config) SetNext(url string) {
c.Next = &url
}
func (c *config) SetPrevious(url string) {
c.Previous = &url
}
// struct to map commands to callback functions
type cliCommand struct {
name string
description string
callback func(*config, ...string) error
}
func getCommandsRegister() map[string]cliCommand {
return map[string]cliCommand{
"exit": {
name: "exit",
description: "Exit the Pokedex",
callback: commandExit,
},
"help": {
name: "help",
description: "Displays a help message",
callback: commandHelp,
},
"map": {
name: "map",
description: "Displays location areas from the PokeAPI",
callback: commandMap,
},
"mapb": {
name: "mapb",
description: "Displays previous location areas from the PokeAPI",
callback: commandMapBack,
},
"explore": {
name: "explore",
description: "provides a list of all pokemon found in a given area",
callback: commandExploreLocation,
},
"catch": {
name: "catch",
description: "catch a pokemon!",
callback: commandCatch,
},
"inspect": {
name: "inspect",
description: "inspect a caught pokemon",
callback: commandInspect,
},
"pokedex": {
name: "pokedex",
description: "check out all of your caught pokemon",
callback: commandPokedex,
},
}
}
// Starts the REPL loop for the main program
func startRepl(cfg *config) {
scanner := bufio.NewScanner(os.Stdin)
commands := getCommandsRegister()
for {
fmt.Print("Pokedex > ")
if !scanner.Scan() {
break
}
input := scanner.Text()
cleaned := cleanInput(input)
// if an empty array is returned, continue to next iteration of the loop
if len(cleaned) == 0 {
continue
}
// if clean input returns more than one value, there's potentially a sub command/arg to process
var commandArgs []string
if len(cleaned) > 1 {
commandArgs = cleaned[1:]
}
command, exists := commands[cleaned[0]]
if !exists {
fmt.Println("Unknown command")
continue
}
if err := command.callback(cfg, commandArgs...); err != nil {
fmt.Println(err)
}
}
// check for any errors encountered while reading from stdin
if err := scanner.Err(); err != nil {
errorOut := fmt.Errorf("error reading input from std in: %v", err)
fmt.Print(errorOut)
}
}
// cleanInput splits the user's input into "words" based on whitespace. Output is lowercase, trimming any leading or trailing whitespace.
func cleanInput(text string) []string {
// Fields splits the string s around each instance of one or more consecutive white space characters
split := strings.Fields(strings.ToLower(text))
return split
}