1st commit, making a pokedex lol

This commit is contained in:
V 2025-09-28 21:15:55 +01:00
commit 9bfdfb7f07
7 changed files with 163 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
pokedexcli
repl.log

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.Print("Pokedex CLI - available commands:\n\n")
for _, v := range getCommands() {
fmt.Printf("%s %s\n", v.name, v.description)
}
fmt.Println()
return nil
}

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module k3gtpi.jumpingcrab.com/go-learning/pokedexcli
go 1.24.5

5
main.go Normal file
View File

@ -0,0 +1,5 @@
package main
func main() {
startRepl()
}

67
repl.go Normal file
View File

@ -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,
},
}
}

59
repl_test.go Normal file
View File

@ -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)
}