internal/selection_labels.go
199 lines · 5178 bytes
1package internal
2
3import (
4 "fmt"
5 "regexp"
6 "strconv"
7 "strings"
8
9 "github.com/wraient/curd/internal/providers/animepahe"
10)
11
12var selectionEpisodeCountRE = regexp.MustCompile(`\((\d+)\s+episodes?\)`)
13
14func FormatAnimeSearchLabel(title string, episodes int) string {
15 title = strings.TrimSpace(title)
16 if title == "" {
17 return ""
18 }
19 if episodes > 0 {
20 return fmt.Sprintf("%s (%d episodes)", title, episodes)
21 }
22 return title
23}
24
25func TrackerEpisodeHint(config *CurdConfig, entry *Entry) string {
26 if entry == nil || entry.Media.Episodes <= 0 {
27 return ""
28 }
29 tracker := RemoteTrackingDisplayName(config)
30 title := mediaDisplayTitle(entry.Media, config)
31 if title == "" {
32 return fmt.Sprintf("Your %s entry has %d episodes — pick the matching title below.", tracker, entry.Media.Episodes)
33 }
34 return fmt.Sprintf("Your %s entry for %q has %d episodes — pick the matching title below.", tracker, title, entry.Media.Episodes)
35}
36
37func episodeCountFromSelectionOption(option SelectionOption) (int, bool) {
38 for _, source := range []string{option.Label, option.Title} {
39 if source == "" {
40 continue
41 }
42 if matches := selectionEpisodeCountRE.FindStringSubmatch(source); len(matches) > 1 {
43 if count, err := strconv.Atoi(matches[1]); err == nil && count > 0 {
44 return count, true
45 }
46 }
47 }
48
49 if option.ExtraData != nil {
50 if item, ok := option.ExtraData.(animepahe.SearchItem); ok && item.Episodes > 0 {
51 return item.Episodes, true
52 }
53 }
54
55 return 0, false
56}
57
58func selectionOptionBaseTitle(option SelectionOption) string {
59 if option.Title != "" {
60 return strings.TrimSpace(option.Title)
61 }
62
63 label := strings.TrimSpace(option.Label)
64 if label == "" {
65 return ""
66 }
67 if idx := strings.Index(label, " ("); idx > 0 {
68 return strings.TrimSpace(label[:idx])
69 }
70 if idx := strings.Index(label, " — "); idx > 0 {
71 return strings.TrimSpace(label[:idx])
72 }
73 if idx := strings.Index(label, " ["); idx > 0 {
74 return strings.TrimSpace(label[:idx])
75 }
76 return label
77}
78
79func ensureEpisodeCountInLabel(option SelectionOption) SelectionOption {
80 if selectionEpisodeCountRE.MatchString(option.Label) {
81 return option
82 }
83
84 count, ok := episodeCountFromSelectionOption(option)
85 if !ok || count <= 0 {
86 return option
87 }
88
89 baseTitle := selectionOptionBaseTitle(option)
90 if baseTitle == "" {
91 return option
92 }
93
94 providerSuffix := ""
95 if idx := strings.LastIndex(option.Label, " ["); idx > 0 && strings.HasSuffix(option.Label, "]") {
96 providerSuffix = option.Label[idx:]
97 }
98
99 option.Label = FormatAnimeSearchLabel(baseTitle, count) + providerSuffix
100 if option.Title == "" {
101 option.Title = baseTitle
102 }
103 return option
104}
105
106func ProviderSearchOptionsForDisplay(options []SelectionOption, entry *Entry) []SelectionOption {
107 if len(options) == 0 {
108 return options
109 }
110
111 targetEpisodes := 0
112 if entry != nil {
113 targetEpisodes = entry.Media.Episodes
114 }
115
116 display := make([]SelectionOption, len(options))
117 for i, option := range options {
118 display[i] = ensureEpisodeCountInLabel(option)
119 if targetEpisodes <= 0 {
120 continue
121 }
122 if count, ok := episodeCountFromSelectionOption(display[i]); ok && count == targetEpisodes {
123 display[i].Label = display[i].Label + " ✓"
124 }
125 }
126 return display
127}
128
129func ManualProviderSearchEnabled(config *CurdConfig) bool {
130 return config != nil && config.ManualProviderSearch
131}
132
133func displayMediaFormat(format string) string {
134 format = strings.TrimSpace(strings.ToUpper(format))
135 switch format {
136 case "TV":
137 return "TV"
138 case "TV_SHORT":
139 return "TV Short"
140 case "MOVIE":
141 return "Movie"
142 case "OVA":
143 return "OVA"
144 case "ONA":
145 return "ONA"
146 case "SPECIAL":
147 return "Special"
148 case "MUSIC":
149 return "Music"
150 default:
151 return strings.ReplaceAll(format, "_", " ")
152 }
153}
154
155func formatEpisodeCountHint(episodes int) string {
156 if episodes > 0 {
157 return fmt.Sprintf("%d episodes", episodes)
158 }
159 return "unknown episode count"
160}
161
162func ManualProviderSearchHint(config *CurdConfig, entry *Entry, query, subOrDub string) string {
163 title := strings.TrimSpace(query)
164 mediaType := ""
165 episodes := 0
166
167 if entry != nil {
168 if trackerTitle := mediaDisplayTitle(entry.Media, config); trackerTitle != "" {
169 title = trackerTitle
170 }
171 mediaType = displayMediaFormat(entry.Media.Format)
172 episodes = entry.Media.Episodes
173 }
174 if title == "" {
175 title = "unknown title"
176 }
177
178 mode := strings.ToLower(normalizeTranslationType(subOrDub))
179 parts := []string{fmt.Sprintf("Searching for %q", title)}
180 if mediaType != "" {
181 parts = append(parts, mediaType)
182 }
183 parts = append(parts, formatEpisodeCountHint(episodes))
184 if mode != "" {
185 parts = append(parts, mode)
186 }
187 return strings.Join(parts, " · ") + " — select the matching provider entry below."
188}
189
190func promptProviderSearchSelectionWithHint(config *CurdConfig, options []SelectionOption, entry *Entry, hint string) (SelectionOption, error) {
191 if strings.TrimSpace(hint) != "" {
192 CurdOut(hint)
193 }
194 return DynamicSelect(ProviderSearchOptionsForDisplay(options, entry))
195}
196
197func promptProviderSearchSelection(config *CurdConfig, options []SelectionOption, entry *Entry) (SelectionOption, error) {
198 return promptProviderSearchSelectionWithHint(config, options, entry, TrackerEpisodeHint(config, entry))
199}