-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathui.go
300 lines (261 loc) · 8.47 KB
/
ui.go
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package ffcss
import (
"fmt"
"os"
"regexp"
"strings"
"github.com/AlecAivazis/survey/v2"
chromaQuick "github.com/alecthomas/chroma/quick"
"github.com/charmbracelet/glamour"
"github.com/mitchellh/colorstring"
)
const indent = " "
// BulletColorsByIndentLevel maps (the "key" is the element's index in the array) an indentation level (see LogStep, for example)
// to a color.
var BulletColorsByIndentLevel = []string{
"blue",
"cyan",
"green",
"yellow",
"orange",
"red",
"magenta",
"white",
}
var colorizer colorstring.Colorize
func init() {
colorizer.Colors = colorstring.DefaultColors
colorizer.Colors["italic"] = "3"
colorizer.Colors["orange"] = "38;2;241;109;12"
colorizer.Reset = true
}
func printf(s string, args ...interface{}) {
fmt.Fprintf(out, s, args...)
}
func printfln(s string, args ...interface{}) {
printf(s+"\n", args...)
}
// DescribeTheme shows the introduction message before installation
func DescribeTheme(theme Theme, indentLevel uint) {
printf("\n")
indentation := strings.Repeat(indent, int(indentLevel))
var author string
urlParts := strings.Split(theme.DownloadAt, "/")
LogDebug("urlParts is %#v", urlParts)
if theme.Author != "" {
author = theme.Author
} else if strings.Contains(theme.DownloadAt, "github.com") && len(urlParts) == 5 {
author = urlParts[len(urlParts)-2]
}
printf(indentation)
printf(
colorizer.Color("[dim]Installing ") +
colorizer.Color("[blue][bold]"+theme.Name()),
)
if regexp.MustCompile(`^v([0-9\.]+)$`).MatchString(theme.Tag) {
printf(colorstring.Color(" [blue]" + theme.Tag))
}
if author != "" {
printf(
colorizer.Color("[dim][italic] by ") +
colorizer.Color("[blue][italic]"+author),
)
}
if theme.Description != "" {
printf("\n")
gutter := colorstring.Color(indentation + "[blue]│")
LogDebug("gutter is %q", gutter)
markdownRendered, err := glamour.Render(theme.Description, "dark")
if err != nil {
markdownRendered = theme.Description
}
printf("\n")
LogDebug("splitted is %#v", strings.Split(markdownRendered, "\n"))
for _, line := range strings.Split(markdownRendered, "\n") {
if strings.TrimSpace(line) == "" {
continue
}
printfln(gutter + strings.TrimSpace(line))
}
printf("\n")
} else {
printf("\n\n")
}
}
func showManifestSource(theme Theme) {
printf("\n")
printfln(colorizer.Color("[italic][dim]" + theme.Name() + "'s manifest"))
chromaQuick.Highlight(os.Stdout, theme.raw, "YAML", "terminal16m", "pygments")
printf("\n")
}
func plural(singular string, amount int, optionalPlural ...string) string {
var plural string
switch len(optionalPlural) {
case 1:
plural = optionalPlural[0]
case 0:
plural = singular + "s"
default:
panic("plural expected 2 or 3 arguments, you gave more")
}
if amount == 1 {
return singular
}
return plural
}
// LogDebug prints a debug log line. This one always prints to the real stdout, ignoring a possibly mocked stdout
func LogDebug(s string, fmtArgs ...interface{}) {
if os.Getenv("DEBUG") != "" {
fmt.Printf(colorizer.Color("[dim][ DEBUG ] "+s+"\n"), fmtArgs...)
}
}
// LogWarning prints a log line with "warning" styling
func LogWarning(s string, fmtArgs ...interface{}) {
printf(colorizer.Color("[yellow][bold]"+s+"\n"), fmtArgs...)
}
// LogError is like warn but with "error" styling
func LogError(s string, fmtArgs ...interface{}) {
printf(colorizer.Color("[red][bold]"+s+"\n"), fmtArgs...)
}
// LogStep displays a list item
func LogStep(indentLevel uint, item string, fmtArgs ...interface{}) {
LogStepC("•", indentLevel, item, fmtArgs...)
}
// LogStepC is like Step, but the bullet point characters is customizable
func LogStepC(bulletChar string, indentLevel uint, item string, fmtArgs ...interface{}) {
indentLevel += BaseIndentLevel
var color string
if int(indentLevel) > len(BulletColorsByIndentLevel)-1 {
color = BulletColorsByIndentLevel[len(BulletColorsByIndentLevel)-1]
} else {
color = BulletColorsByIndentLevel[indentLevel]
}
bullet := strings.Repeat(indent, int(indentLevel)) +
colorizer.Color("["+color+"]"+bulletChar)
printfln(bullet + " " + colorizer.Color(strings.TrimSpace(fmt.Sprintf(item, fmtArgs...))))
}
// Display is like String, but adds terminal ANSI sequences for some color.
func (ffp FirefoxProfile) Display() string {
return colorizer.Color(fmt.Sprintf("[bold]%s [reset][dim](%s)", ffp.Name, ffp.ID))
}
// AskProfiles prompts the user to select one or more profiles from the given array, and returns the user's chosen profiles.
func AskProfiles(profiles []FirefoxProfile) []FirefoxProfile {
var selectedProfiles []FirefoxProfile
// XXX the whole display thing should be put in survey.MultiSelect.Renderer, look into that.
selectedProfileDirsDisplay := make([]string, 0)
LogStep(0, "Please select profiles to apply the theme on")
profileDirsDisplay := make([]string, 0)
for _, profile := range profiles {
profileDirsDisplay = append(profileDirsDisplay, profile.Display())
}
survey.AskOne(&survey.MultiSelect{
Message: "Select profiles",
Options: profileDirsDisplay,
VimMode: vimModeEnabled(),
}, &selectedProfileDirsDisplay)
for _, chosenProfileDisplay := range selectedProfileDirsDisplay {
selectedProfiles = append(selectedProfiles, NewFirefoxProfileFromDisplay(chosenProfileDisplay, profiles))
}
return selectedProfiles
}
// AskToSeeManifestSource prompts the user to display the theme's manifest, and, if the user accepts, displays it.
func (t Theme) AskToSeeManifestSource(skip bool) {
wantsSource := false
if !skip {
survey.AskOne(&survey.Confirm{
Message: "Show the manifest source?",
}, &wantsSource)
}
if wantsSource {
showManifestSource(t)
}
}
// ChooseVariant asks the user to choose a variant.
// If the users interrupts the prompt (by e.g. pressing Ctrl-C), cancel is true.
// Else, the selected variant is returned and cancel is false.
// If no variants are available, the empty variant is returned and cancel is false (and the user does not get prompted).
func (t Theme) ChooseVariant() (chosen Variant, cancel bool) {
var variantName string
if len(t.AvailableVariants()) > 0 {
LogStep(0, "Please choose the theme's variant")
variantPrompt := &survey.Select{
Message: "Install variant",
Options: t.AvailableVariants(),
VimMode: vimModeEnabled(),
}
survey.AskOne(variantPrompt, &variantName)
// user Ctrl-C'd
if variantName == "" {
return Variant{}, true
}
return t.Variants[variantName], false
}
return Variant{}, false
}
// ConfirmInstallAddons asks the user to confirm the installation of addons.
func ConfirmInstallAddons(addons []string) bool {
acceptOpenExtensionPages := false
survey.AskOne(&survey.Confirm{
Message: fmt.Sprintf("This theme suggests installing %d %s. Open %s?",
len(addons),
plural("addon", len(addons)),
plural("its page", len(addons), "their pages"),
),
Default: acceptOpenExtensionPages,
}, &acceptOpenExtensionPages)
return acceptOpenExtensionPages
}
// ShowHookOutput displays the given output text with additional horizontal and vertical padding
func ShowHookOutput(output string) {
fmt.Fprint(
out,
"\n",
prefixEachLine(
strings.TrimSpace(output),
strings.Repeat(indent, int(BaseIndentLevel)+2),
),
"\n",
"\n",
)
}
// DisplayErrorMessage displays a nested error message (split on colons)
func DisplayErrorMessage(err error) {
for idx, errorFragment := range strings.Split(err.Error(), ":") {
LogStep(uint(idx), errorFragment)
}
}
// SelectProfiles returns an array of FirefoxProfile:
//
// If selected is non-empty, it parses the paths into an array of FirefoxProfile
// Else, it returns all profiles if all is true
// Else, it asks the user to select one or more profiles and returns those
func SelectProfiles(selected []string, dir string, useDefault bool, all bool) ([]FirefoxProfile, error) {
var selectedProfiles []FirefoxProfile
if len(selected) > 0 {
for _, profilePath := range selected {
selectedProfiles = append(selectedProfiles, NewFirefoxProfileFromPath(profilePath))
}
} else {
LogStep(0, "Getting profiles")
profiles, err := Profiles(dir)
if err != nil {
return []FirefoxProfile{}, fmt.Errorf("couldn't get profile directories: %w", err)
}
if useDefault {
for _, profile := range profiles {
if profile.Name == "default-release" {
return []FirefoxProfile{profile}, nil
}
}
}
// Choose profiles
// TODO smart default (based on {{profileDirectory}}/times.json:firstUse)
if all {
LogStep(0, "Selecting all profiles")
selectedProfiles = profiles
} else {
selectedProfiles = AskProfiles(profiles)
}
}
return selectedProfiles, nil
}