|
| 1 | +package item |
| 2 | + |
| 3 | +import ( |
| 4 | + "flag" |
| 5 | + "fmt" |
| 6 | + "github.com/charmbracelet/lipgloss" |
| 7 | + "github.com/digitalghost-dev/poke-cli/cmd/utils" |
| 8 | + "github.com/digitalghost-dev/poke-cli/connections" |
| 9 | + "github.com/digitalghost-dev/poke-cli/structs" |
| 10 | + "github.com/digitalghost-dev/poke-cli/styling" |
| 11 | + "golang.org/x/text/cases" |
| 12 | + "golang.org/x/text/language" |
| 13 | + "os" |
| 14 | + "strings" |
| 15 | +) |
| 16 | + |
| 17 | +func ItemCommand() (string, error) { |
| 18 | + var output strings.Builder |
| 19 | + |
| 20 | + flag.Usage = func() { |
| 21 | + helpMessage := styling.HelpBorder.Render( |
| 22 | + "Get details about a specific item.\n\n", |
| 23 | + styling.StyleBold.Render("USAGE:"), |
| 24 | + fmt.Sprintf("\n\t%s %s %s %s", "poke-cli", styling.StyleBold.Render("item"), "<item-name>", "[flag]"), |
| 25 | + fmt.Sprintf("\n\t%-30s", styling.StyleItalic.Render("Use a hyphen when typing a name with a space.")), |
| 26 | + "\n\n", |
| 27 | + styling.StyleBold.Render("FLAGS:"), |
| 28 | + fmt.Sprintf("\n\t%-30s %s", "-h, --help", "Prints the help menu."), |
| 29 | + ) |
| 30 | + output.WriteString(helpMessage) |
| 31 | + } |
| 32 | + |
| 33 | + args := os.Args |
| 34 | + |
| 35 | + flag.Parse() |
| 36 | + |
| 37 | + if len(os.Args) == 3 && (os.Args[2] == "-h" || os.Args[2] == "--help") { |
| 38 | + flag.Usage() |
| 39 | + return output.String(), nil |
| 40 | + } |
| 41 | + |
| 42 | + if err := utils.ValidateItemArgs(os.Args); err != nil { |
| 43 | + output.WriteString(err.Error()) |
| 44 | + return output.String(), err |
| 45 | + } |
| 46 | + |
| 47 | + endpoint := strings.ToLower(args[1]) |
| 48 | + itemName := strings.ToLower(args[2]) |
| 49 | + |
| 50 | + itemStruct, itemName, err := connections.ItemApiCall(endpoint, itemName, connections.APIURL) |
| 51 | + if err != nil { |
| 52 | + if os.Getenv("GO_TESTING") != "1" { |
| 53 | + fmt.Fprintln(os.Stderr, err) |
| 54 | + os.Exit(1) |
| 55 | + } |
| 56 | + return err.Error(), nil |
| 57 | + } |
| 58 | + |
| 59 | + itemInfoContainer(&output, itemStruct, itemName) |
| 60 | + |
| 61 | + return output.String(), nil |
| 62 | +} |
| 63 | + |
| 64 | +func itemInfoContainer(output *strings.Builder, itemStruct structs.ItemJSONStruct, itemName string) { |
| 65 | + capitalizedItem := styling.StyleBold.Render(cases.Title(language.English).String(strings.ReplaceAll(itemName, "-", " "))) |
| 66 | + itemCost := fmt.Sprintf("Cost: %d", itemStruct.Cost) |
| 67 | + itemCategory := "Category: " + cases.Title(language.English).String(strings.ReplaceAll(itemStruct.Category.Name, "-", " ")) |
| 68 | + |
| 69 | + docStyle := lipgloss.NewStyle(). |
| 70 | + Padding(1, 2). |
| 71 | + BorderStyle(lipgloss.ThickBorder()). |
| 72 | + BorderForeground(lipgloss.AdaptiveColor{Light: "#444", Dark: "#EEE"}). |
| 73 | + Width(32) |
| 74 | + |
| 75 | + var flavorTextEntry string |
| 76 | + var missingData string |
| 77 | + var fullDoc string |
| 78 | + |
| 79 | + if len(itemStruct.FlavorTextEntries) == 0 { |
| 80 | + missingData = styling.StyleItalic.Render("Missing data from API") |
| 81 | + fullDoc = lipgloss.JoinVertical(lipgloss.Top, capitalizedItem, itemCost, itemCategory, "---", "Description:", missingData) |
| 82 | + } else { |
| 83 | + for _, entry := range itemStruct.FlavorTextEntries { |
| 84 | + if entry.Language.Name == "en" && entry.VersionGroup.Name == "sword-shield" { |
| 85 | + if entry.Text != "" { |
| 86 | + flavorTextEntry = entry.Text |
| 87 | + fullDoc = lipgloss.JoinVertical(lipgloss.Top, capitalizedItem, itemCost, itemCategory, "---", "Description:", flavorTextEntry) |
| 88 | + break |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + output.WriteString(docStyle.Render(fullDoc)) |
| 95 | + output.WriteString("\n") |
| 96 | +} |
0 commit comments