-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepl.go
More file actions
106 lines (96 loc) · 2.23 KB
/
repl.go
File metadata and controls
106 lines (96 loc) · 2.23 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
package main
import (
"bufio"
"fmt"
"os"
"strings"
"time"
"github.com/mamoss-oss/pokedexcli/internal/pokecache"
)
type cliCommand struct {
name string
description string
callback func(*config, ...string) error
}
type config struct {
cache pokecache.Cache
next string
previous string
pokedex map[string]PokemonData
}
func startRepl() {
userConfig := config{
cache: pokecache.NewCache(time.Second * 300),
next: "https://pokeapi.co/api/v2/location-area/",
pokedex: make(map[string]PokemonData),
}
for {
// Create a new scanner for reading from standard input
scanner := bufio.NewScanner(os.Stdin)
fmt.Print("Pokedex > ")
// Scan the user input and store it in a variable
scanner.Scan()
userCommand := scanner.Text()
userCommand = CleanText(userCommand)
split := strings.Split(userCommand, " ")
commands := getCommands()
c, ok := commands[split[0]]
if !ok {
fmt.Println("Sorry, command not found. Try 'help' for usage guidelines.")
continue
}
err := c.callback(&userConfig, split...)
if err != nil {
fmt.Println(err.Error())
}
}
}
func CleanText(s string) string {
s = strings.ToLower(s)
s = strings.TrimSpace(s)
return s
}
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,
},
"map": {
name: "map",
description: "Show the next location area",
callback: commandMap,
},
"mapb": {
name: "mapb",
description: "Show the previous location area",
callback: commandMapb,
},
"explore": {
name: "explore",
description: "Explore the Pokemons in a location",
callback: commandExplore,
},
"catch": {
name: "catch",
description: "Attempt to catch a Pokemon",
callback: commandCatch,
},
"inspect": {
name: "inspect",
description: "Check out a known pokemon in your pokedex",
callback: commandInspect,
},
"pokedex": {
name: "pokedex",
description: "List all Pokemons in your current pokedex",
callback: commandPokedex,
},
}
}