Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
tmck-code committed Apr 4, 2024
1 parent 50a808c commit 4d5ad1c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
28 changes: 28 additions & 0 deletions pokesay.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -87,6 +88,7 @@ func parseFlags() pokesay.Args {
ListNames: *listNames,
Category: *category,
NameToken: *name,
IDToken: *id,
JapaneseName: *japaneseName,
BoxCharacters: pokesay.DetermineBoxCharacters(*unicodeBorders),
DrawInfoBorder: *drawInfoBorder,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions src/pokesay/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type Args struct {
ListNames bool
Category string
NameToken string
IDToken string
JapaneseName bool
BoxCharacters *BoxCharacters
DrawInfoBorder bool
Expand Down

0 comments on commit 4d5ad1c

Please sign in to comment.