diff --git a/build/cows.gob b/build/cows.gob index 2816df9a..757d889c 100644 Binary files a/build/cows.gob and b/build/cows.gob differ diff --git a/go.mod b/go.mod index 85433f7a..5c1f4937 100644 --- a/go.mod +++ b/go.mod @@ -19,4 +19,4 @@ require ( golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b // indirect ) -replace src/internal/timer => ./src/internal/timer +// replace src/internal/timer => ./src/internal/timer diff --git a/pokesay.go b/pokesay.go index be810014..6586851c 100644 --- a/pokesay.go +++ b/pokesay.go @@ -2,6 +2,7 @@ package main import ( "bufio" + "encoding/json" _ "embed" "encoding/binary" "flag" @@ -73,30 +74,37 @@ func printPokemon(choice pokedex.PokemonEntry) { } -func chooseRandomCategory(entries pokedex.PokemonEntryMap) []pokedex.PokemonEntry { - choice := randomInt(entries.NCategories) +func chooseRandomCategory(entries pokedex.PokemonEntryMap) []*pokedex.PokemonEntry { + categoryChoice := randomInt(entries.NCategories) + var choice []*pokedex.PokemonEntry + var defaultChoice []*pokedex.PokemonEntry idx := 0 + for category, _ := range entries.Categories { - if idx == choice { - return entries.Categories[category] + defaultChoice = entries.Categories[category] + if idx == categoryChoice { + choice = entries.Categories[category] } idx++ } - // If something goes wrong, select from the parent category "cows" - return entries.Categories["cows"] + // If something goes wrong, just return the first category by default + choice = defaultChoice + return choice } -func chooseRandomPokemon(pokemon []pokedex.PokemonEntry) pokedex.PokemonEntry { - return pokemon[randomInt(len(pokemon))] +func chooseRandomPokemon(pokemon []*pokedex.PokemonEntry) pokedex.PokemonEntry { + return *pokemon[randomInt(len(pokemon))] } -func collectPokemonWithToken(entries pokedex.PokemonEntryMap, token string) []pokedex.PokemonEntry { - found := []pokedex.PokemonEntry{} +func collectPokemonWithToken(entries pokedex.PokemonEntryMap, token string) []*pokedex.PokemonEntry { + found := []*pokedex.PokemonEntry{} - for _, entry := range entries.Categories["cows"] { - for _, t := range entry.NameTokens { - if t == token { - found = append(found, entry) + for _, entries := range entries.Categories { + for _, entry := range entries { + for _, t := range entry.NameTokens { + if t == token { + found = append(found, entry) + } } } } @@ -119,7 +127,7 @@ func parseFlags() Args { tabWidth := flag.Int("tabwidth", 4, "replace any tab characters with N spaces") noTabSpaces := flag.Bool("notabspaces", false, "do not replace tab characters (fastest)") fastest := flag.Bool("fastest", false, "run with the fastest possible configuration (-nowrap -notabspaces)") - category := flag.String("category", "cows", "choose a pokemon from a specific category") + category := flag.String("category", "", "choose a pokemon from a specific category") name := flag.String("name", "", "choose a pokemon from a specific name") listCategories := flag.Bool("category-list", false, "list all available categories") @@ -168,10 +176,18 @@ func main() { os.Exit(0) } - if category, ok := pokemon.Categories[args.Category]; ok { - printSpeechBubble(bufio.NewScanner(os.Stdin), args) - printPokemon(chooseRandomPokemon(category)) + categoryName := args.Category + var category []*pokedex.PokemonEntry + var ok bool + + if categoryName == "" { + category = chooseRandomCategory(pokemon) } else { - log.Fatal(fmt.Sprintf("Not a valid category: '%s'", args.Category)) + if category, ok = pokemon.Categories[categoryName]; !ok { + log.Fatal(fmt.Sprintf("Not a valid category: '%s'", args.Category)) + } } + + printSpeechBubble(bufio.NewScanner(os.Stdin), args) + printPokemon(chooseRandomPokemon(category)) } diff --git a/scripts/timer.sh b/scripts/timer.sh index 3dbe47db..59e0fb6d 100755 --- a/scripts/timer.sh +++ b/scripts/timer.sh @@ -2,4 +2,4 @@ # Run pokesay and get the timings only -fortune | "${1:-./pokesay}" 2>&1 > /dev/null | jq . +echo w | "${1:-./pokesay}" 2>&1 > /dev/null | jq . diff --git a/src/pokedex.go b/src/pokedex.go index 01ad2147..eb936729 100644 --- a/src/pokedex.go +++ b/src/pokedex.go @@ -19,7 +19,7 @@ func check(e error) { } func findFiles(dirpath string, ext string, skip []string) pokedex.PokemonEntryMap { - categories := &pokedex.PokemonEntryMap{Categories: make(map[string][]pokedex.PokemonEntry)} + categories := &pokedex.PokemonEntryMap{Categories: make(map[string][]*pokedex.PokemonEntry)} err := filepath.Walk(dirpath, func(fpath string, f os.FileInfo, err error) error { for _, s := range skip { if strings.Contains(fpath, s) { @@ -29,21 +29,22 @@ func findFiles(dirpath string, ext string, skip []string) pokedex.PokemonEntryMa if !f.IsDir() && filepath.Ext(f.Name()) == ext { data, err := os.ReadFile(fpath) check(err) + pokemonCategories := createCategories(fpath) + p := pokedex.NewPokemonEntry( + data, + createName(fpath), + tokenizeName(fpath), + createCategories(fpath), + ) for _, c := range pokemonCategories { - p := pokedex.NewPokemonEntry( - data, - createName(fpath), - tokenizeName(fpath), - createCategories(fpath), - ) if val, ok := categories.Categories[c]; ok { - val = append(val, *p) + val = append(val, p) } else { - categories.Categories[c] = []pokedex.PokemonEntry{*p} + categories.Categories[c] = []*pokedex.PokemonEntry{p} } - categories.Categories[c] = append(categories.Categories[c], *p) + categories.Categories[c] = append(categories.Categories[c], p) } } return err @@ -66,7 +67,7 @@ func tokenizeName(fpath string) []string { func createCategories(fpath string) []string { parts := strings.Split(fpath, "/") - return parts[2 : len(parts)-1] + return parts[3 : len(parts)-1] } type CowBuildArgs struct { diff --git a/src/pokedex/entries.go b/src/pokedex/entries.go index c94ba7b6..cf005037 100644 --- a/src/pokedex/entries.go +++ b/src/pokedex/entries.go @@ -23,7 +23,7 @@ type PokemonEntry struct { } type PokemonEntryMap struct { - Categories map[string][]PokemonEntry + Categories map[string][]*PokemonEntry NCategories int }