Started working on map command
This commit is contained in:
parent
94f4124f04
commit
c26a22c6d7
12
command_exit.go
Normal file
12
command_exit.go
Normal 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
15
command_help.go
Normal 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
16
main.go
@ -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
64
repl.go
@ -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)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user