|
| 1 | +package move |
| 2 | + |
| 3 | +import ( |
| 4 | + "flag" |
| 5 | + "fmt" |
| 6 | + "github.com/charmbracelet/lipgloss" |
| 7 | + "github.com/digitalghost-dev/poke-cli/cmd" |
| 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 | + "strconv" |
| 15 | + "strings" |
| 16 | +) |
| 17 | + |
| 18 | +func MoveCommand() { |
| 19 | + flag.Usage = func() { |
| 20 | + helpMessage := styling.HelpBorder.Render( |
| 21 | + "Get details about a specific move.\n\n", |
| 22 | + styling.StyleBold.Render("USAGE:"), |
| 23 | + fmt.Sprintf("\n\t%s %s %s %s", "poke-cli", styling.StyleBold.Render("move"), "<move-name>", "[flag]"), |
| 24 | + fmt.Sprintf("\n\t%-20s", styling.StyleItalic.Render("Use a hyphen when typing a name with a space.")), |
| 25 | + "\n\n", |
| 26 | + styling.StyleBold.Render("FLAGS:"), |
| 27 | + fmt.Sprintf("\n\t%-20s %s", "-h, --help", "Prints the help menu."), |
| 28 | + ) |
| 29 | + fmt.Println(helpMessage) |
| 30 | + } |
| 31 | + |
| 32 | + flag.Parse() |
| 33 | + |
| 34 | + if err := cmd.ValidateMoveArgs(os.Args); err != nil { |
| 35 | + fmt.Println(err.Error()) |
| 36 | + os.Exit(1) |
| 37 | + } |
| 38 | + |
| 39 | + args := flag.Args() |
| 40 | + |
| 41 | + endpoint := strings.ToLower(args[0]) |
| 42 | + moveName := strings.ToLower(args[1]) |
| 43 | + |
| 44 | + moveStruct, moveName, err := connections.MoveApiCall(endpoint, moveName, connections.APIURL) |
| 45 | + if err != nil { |
| 46 | + fmt.Println(err) |
| 47 | + if os.Getenv("GO_TESTING") != "1" { |
| 48 | + os.Exit(1) |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + moveInfoContainer(moveStruct, moveName) |
| 53 | + moveEffectContainer(moveStruct) |
| 54 | +} |
| 55 | + |
| 56 | +func moveInfoContainer(moveStruct structs.MoveJSONStruct, moveName string) { |
| 57 | + capitalizedMove := cases.Title(language.English).String(strings.ReplaceAll(moveName, "-", " ")) |
| 58 | + |
| 59 | + docStyle := lipgloss.NewStyle(). |
| 60 | + Padding(1, 2). |
| 61 | + BorderStyle(lipgloss.ThickBorder()). |
| 62 | + BorderForeground(lipgloss.Color(styling.GetTypeColor(moveStruct.Type.Name))). |
| 63 | + Width(32) |
| 64 | + |
| 65 | + headerStyle := lipgloss.NewStyle(). |
| 66 | + Bold(true). |
| 67 | + Foreground(lipgloss.Color(styling.GetTypeColor(moveStruct.Type.Name))). |
| 68 | + PaddingBottom(1) |
| 69 | + |
| 70 | + labelStyle := lipgloss.NewStyle().Bold(true).Width(15) |
| 71 | + valueStyle := lipgloss.NewStyle().Faint(true) |
| 72 | + |
| 73 | + header := headerStyle.Render(capitalizedMove) |
| 74 | + |
| 75 | + infoRows := []string{ |
| 76 | + lipgloss.JoinHorizontal(lipgloss.Top, labelStyle.Render("Type"), "|", valueStyle.Render(cases.Title(language.English).String(moveStruct.Type.Name))), |
| 77 | + lipgloss.JoinHorizontal(lipgloss.Top, labelStyle.Render("Power"), "|", valueStyle.Render(strconv.Itoa(moveStruct.Power))), |
| 78 | + lipgloss.JoinHorizontal(lipgloss.Top, labelStyle.Render("PP"), "|", valueStyle.Render(strconv.Itoa(moveStruct.PowerPoints))), |
| 79 | + lipgloss.JoinHorizontal(lipgloss.Top, labelStyle.Render("Accuracy"), "|", valueStyle.Render(strconv.Itoa(moveStruct.Accuracy))), |
| 80 | + lipgloss.JoinHorizontal(lipgloss.Top, labelStyle.Render("Category"), "|", valueStyle.Render(cases.Title(language.English).String(moveStruct.DamageClass.Name))), |
| 81 | + lipgloss.JoinHorizontal(lipgloss.Top, labelStyle.Render("Effect Chance"), "|", valueStyle.Render(fmt.Sprintf("%d%%", moveStruct.EffectChance))), |
| 82 | + lipgloss.JoinHorizontal(lipgloss.Top, labelStyle.Render("Priority"), "|", valueStyle.Render(strconv.Itoa(moveStruct.Priority))), |
| 83 | + } |
| 84 | + |
| 85 | + infoBlock := lipgloss.JoinVertical(lipgloss.Left, infoRows...) |
| 86 | + |
| 87 | + fullDoc := lipgloss.JoinVertical(lipgloss.Top, header, infoBlock) |
| 88 | + |
| 89 | + fmt.Println(docStyle.Render(fullDoc)) |
| 90 | +} |
| 91 | + |
| 92 | +func moveEffectContainer(moveStruct structs.MoveJSONStruct) { |
| 93 | + docStyle := lipgloss.NewStyle(). |
| 94 | + Padding(1, 2). |
| 95 | + BorderStyle(lipgloss.ThickBorder()). |
| 96 | + BorderForeground(lipgloss.Color(styling.GetTypeColor(moveStruct.Type.Name))). |
| 97 | + Width(32) |
| 98 | + |
| 99 | + var flavorTextEntry string |
| 100 | + for _, entry := range moveStruct.FlavorTextEntries { |
| 101 | + if entry.Language.Name == "en" && entry.VersionGroup.Name == "scarlet-violet" { |
| 102 | + flavorTextEntry = entry.FlavorText |
| 103 | + break |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + effectBold := styling.StyleBold.Render("Effect:") |
| 108 | + |
| 109 | + fullDoc := lipgloss.JoinVertical(lipgloss.Top, effectBold, flavorTextEntry) |
| 110 | + |
| 111 | + fmt.Println(docStyle.Render(fullDoc)) |
| 112 | +} |
0 commit comments