internal/providers/substyle/substyle.go
155 lines · 3228 bytes
1package substyle
2
3import (
4 "fmt"
5 "strings"
6 "sync"
7
8 "github.com/wraient/curd/internal/curdhost"
9)
10
11var (
12 choiceMu sync.Mutex
13 chosen string
14)
15
16func Normalize(style string) string {
17 switch strings.ToLower(strings.TrimSpace(style)) {
18 case "soft":
19 return "soft"
20 case "hard":
21 return "hard"
22 default:
23 return "ask"
24 }
25}
26
27func resolvePreference(fallback string) string {
28 if curdhost.CurrentSubStyle != nil {
29 if live := Normalize(curdhost.CurrentSubStyle()); live != "ask" {
30 return live
31 }
32 }
33
34 choiceMu.Lock()
35 defer choiceMu.Unlock()
36 if chosen != "" {
37 return chosen
38 }
39 return Normalize(fallback)
40}
41
42func rememberChoice(style string) {
43 style = Normalize(style)
44 if style != "soft" && style != "hard" {
45 return
46 }
47
48 choiceMu.Lock()
49 chosen = style
50 choiceMu.Unlock()
51
52 if curdhost.PersistSubStylePreference != nil {
53 _ = curdhost.PersistSubStylePreference(style)
54 }
55}
56
57// Choose picks soft or hard subtitle delivery for sub-mode playback.
58func Choose(hasSoft, hasHard bool, preference string) (string, error) {
59 preference = resolvePreference(preference)
60
61 switch preference {
62 case "soft":
63 if hasSoft {
64 return "soft", nil
65 }
66 if hasHard {
67 return "hard", nil
68 }
69 case "hard":
70 if hasHard {
71 return "hard", nil
72 }
73 if hasSoft {
74 return promptSoftFallback()
75 }
76 case "ask":
77 if hasSoft && hasHard {
78 return promptBoth()
79 }
80 if hasSoft {
81 if curdhost.Out != nil {
82 curdhost.Out("Using soft subs with external subtitles.")
83 }
84 rememberChoice("soft")
85 return "soft", nil
86 }
87 if hasHard {
88 return "hard", nil
89 }
90 }
91
92 return "", fmt.Errorf("no sub streams found")
93}
94
95func promptBoth() (string, error) {
96 if curdhost.PromptSelect == nil {
97 rememberChoice("soft")
98 return "soft", nil
99 }
100 if curdhost.Out != nil {
101 curdhost.Out("Both soft-sub and hard-sub streams are available.")
102 }
103 selected, err := curdhost.PromptSelect([]curdhost.PromptOption{
104 {Key: "soft", Label: "Soft sub (external subtitles)"},
105 {Key: "hard", Label: "Hard sub (burned-in subtitles)"},
106 })
107 if err != nil {
108 return "", err
109 }
110 chosenStyle := "soft"
111 if selected.Key == "hard" {
112 chosenStyle = "hard"
113 }
114 rememberChoice(chosenStyle)
115 return chosenStyle, nil
116}
117
118func promptSoftFallback() (string, error) {
119 if curdhost.PromptSelect == nil {
120 rememberChoice("soft")
121 return "soft", nil
122 }
123 if curdhost.Out != nil {
124 curdhost.Out("Only soft subs with external subtitles are available.")
125 }
126 selected, err := curdhost.PromptSelect([]curdhost.PromptOption{
127 {Key: "soft", Label: "Use soft subs (external subtitles)"},
128 {Key: "cancel", Label: "Cancel"},
129 })
130 if err != nil {
131 return "", err
132 }
133 if selected.Key != "soft" {
134 return "", fmt.Errorf("no hard-sub streams found")
135 }
136 rememberChoice("soft")
137 return "soft", nil
138}
139
140// HardOnly reports whether playback should restrict to burned-in subtitles.
141func HardOnly(preference string) bool {
142 return resolvePreference(preference) == "hard"
143}
144
145// SoftOnly reports whether playback should restrict to external subtitles.
146func SoftOnly(preference string) bool {
147 return resolvePreference(preference) == "soft"
148}
149
150// ResetForTest clears the in-session subtitle style choice.
151func ResetForTest() {
152 choiceMu.Lock()
153 chosen = ""
154 choiceMu.Unlock()
155}