/
command.go
519 lines (457 loc) · 14.1 KB
/
command.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
package main
import (
"encoding/json"
"fmt"
"io"
"os"
"os/exec"
"strconv"
"strings"
"time"
"github.com/atotto/clipboard"
"github.com/charmbracelet/vhs/lexer"
"github.com/charmbracelet/vhs/parser"
"github.com/charmbracelet/vhs/token"
"github.com/go-rod/rod/lib/input"
"github.com/mattn/go-runewidth"
)
// Execute executes a command on a running instance of vhs.
func Execute(c parser.Command, v *VHS) {
if c.Type == token.SOURCE {
ExecuteSourceTape(c, v)
} else {
CommandFuncs[c.Type](c, v)
}
if v.recording && v.Options.Test.Output != "" {
v.SaveOutput()
}
}
// CommandFunc is a function that executes a command on a running
// instance of vhs.
type CommandFunc func(c parser.Command, v *VHS)
// CommandFuncs maps command types to their executable functions.
var CommandFuncs = map[parser.CommandType]CommandFunc{
token.BACKSPACE: ExecuteKey(input.Backspace),
token.DELETE: ExecuteKey(input.Delete),
token.INSERT: ExecuteKey(input.Insert),
token.DOWN: ExecuteKey(input.ArrowDown),
token.ENTER: ExecuteKey(input.Enter),
token.LEFT: ExecuteKey(input.ArrowLeft),
token.RIGHT: ExecuteKey(input.ArrowRight),
token.SPACE: ExecuteKey(input.Space),
token.UP: ExecuteKey(input.ArrowUp),
token.TAB: ExecuteKey(input.Tab),
token.ESCAPE: ExecuteKey(input.Escape),
token.PAGEUP: ExecuteKey(input.PageUp),
token.PAGEDOWN: ExecuteKey(input.PageDown),
token.HIDE: ExecuteHide,
token.REQUIRE: ExecuteRequire,
token.SHOW: ExecuteShow,
token.SET: ExecuteSet,
token.OUTPUT: ExecuteOutput,
token.SLEEP: ExecuteSleep,
token.TYPE: ExecuteType,
token.CTRL: ExecuteCtrl,
token.ALT: ExecuteAlt,
token.SHIFT: ExecuteShift,
token.ILLEGAL: ExecuteNoop,
token.SCREENSHOT: ExecuteScreenshot,
token.COPY: ExecuteCopy,
token.PASTE: ExecutePaste,
token.ENV: ExecuteEnv,
}
// ExecuteNoop is a no-op command that does nothing.
// Generally, this is used for Unknown commands when dealing with
// commands that are not recognized.
func ExecuteNoop(_ parser.Command, _ *VHS) {}
// ExecuteKey is a higher-order function that returns a CommandFunc to execute
// a key press for a given key. This is so that the logic for key pressing
// (since they are repeatable and delayable) can be re-used.
//
// i.e. ExecuteKey(input.ArrowDown) would return a CommandFunc that executes
// the ArrowDown key press.
func ExecuteKey(k input.Key) CommandFunc {
return func(c parser.Command, v *VHS) {
typingSpeed, err := time.ParseDuration(c.Options)
if err != nil {
typingSpeed = v.Options.TypingSpeed
}
repeat, err := strconv.Atoi(c.Args)
if err != nil {
repeat = 1
}
for i := 0; i < repeat; i++ {
_ = v.Page.Keyboard.Type(k)
time.Sleep(typingSpeed)
}
}
}
// ExecuteCtrl is a CommandFunc that presses the argument keys and/or modifiers
// with the ctrl key held down on the running instance of vhs.
func ExecuteCtrl(c parser.Command, v *VHS) {
// Create key combination by holding ControlLeft
action := v.Page.KeyActions().Press(input.ControlLeft)
keys := strings.Split(c.Args, " ")
for i, key := range keys {
var inputKey *input.Key
switch key {
case "Shift":
inputKey = &input.ShiftLeft
case "Alt":
inputKey = &input.AltLeft
case "Enter":
inputKey = &input.Enter
case "Space":
inputKey = &input.Space
case "Backspace":
inputKey = &input.Backspace
default:
r := rune(key[0])
if k, ok := keymap[r]; ok {
inputKey = &k
}
}
// Press or hold key in case it's valid
if inputKey != nil {
if i != len(keys)-1 {
action.Press(*inputKey)
} else {
// Other keys will remain pressed until the combination reaches the end
action.Type(*inputKey)
}
}
}
action.MustDo()
}
// ExecuteAlt is a CommandFunc that presses the argument key with the alt key
// held down on the running instance of vhs.
func ExecuteAlt(c parser.Command, v *VHS) {
_ = v.Page.Keyboard.Press(input.AltLeft)
if k, ok := token.Keywords[c.Args]; ok {
switch k {
case token.ENTER:
_ = v.Page.Keyboard.Type(input.Enter)
case token.TAB:
_ = v.Page.Keyboard.Type(input.Tab)
}
} else {
for _, r := range c.Args {
if k, ok := keymap[r]; ok {
_ = v.Page.Keyboard.Type(k)
}
}
}
_ = v.Page.Keyboard.Release(input.AltLeft)
}
// ExecuteShift is a CommandFunc that presses the argument key with the shift
// key held down on the running instance of vhs.
func ExecuteShift(c parser.Command, v *VHS) {
_ = v.Page.Keyboard.Press(input.ShiftLeft)
if k, ok := token.Keywords[c.Args]; ok {
switch k {
case token.ENTER:
_ = v.Page.Keyboard.Type(input.Enter)
case token.TAB:
_ = v.Page.Keyboard.Type(input.Tab)
}
} else {
for _, r := range c.Args {
if k, ok := keymap[r]; ok {
_ = v.Page.Keyboard.Type(k)
}
}
}
_ = v.Page.Keyboard.Release(input.ShiftLeft)
}
// ExecuteHide is a CommandFunc that starts or stops the recording of the vhs.
func ExecuteHide(_ parser.Command, v *VHS) {
v.PauseRecording()
}
// ExecuteRequire is a CommandFunc that checks if all the binaries mentioned in the
// Require command are present. If not, it exits with a non-zero error.
func ExecuteRequire(c parser.Command, v *VHS) {
_, err := exec.LookPath(c.Args)
if err != nil {
v.Errors = append(v.Errors, err)
}
}
// ExecuteShow is a CommandFunc that resumes the recording of the vhs.
func ExecuteShow(_ parser.Command, v *VHS) {
v.ResumeRecording()
}
// ExecuteSleep sleeps for the desired time specified through the argument of
// the Sleep command.
func ExecuteSleep(c parser.Command, _ *VHS) {
dur, err := time.ParseDuration(c.Args)
if err != nil {
return
}
time.Sleep(dur)
}
// ExecuteType types the argument string on the running instance of vhs.
func ExecuteType(c parser.Command, v *VHS) {
typingSpeed, err := time.ParseDuration(c.Options)
if err != nil {
typingSpeed = v.Options.TypingSpeed
}
for _, r := range c.Args {
k, ok := keymap[r]
if ok {
_ = v.Page.Keyboard.Type(k)
} else {
_ = v.Page.MustElement("textarea").Input(string(r))
v.Page.MustWaitIdle()
}
time.Sleep(typingSpeed)
}
}
// ExecuteOutput applies the output on the vhs videos.
func ExecuteOutput(c parser.Command, v *VHS) {
switch c.Options {
case ".mp4":
v.Options.Video.Output.MP4 = c.Args
case ".test", ".ascii", ".txt":
v.Options.Test.Output = c.Args
case ".png":
v.Options.Video.Output.Frames = c.Args
case ".webm":
v.Options.Video.Output.WebM = c.Args
default:
v.Options.Video.Output.GIF = c.Args
}
}
// ExecuteCopy copies text to the clipboard.
func ExecuteCopy(c parser.Command, _ *VHS) {
_ = clipboard.WriteAll(c.Args)
}
// ExecuteEnv sets env with given key-value pair.
func ExecuteEnv(c parser.Command, _ *VHS) {
_ = os.Setenv(c.Options, c.Args)
}
// ExecutePaste pastes text from the clipboard.
func ExecutePaste(_ parser.Command, v *VHS) {
clip, err := clipboard.ReadAll()
if err != nil {
return
}
for _, r := range clip {
k, ok := keymap[r]
if ok {
_ = v.Page.Keyboard.Type(k)
} else {
_ = v.Page.MustElement("textarea").Input(string(r))
v.Page.MustWaitIdle()
}
}
}
// Settings maps the Set commands to their respective functions.
var Settings = map[string]CommandFunc{
"FontFamily": ExecuteSetFontFamily,
"FontSize": ExecuteSetFontSize,
"Framerate": ExecuteSetFramerate,
"Height": ExecuteSetHeight,
"LetterSpacing": ExecuteSetLetterSpacing,
"LineHeight": ExecuteSetLineHeight,
"PlaybackSpeed": ExecuteSetPlaybackSpeed,
"Padding": ExecuteSetPadding,
"Theme": ExecuteSetTheme,
"TypingSpeed": ExecuteSetTypingSpeed,
"Width": ExecuteSetWidth,
"Shell": ExecuteSetShell,
"LoopOffset": ExecuteLoopOffset,
"MarginFill": ExecuteSetMarginFill,
"Margin": ExecuteSetMargin,
"WindowBar": ExecuteSetWindowBar,
"WindowBarSize": ExecuteSetWindowBarSize,
"BorderRadius": ExecuteSetBorderRadius,
"CursorBlink": ExecuteSetCursorBlink,
}
// ExecuteSet applies the settings on the running vhs specified by the
// option and argument pass to the command.
func ExecuteSet(c parser.Command, v *VHS) {
Settings[c.Options](c, v)
}
// ExecuteSetFontSize applies the font size on the vhs.
func ExecuteSetFontSize(c parser.Command, v *VHS) {
fontSize, _ := strconv.Atoi(c.Args)
v.Options.FontSize = fontSize
_, _ = v.Page.Eval(fmt.Sprintf("() => term.options.fontSize = %d", fontSize))
// When changing the font size only the canvas dimensions change which are
// scaled back during the render to fit the aspect ration and dimensions.
//
// We need to call term.fit to ensure that everything is resized properly.
_, _ = v.Page.Eval("term.fit")
}
// ExecuteSetFontFamily applies the font family on the vhs.
func ExecuteSetFontFamily(c parser.Command, v *VHS) {
v.Options.FontFamily = c.Args
_, _ = v.Page.Eval(fmt.Sprintf("() => term.options.fontFamily = '%s'", withSymbolsFallback(c.Args)))
}
// ExecuteSetHeight applies the height on the vhs.
func ExecuteSetHeight(c parser.Command, v *VHS) {
v.Options.Video.Style.Height, _ = strconv.Atoi(c.Args)
}
// ExecuteSetWidth applies the width on the vhs.
func ExecuteSetWidth(c parser.Command, v *VHS) {
v.Options.Video.Style.Width, _ = strconv.Atoi(c.Args)
}
// ExecuteSetShell applies the shell on the vhs.
func ExecuteSetShell(c parser.Command, v *VHS) {
if s, ok := Shells[c.Args]; ok {
v.Options.Shell = s
}
}
const (
bitSize = 64
base = 10
)
// ExecuteSetLetterSpacing applies letter spacing (also known as tracking) on
// the vhs.
func ExecuteSetLetterSpacing(c parser.Command, v *VHS) {
letterSpacing, _ := strconv.ParseFloat(c.Args, bitSize)
v.Options.LetterSpacing = letterSpacing
_, _ = v.Page.Eval(fmt.Sprintf("() => term.options.letterSpacing = %f", letterSpacing))
}
// ExecuteSetLineHeight applies the line height on the vhs.
func ExecuteSetLineHeight(c parser.Command, v *VHS) {
lineHeight, _ := strconv.ParseFloat(c.Args, bitSize)
v.Options.LineHeight = lineHeight
_, _ = v.Page.Eval(fmt.Sprintf("() => term.options.lineHeight = %f", lineHeight))
}
// ExecuteSetTheme applies the theme on the vhs.
func ExecuteSetTheme(c parser.Command, v *VHS) {
var err error
v.Options.Theme, err = getTheme(c.Args)
if err != nil {
v.Errors = append(v.Errors, err)
return
}
bts, _ := json.Marshal(v.Options.Theme)
_, _ = v.Page.Eval(fmt.Sprintf("() => term.options.theme = %s", string(bts)))
v.Options.Video.Style.BackgroundColor = v.Options.Theme.Background
v.Options.Video.Style.WindowBarColor = v.Options.Theme.Background
}
// ExecuteSetTypingSpeed applies the default typing speed on the vhs.
func ExecuteSetTypingSpeed(c parser.Command, v *VHS) {
typingSpeed, err := time.ParseDuration(c.Args)
if err != nil {
return
}
v.Options.TypingSpeed = typingSpeed
}
// ExecuteSetPadding applies the padding on the vhs.
func ExecuteSetPadding(c parser.Command, v *VHS) {
v.Options.Video.Style.Padding, _ = strconv.Atoi(c.Args)
}
// ExecuteSetFramerate applies the framerate on the vhs.
func ExecuteSetFramerate(c parser.Command, v *VHS) {
framerate, err := strconv.ParseInt(c.Args, base, 0)
if err != nil {
return
}
v.Options.Video.Framerate = int(framerate)
}
// ExecuteSetPlaybackSpeed applies the playback speed option on the vhs.
func ExecuteSetPlaybackSpeed(c parser.Command, v *VHS) {
playbackSpeed, err := strconv.ParseFloat(c.Args, bitSize)
if err != nil {
return
}
v.Options.Video.PlaybackSpeed = playbackSpeed
}
// ExecuteLoopOffset applies the loop offset option on the vhs.
func ExecuteLoopOffset(c parser.Command, v *VHS) {
loopOffset, err := strconv.ParseFloat(strings.TrimRight(c.Args, "%"), bitSize)
if err != nil {
return
}
v.Options.LoopOffset = loopOffset
}
// ExecuteSetMarginFill sets vhs margin fill
func ExecuteSetMarginFill(c parser.Command, v *VHS) {
v.Options.Video.Style.MarginFill = c.Args
}
// ExecuteSetMargin sets vhs margin size
func ExecuteSetMargin(c parser.Command, v *VHS) {
v.Options.Video.Style.Margin, _ = strconv.Atoi(c.Args)
}
// ExecuteSetWindowBar sets window bar type
func ExecuteSetWindowBar(c parser.Command, v *VHS) {
v.Options.Video.Style.WindowBar = c.Args
}
// ExecuteSetWindowBar sets window bar size
func ExecuteSetWindowBarSize(c parser.Command, v *VHS) {
v.Options.Video.Style.WindowBarSize, _ = strconv.Atoi(c.Args)
}
// ExecuteSetBorderRadius sets corner radius
func ExecuteSetBorderRadius(c parser.Command, v *VHS) {
v.Options.Video.Style.BorderRadius, _ = strconv.Atoi(c.Args)
}
// ExecuteSetCursorBlink sets cursor blinking
func ExecuteSetCursorBlink(c parser.Command, v *VHS) {
var err error
v.Options.CursorBlink, err = strconv.ParseBool(c.Args)
if err != nil {
return
}
}
const sourceDisplayMaxLength = 10
// ExecuteSourceTape is a CommandFunc that executes all commands of source tape.
func ExecuteSourceTape(c parser.Command, v *VHS) {
tapePath := c.Args
var out io.Writer = os.Stdout
if quietFlag {
out = io.Discard
}
// read tape file
tape, err := os.ReadFile(tapePath)
if err != nil {
fmt.Println(err)
return
}
l := lexer.New(string(tape))
p := parser.New(l)
cmds := p.Parse()
errs := []error{}
for _, parsedErr := range p.Errors() {
errs = append(errs, parsedErr)
}
if len(errs) != 0 {
fmt.Fprintln(out, ErrorStyle.Render(fmt.Sprintf("tape %s has errors", tapePath)))
printErrors(out, tapePath, errs)
return
}
displayPath := runewidth.Truncate(strings.TrimSuffix(tapePath, extension), sourceDisplayMaxLength, "…")
// Run all commands from the sourced tape file.
for _, cmd := range cmds {
// Output have to be avoid in order to not overwrite output of the original tape.
if cmd.Type == token.SOURCE ||
cmd.Type == token.OUTPUT {
continue
}
fmt.Fprintf(out, "%s %s\n", GrayStyle.Render(displayPath+":"), Highlight(cmd, false))
CommandFuncs[cmd.Type](cmd, v)
}
}
// ExecuteScreenshot is a CommandFunc that indicates a new screenshot must be taken.
func ExecuteScreenshot(c parser.Command, v *VHS) {
v.ScreenshotNextFrame(c.Args)
}
func getTheme(s string) (Theme, error) {
if strings.TrimSpace(s) == "" {
return DefaultTheme, nil
}
switch s[0] {
case '{':
return getJSONTheme(s)
default:
return findTheme(s)
}
}
func getJSONTheme(s string) (Theme, error) {
var t Theme
if err := json.Unmarshal([]byte(s), &t); err != nil {
return DefaultTheme, fmt.Errorf("invalid `Set Theme %q: %w`", s, err)
}
return t, nil
}