Started working on map command

This commit is contained in:
enordaz 2025-05-31 21:13:56 -06:00
parent 94f4124f04
commit c26a22c6d7
4 changed files with 91 additions and 16 deletions

12
command_exit.go Normal file
View File

@ -0,0 +1,12 @@
package main
import (
"fmt"
"os"
)
func commandExit() error {
fmt.Println("Closing the Pokedex... Goodbye!")
os.Exit(0)
return nil
}

15
command_help.go Normal file
View File

@ -0,0 +1,15 @@
package main
import "fmt"
func commandHelp() error {
fmt.Println()
fmt.Println("Welcome to the Pokedex!")
fmt.Println("Usage:")
fmt.Println()
for _, c := range getCommands() {
fmt.Printf("\t%v\t\t%v\n", c.name, c.description)
}
fmt.Println()
return nil
}

16
main.go
View File

@ -1,21 +1,7 @@
package main package main
import (
"bufio"
"fmt"
"os"
)
const prompt = "Pokedex > " const prompt = "Pokedex > "
func main() { func main() {
dexScanner := bufio.NewScanner(os.Stdin) startRepl()
for {
fmt.Print(prompt)
if !dexScanner.Scan() {
break
}
command := cleanInput(dexScanner.Text())
fmt.Println("Your command was:", command[0])
}
} }

64
repl.go
View File

@ -1,6 +1,68 @@
package main package main
import "strings" import (
"bufio"
"fmt"
"os"
"strings"
)
func getCommands() 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 the names of the next 20 locations",
},
"mapb": {
name: "mapb",
description: "Displays the names of the previous 20 locations",
},
}
}
func startRepl() {
reader := bufio.NewScanner(os.Stdin)
for {
fmt.Print(prompt)
reader.Scan()
words := cleanInput(reader.Text())
if len(words) == 0 {
continue
}
inputCommand := words[0]
command, exists := getCommands()[inputCommand]
if exists {
err := command.callback()
if err != nil {
fmt.Printf("%v error: %v", command.name, err.Error())
}
} else {
fmt.Println("Unknown command")
continue
}
}
}
type cliCommand struct {
name string
description string
callback func() error
}
func cleanInput(text string) []string { func cleanInput(text string) []string {
input := strings.ToLower(text) input := strings.ToLower(text)