diff --git a/pokesay.go b/pokesay.go index 61048ea1..ee75dd38 100644 --- a/pokesay.go +++ b/pokesay.go @@ -41,6 +41,7 @@ func parseFlags() pokesay.Args { // selection/filtering name := getopt.StringLong("name", 'n', "", "choose a pokemon from a specific name") + id := getopt.StringLong("id", 'i', "", "choose a pokemon from a specific ID (see `pokesay -l` for IDs)") category := getopt.StringLong("category", 'c', "", "choose a pokemon from a specific category") // list operations @@ -87,6 +88,7 @@ func parseFlags() pokesay.Args { ListNames: *listNames, Category: *category, NameToken: *name, + IDToken: *id, JapaneseName: *japaneseName, BoxCharacters: pokesay.DetermineBoxCharacters(*unicodeBorders), DrawInfoBorder: *drawInfoBorder, @@ -155,6 +157,30 @@ func runPrintByName(args pokesay.Args) { t.PrintJson() } +// runPrintByName prints a pokemon corresponding to a specific ID +// - This reads a struct of {name -> metadata indexes} from the embedded filesystem +// - It loads the specific metadata index, loads the corresponding metadata file, and then chooses a random entry +// - Finally, it prints the pokemon +func runPrintByID(args pokesay.Args) { + t := timer.NewTimer("runPrintByName", true) + + names := pokedex.ReadStructFromBytes[map[string][]int](GOBAllNames) + + _, idx := strconv.Atoi(args.IDToken) + for i, name := range names { + if i == idx { + fmt.Println("found", i, name) + break + } + } + + json, _ := json.MarshalIndent(names, "", strings.Repeat(" ", 2)) + fmt.Fprintln(os.Stderr, string(json)) + + t.Stop() + t.PrintJson() +} + // runPrintByCategory prints a pokemon matched by a category // - This loads a GOB file containing a pokemon "category" search struct from the embedded filesystem // - It chooses a random category file from the corresponding category directory @@ -243,6 +269,8 @@ func main() { runPrintByNameAndCategory(args) } else if args.NameToken != "" { runPrintByName(args) + } else if args.IDToken != "" { + runPrintByID(args) } else if args.Category != "" { runPrintByCategory(args) } else { diff --git a/src/pokesay/print.go b/src/pokesay/print.go index 43f06030..2384d3c5 100644 --- a/src/pokesay/print.go +++ b/src/pokesay/print.go @@ -35,6 +35,7 @@ type Args struct { ListNames bool Category string NameToken string + IDToken string JapaneseName bool BoxCharacters *BoxCharacters DrawInfoBorder bool