-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepl.go
98 lines (86 loc) · 1.79 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
package main
import (
"bufio"
"fmt"
"os"
"strings"
"github.com/bigveezus/my-cli/internal/pokeapi"
)
type config struct {
pokeapiClient pokeapi.Client
nextLocationsURL *string
prevLocationsURL *string
}
func startRepl(cfg *config) {
reader := bufio.NewScanner(os.Stdin)
for {
fmt.Println("Enter `help` to see a list of all commands")
fmt.Print("Pokedex > ")
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(cfg, args...)
if err != nil {
fmt.Println(err)
}
continue
} else {
fmt.Println("Unknown command")
fmt.Println("Enter `help` to see a list of commands ")
continue
}
}
}
func cleanInput(text string) []string {
output := strings.ToLower(text)
words := strings.Fields(output)
return words
}
type cliCommand struct {
name string
description string
callback func(*config, ...string) error
}
func getCommands() map[string]cliCommand {
return map[string]cliCommand{
"help": {
name: "help",
description: "Displays a help message",
callback: commandHelp,
},
"exit": {
name: "exit",
description: "Exit the Pokedex",
callback: commandExit,
},
"about": {
name: "about",
description: "Tells you about the owner Elplay!",
callback: commandAbout,
},
"map": {
name: "map",
description: "Get the next map of locations",
callback: commandMapf,
},
"mapb": {
name: "mapb",
description: "Get the previous map of locations",
callback: commandMapb,
},
"explore": {
name: "explore <location>",
description: "Explore location",
callback: commandExplore,
},
}
}