From 9bfdfb7f07270fdd811f76007e59799485d209d3 Mon Sep 17 00:00:00 2001 From: V Date: Sun, 28 Sep 2025 21:15:55 +0100 Subject: [PATCH] 1st commit, making a pokedex lol --- .gitignore | 2 ++ command_exit.go | 12 +++++++++ command_help.go | 15 +++++++++++ go.mod | 3 +++ main.go | 5 ++++ repl.go | 67 +++++++++++++++++++++++++++++++++++++++++++++++++ repl_test.go | 59 +++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 163 insertions(+) create mode 100644 .gitignore create mode 100644 command_exit.go create mode 100644 command_help.go create mode 100644 go.mod create mode 100644 main.go create mode 100644 repl.go create mode 100644 repl_test.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..51aa5bb --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +pokedexcli +repl.log diff --git a/command_exit.go b/command_exit.go new file mode 100644 index 0000000..ca866ff --- /dev/null +++ b/command_exit.go @@ -0,0 +1,12 @@ +package main + +import ( + "fmt" + "os" +) + +func commandExit() error { + fmt.Println("Closing the Pokedex... Goodbye!") + os.Exit(0) + return nil +} diff --git a/command_help.go b/command_help.go new file mode 100644 index 0000000..6e389cf --- /dev/null +++ b/command_help.go @@ -0,0 +1,15 @@ +package main + +import ( + "fmt" +) + + +func commandHelp() error { + fmt.Print("Pokedex CLI - available commands:\n\n") + for _, v := range getCommands() { + fmt.Printf("%s %s\n", v.name, v.description) + } + fmt.Println() + return nil +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..0e362a9 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module k3gtpi.jumpingcrab.com/go-learning/pokedexcli + +go 1.24.5 diff --git a/main.go b/main.go new file mode 100644 index 0000000..0840565 --- /dev/null +++ b/main.go @@ -0,0 +1,5 @@ +package main + +func main() { + startRepl() +} diff --git a/repl.go b/repl.go new file mode 100644 index 0000000..89366f4 --- /dev/null +++ b/repl.go @@ -0,0 +1,67 @@ +package main + +import ( + "fmt" + "strings" + "bufio" + "os" +) + +type cliCommand struct { + name string + description string + callback func() error +} + +var supportedCommands map[string]cliCommand + +func startRepl() { + reader := bufio.NewScanner(os.Stdin) + fmt.Println("Welcome to the Pokedex!") + 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(); err != nil { + fmt.Printf("Encountered error running command: %v\n", command.name) + } + } +} + +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, + }, + } +} diff --git a/repl_test.go b/repl_test.go new file mode 100644 index 0000000..e6a58da --- /dev/null +++ b/repl_test.go @@ -0,0 +1,59 @@ +package main + +import ( + "testing" + "fmt" +) + +func TestCleanInput(t * testing.T) { + cases := []struct { + input string + expected []string + }{ + { + input: " hello world ", + expected: []string{"hello", "world"}, + }, + { + input: "Charmander Bulbasaur PIKACHU", + expected: []string{"charmander", "bulbasaur", "pikachu"}, + }, + { + input: "", + expected: []string{}, + }, + } + successCount := 0 + failCount := 0 + passed := true + + for _, c := range cases { + output := cleanInput(c.input) + for i := range output { + word := output[i] + expectedWord := c.expected[i] + if word != expectedWord { + passed = false + break + } + } + if !passed { + t.Errorf(` +=================== +Input: %v +Expected: %v +Output: %v +`, c.input, c.expected, output) + failCount++ + } else { + fmt.Printf(` +=================== +Input: %v +Expected: %v +Output: %v +`, c.input, c.expected, output) + successCount++ + } + } + fmt.Printf("%d passed, %d failed", successCount, failCount) +}