-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepl.go
50 lines (41 loc) · 980 Bytes
/
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
package main
import (
"bufio"
"fmt"
"os"
"strings"
"github.com/grevgeny/pokedexcli/internal/pokeapi"
)
type config struct {
nextLocationsURL *string
prevLocationsURL *string
catchedPokemons map[string]pokeapi.Pokemon
pokeapiClient pokeapi.Client
}
func initREPL(cfg *config) {
scanner := bufio.NewScanner(os.Stdin)
for {
fmt.Print("Pokedex > ")
if !scanner.Scan() {
break
}
processedInput := processInput(scanner.Text())
cmdName := processedInput[0]
var params []string
if len(processedInput) > 1 {
params = processedInput[1:]
}
command, ok := getCommands()[cmdName]
if !ok {
fmt.Print("\nCommand not supported! Type 'help' to see available commands.\n\n")
continue
}
if err := command.callback(cfg, params...); err != nil {
fmt.Fprintf(os.Stderr, "Error executing command: %s\n", err)
}
}
}
func processInput(input string) []string {
loweredInput := strings.ToLower(input)
return strings.Fields(loweredInput)
}