Compare commits

..

1 commit

Author SHA1 Message Date
c098a4c8ce
update documentation 2024-09-23 17:08:24 +01:00
6 changed files with 35 additions and 57 deletions

2
.gitignore vendored
View file

@ -1 +1 @@
/pokecli
pokecli

View file

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

View file

@ -1,13 +0,0 @@
package main
import "maps"
func summaryMap(commandMap map[string]command) map[string]string {
summaries := make(map[string]string)
for key, value := range maps.All(commandMap) {
summaries[key] = value.description
}
return summaries
}

View file

@ -3,10 +3,7 @@ package commands
import (
"fmt"
"maps"
"os"
"slices"
"strings"
"text/tabwriter"
)
func HelpFunc(summaries map[string]string) CommandFunc {
@ -19,21 +16,13 @@ func HelpFunc(summaries map[string]string) CommandFunc {
slices.Sort(keys)
var builder strings.Builder
builder.WriteString("\nCommands:\n")
tableWriter := tabwriter.NewWriter(&builder, 0, 8, 0, '\t', 0)
fmt.Printf("\nCommands:\n")
for _, key := range slices.All(keys) {
fmt.Fprintf(tableWriter, "\n%s\t%s", key, summaries[key])
fmt.Printf("\n%s: %s", key, summaries[key])
}
tableWriter.Flush()
builder.WriteString("\n\n")
fmt.Fprint(os.Stdout, builder.String())
fmt.Printf("\n\n")
return nil
}

View file

@ -56,7 +56,7 @@ func Lint() error {
// To rebuild packages that are already up-to-date set POKECLI_BUILD_REBUILD_ALL=1
// To enable verbose mode set POKECLI_BUILD_VERBOSE=1
func Build() error {
path := "./cmd/" + app
main := "main.go"
flags := ldflags()
build := sh.RunCmd("go", "build")
args := []string{"-ldflags=" + flags, "-o", binary}
@ -69,7 +69,7 @@ func Build() error {
args = append(args, "-v")
}
args = append(args, path)
args = append(args, main)
return build(args...)
}

View file

@ -3,6 +3,7 @@ package main
import (
"bufio"
"fmt"
"maps"
"os"
"strings"
"time"
@ -17,7 +18,11 @@ type command struct {
callback commands.CommandFunc
}
func repl() {
func main() {
run()
}
func run() {
var (
cacheCleanupInterval = 30 * time.Minute
httpTimeout = 10 * time.Second
@ -75,46 +80,52 @@ func repl() {
callback: commands.HelpFunc(summaries),
}
fmt.Printf("\nWelcome to the Pokedex!\n")
fmt.Print("\npokedex > ")
scanner := bufio.NewScanner(os.Stdin)
loopFunc := func() {
defer printPrompt()
for scanner.Scan() {
input := scanner.Text()
command, args := parseInput(input)
command, args := parseArgs(input)
cmd, ok := commandMap[command]
if !ok {
fmt.Println("ERROR: Unrecognised command.")
return
fmt.Print("\npokedex > ")
continue
}
if cmd.callback == nil {
fmt.Println("ERROR: This command is defined but does not have a callback function.")
return
fmt.Print("\npokedex > ")
continue
}
if err := commandMap[command].callback(args); err != nil {
fmt.Printf("ERROR: %v.\n", err)
}
return
fmt.Print("pokedex > ")
}
}
fmt.Printf("\nWelcome to the Pokemon world!\n")
printPrompt()
func summaryMap(commandMap map[string]command) map[string]string {
summaries := make(map[string]string)
for scanner.Scan() {
loopFunc()
}
for key, value := range maps.All(commandMap) {
summaries[key] = value.description
}
func parseInput(input string) (string, []string) {
input = strings.TrimSpace(input)
input = strings.ToLower(input)
return summaries
}
func parseArgs(input string) (string, []string) {
split := strings.Split(input, " ")
if len(split) == 0 {
@ -127,7 +138,3 @@ func parseInput(input string) (string, []string) {
return split[0], split[1:]
}
func printPrompt() {
fmt.Print("pokecli > ")
}