-
Notifications
You must be signed in to change notification settings - Fork 61
/
interactive.go
268 lines (227 loc) · 7.21 KB
/
interactive.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
package main
import (
"errors"
"fmt"
"regexp"
"strconv"
"strings"
"github.com/alecthomas/chroma/v2/styles"
"github.com/charmbracelet/huh"
"github.com/charmbracelet/lipgloss"
)
var green = lipgloss.Color("#03BF87")
func runForm(config *Config) (*Config, error) {
var (
padding = strings.Trim(fmt.Sprintf("%v", config.Padding), "[]")
margin = strings.Trim(fmt.Sprintf("%v", config.Margin), "[]")
fontSize = fmt.Sprintf("%d", int(config.Font.Size))
lineHeight = fmt.Sprintf("%.1f", config.LineHeight)
borderRadius = fmt.Sprintf("%.0f", config.Border.Radius)
borderWidth = fmt.Sprintf("%.0f", config.Border.Width)
shadowBlur = fmt.Sprintf("%.0f", config.Shadow.Blur)
shadowX = fmt.Sprintf("%.0f", config.Shadow.X)
shadowY = fmt.Sprintf("%.0f", config.Shadow.Y)
)
theme := huh.ThemeCharm()
theme.FieldSeparator = lipgloss.NewStyle()
theme.Blurred.TextInput.Text = theme.Blurred.TextInput.Text.Foreground(lipgloss.Color("243"))
theme.Blurred.BlurredButton = lipgloss.NewStyle().Foreground(lipgloss.Color("8")).PaddingRight(1)
theme.Blurred.FocusedButton = lipgloss.NewStyle().Foreground(lipgloss.Color("7")).PaddingRight(1)
theme.Focused.BlurredButton = lipgloss.NewStyle().Foreground(lipgloss.Color("8")).PaddingRight(1)
theme.Focused.FocusedButton = lipgloss.NewStyle().Foreground(lipgloss.Color("15")).PaddingRight(1)
theme.Focused.NoteTitle = theme.Focused.NoteTitle.Margin(1, 0)
theme.Blurred.NoteTitle = theme.Blurred.NoteTitle.Margin(1, 0)
theme.Blurred.Description = theme.Blurred.Description.Foreground(lipgloss.Color("0"))
theme.Focused.Description = theme.Focused.Description.Foreground(lipgloss.Color("7"))
theme.Blurred.Title = theme.Blurred.Title.Width(18).Foreground(lipgloss.Color("7"))
theme.Focused.Title = theme.Focused.Title.Width(18).Foreground(green).Bold(true)
theme.Blurred.SelectedOption = theme.Blurred.SelectedOption.Foreground(lipgloss.Color("243"))
theme.Focused.SelectedOption = lipgloss.NewStyle().Foreground(green)
theme.Focused.Base.BorderForeground(green)
f := huh.NewForm(
huh.NewGroup(
huh.NewNote().Title("\nCapture file"),
huh.NewFilePicker().
Title("").
Picking(true).
Height(10).
Value(&config.Input),
huh.NewNote().Description("Choose a code file to screenshot."),
).WithHide(config.Input != "" || config.Execute != ""),
huh.NewGroup(
huh.NewNote().Title("Settings"),
huh.NewInput().
Title("Output").
Placeholder(defaultOutputFilename).
// Description("Output location for image.").
Inline(true).
Prompt("").
Value(&config.Output),
huh.NewSelect[string]().Title("Theme ").
// Description("Theme for syntax highlighting.").
Inline(true).
Options(huh.NewOptions(styles.Names()...)...).
Value(&config.Theme),
// huh.NewInput().Title("Background ").
// // Description("Apply a background fill.").
// Placeholder("#FFF").
// Value(&config.Background).
// Inline(true).
// Prompt("").
// Validate(validateColor),
huh.NewNote().Title("Window"),
huh.NewInput().Title("Padding ").
// Description("Apply padding to the code.").
Placeholder("20 40").
Inline(true).
Value(&padding).
Prompt("").
Validate(validatePadding),
huh.NewInput().Title("Margin ").
// Description("Apply margin to the window.").
Placeholder("20").
Inline(true).
Value(&margin).
Prompt("").
Validate(validatePadding),
huh.NewConfirm().Title("Controls").
Inline(true).
Value(&config.Window),
huh.NewNote().Title("Font"),
huh.NewInput().Title("Font Family ").
// Description("Font family to use for code").
Placeholder("JetBrains Mono").
Inline(true).
Prompt("").
Value(&config.Font.Family),
huh.NewInput().Title("Font Size ").
// Description("Font size to use for code.").
Placeholder("14").
Inline(true).
Prompt("").
Value(&fontSize).
Validate(validateInteger),
huh.NewInput().Title("Line Height ").
// Description("Line height relative to size.").
Placeholder("1.2").
Inline(true).
Prompt("").
Value(&lineHeight).
Validate(validateFloat),
huh.NewNote().Title("Border"),
huh.NewInput().Title("Border Radius ").
// Description("Corner radius of the window.").
Placeholder("0").
Inline(true).
Prompt("").
Value(&borderRadius).
Validate(validateInteger),
huh.NewInput().Title("Border Width ").
// Description("Border width thickness.").
Placeholder("1").
Inline(true).
Prompt("").
Value(&borderWidth).
Validate(validateInteger),
huh.NewInput().Title("Border Color ").
// Description("Color of outline stroke.").
Validate(validateColor).
Inline(true).
Prompt("").
Value(&config.Border.Color).
Placeholder("#515151"),
huh.NewNote().Title("Shadow"),
huh.NewInput().Title("Blur ").
// Description("Shadow Gaussian Blur.").
Placeholder("0").
Inline(true).
Prompt("").
Value(&shadowBlur).
Validate(validateInteger),
huh.NewInput().Title("X Offset ").
// Description("Shadow offset x coordinate").
Placeholder("0").
Inline(true).
Prompt("").
Value(&shadowX).
Validate(validateInteger),
huh.NewInput().Title("Y Offset ").
// Description("Shadow offset y coordinate").
Placeholder("0").
Inline(true).
Prompt("").
Value(&shadowY).
Validate(validateInteger),
).WithHeight(33),
).WithTheme(theme).WithWidth(40)
err := f.Run()
if config.Output == "" {
config.Output = defaultOutputFilename
}
config.Padding = parsePadding(padding)
config.Margin = parseMargin(margin)
config.Font.Size, _ = strconv.ParseFloat(fontSize, 64)
config.LineHeight, _ = strconv.ParseFloat(lineHeight, 64)
config.Border.Radius, _ = strconv.ParseFloat(borderRadius, 64)
config.Border.Width, _ = strconv.ParseFloat(borderWidth, 64)
config.Shadow.Blur, _ = strconv.ParseFloat(shadowBlur, 64)
config.Shadow.X, _ = strconv.ParseFloat(shadowX, 64)
config.Shadow.Y, _ = strconv.ParseFloat(shadowY, 64)
return config, err //nolint: wrapcheck
}
func validateMargin(s string) error {
tokens := strings.Fields(s)
if len(tokens) > 4 {
return errors.New("maximum four values")
}
for _, t := range tokens {
_, err := strconv.Atoi(t)
if err != nil {
return errors.New("must be valid space-separated integers")
}
}
return nil
}
func validatePadding(s string) error {
return validateMargin(s)
}
func validateInteger(s string) error {
if len(s) <= 0 {
return nil
}
_, err := strconv.Atoi(s)
if err != nil {
return errors.New("must be valid integer")
}
return nil
}
func validateFloat(s string) error {
if len(s) <= 0 {
return nil
}
_, err := strconv.ParseFloat(s, 64)
if err != nil {
return errors.New("must be valid float")
}
return nil
}
var colorRegex = regexp.MustCompile("^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$")
func validateColor(s string) error {
if len(s) <= 0 {
return nil
}
if !colorRegex.MatchString(s) {
return errors.New("must be valid color")
}
return nil
}
func parsePadding(v string) []float64 {
//nolint: prealloc
var values []float64
for _, p := range strings.Fields(v) {
pi, _ := strconv.ParseFloat(p, 64) // already validated
values = append(values, pi)
}
return expandPadding(values, 1)
}
var parseMargin = parsePadding