-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcard.go
More file actions
113 lines (93 loc) · 3.02 KB
/
card.go
File metadata and controls
113 lines (93 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package card
import (
"flag"
"fmt"
"os"
"strings"
tea "github.com/charmbracelet/bubbletea"
"github.com/digitalghost-dev/poke-cli/cmd/utils"
"github.com/digitalghost-dev/poke-cli/styling"
)
func CardCommand() (string, error) {
var output strings.Builder
flag.Usage = func() {
helpMessage := styling.HelpBorder.Render(
"View data about cards from the TCG!\n\n",
styling.StyleBold.Render("USAGE:"),
fmt.Sprintf("\n\t%s %s %s", "poke-cli", styling.StyleBold.Render("card"), "[flag]"),
"\n\n",
styling.StyleBold.Render("FLAGS:"),
fmt.Sprintf("\n\t%-30s %s", "-h, --help", "Prints out the help menu"),
)
output.WriteString(helpMessage)
}
if utils.CheckHelpFlag(&output, flag.Usage) {
return output.String(), nil
}
flag.Parse()
// Validate arguments
if err := utils.ValidateArgs(os.Args, utils.Validator{MaxArgs: 3, CmdName: "card", RequireName: false, HasFlags: false}); err != nil {
output.WriteString(err.Error())
return output.String(), err
}
seriesModel := SeriesList()
// Program 1: Series selection
finalModel, err := tea.NewProgram(seriesModel, tea.WithAltScreen()).Run()
if err != nil {
return "", fmt.Errorf("error running series selection program: %w", err)
}
result, ok := finalModel.(SeriesModel)
if !ok {
return "", fmt.Errorf("unexpected model type from series selection: got %T, want SeriesModel", finalModel)
}
if result.SeriesID != "" {
// Program 2: Sets selection
setsModel, err := SetsList(result.SeriesID)
if err != nil {
return "", fmt.Errorf("error loading sets: %w", err)
}
finalSetsModel, err := tea.NewProgram(setsModel, tea.WithAltScreen()).Run()
if err != nil {
return "", fmt.Errorf("error running sets selection program: %w", err)
}
setsResult, ok := finalSetsModel.(SetsModel)
if !ok {
return "", fmt.Errorf("unexpected model type from sets selection: got %T, want SetsModel", finalSetsModel)
}
if setsResult.Quitting {
return output.String(), nil
}
// Program 3: Cards display
if setsResult.SetID != "" {
cardsModel, err := CardsList(setsResult.SetID)
if err != nil {
return "", fmt.Errorf("error loading cards: %w", err)
}
for {
finalCardsModel, err := tea.NewProgram(cardsModel, tea.WithAltScreen()).Run()
if err != nil {
return "", fmt.Errorf("error running cards program: %w", err)
}
cardsResult, ok := finalCardsModel.(CardsModel)
if !ok {
return "", fmt.Errorf("unexpected model type from cards display: got %T, want CardsModel", finalCardsModel)
}
if cardsResult.ViewImage {
// Launch image viewer
imageURL := cardsResult.ImageMap[cardsResult.SelectedOption]
imageModel := ImageRenderer(cardsResult.SelectedOption, imageURL)
_, err := tea.NewProgram(imageModel, tea.WithAltScreen()).Run()
if err != nil {
fmt.Fprintf(os.Stderr, "Warning: image viewer error: %v\n", err)
}
// Re-launch cards with same state
cardsResult.ViewImage = false
cardsModel = cardsResult
} else {
break
}
}
}
}
return output.String(), nil
}