package main import ( "fmt" "strings" "bufio" "os" "k3gtpi.jumpingcrab.com/go-learning/pokedexcli/internal/pokecache" "time" ) type cliCommand struct { name string description string callback func(*PokedexConfig, *pokecache.Cache) error } type PokedexConfig struct { Next *string Previous *string Results []map[string]string } var supportedCommands map[string]cliCommand func startRepl() { reader := bufio.NewScanner(os.Stdin) fmt.Println("Welcome to the Pokedex!") pokedexConfig := PokedexConfig{} cache := pokecache.NewCache(5 * time.Second) for { // Print prompt fmt.Printf("Pokedex > ") reader.Scan() words := cleanInput(reader.Text()) if len(words) == 0 { continue } commandName := words[0] command, valid := getCommands()[commandName] if !valid { fmt.Println("Unknown command.") continue } if err := command.callback(&pokedexConfig, cache); err != nil { fmt.Printf("Encountered error running command: %v\nErr: %v", command.name, err) } } } func cleanInput(text string) []string { if len(text) == 0 { fmt.Println("Input is empty!") return []string{} } output := strings.ToLower(text) words := strings.Fields(output) return words } func getCommands() map[string]cliCommand{ return map[string]cliCommand{ "help": { name: "help", description: "Prints this help menu.", callback: commandHelp, }, "exit": { name: "exit", description: "Exits the Pokedex", callback: commandExit, }, "map": { name: "map", description: "Print Pokemon world locations.", callback: commandMap, }, "mapb": { name: "mapb", description: "Print previoud Pokemon locationsi.", callback: commandMapb, }, } }