-
Notifications
You must be signed in to change notification settings - Fork 261
/
evaluator.go
188 lines (165 loc) · 4.89 KB
/
evaluator.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
package main
import (
"context"
"fmt"
"io"
"log"
"os"
"github.com/charmbracelet/vhs/lexer"
"github.com/charmbracelet/vhs/parser"
"github.com/charmbracelet/vhs/token"
"github.com/go-rod/rod"
)
// EvaluatorOption is a function that can be used to modify the VHS instance.
type EvaluatorOption func(*VHS)
// Evaluate takes as input a tape string, an output writer, and an output file
// and evaluates all the commands within the tape string and produces a GIF.
func Evaluate(ctx context.Context, tape string, out io.Writer, opts ...EvaluatorOption) []error {
l := lexer.New(tape)
p := parser.New(l)
cmds := p.Parse()
errs := p.Errors()
if len(errs) != 0 || len(cmds) == 0 {
return []error{InvalidSyntaxError{errs}}
}
v := New()
for _, cmd := range cmds {
if cmd.Type == token.SET && cmd.Options == "Shell" || cmd.Type == token.ENV {
err := Execute(cmd, &v)
if err != nil {
return []error{err}
}
}
}
// Start things up
if err := v.Start(); err != nil {
return []error{err}
}
defer func() { _ = v.close() }()
// Let's wait until we can access the window.term variable.
//
// This is necessary because some SET commands modify the terminal.
err := v.Page.Wait(rod.Eval("() => window.term != undefined"))
if err != nil {
return []error{err}
}
var offset int
for i, cmd := range cmds {
if cmd.Type == token.SET || cmd.Type == token.OUTPUT || cmd.Type == token.REQUIRE {
_, _ = fmt.Fprintln(out, Highlight(cmd, false))
if cmd.Options != "Shell" {
err := Execute(cmd, &v)
if err != nil {
return []error{err}
}
}
} else {
offset = i
break
}
}
// Make sure image is big enough to fit padding, bar, and margins
video := v.Options.Video
minWidth := double(video.Style.Padding) + double(video.Style.Margin)
minHeight := double(video.Style.Padding) + double(video.Style.Margin)
if video.Style.WindowBar != "" {
minHeight += video.Style.WindowBarSize
}
if video.Style.Height < minHeight || video.Style.Width < minWidth {
v.Errors = append(
v.Errors,
fmt.Errorf(
"Dimensions must be at least %d x %d",
minWidth, minHeight,
),
)
}
if len(v.Errors) > 0 {
return v.Errors
}
// Setup the terminal session so we can start executing commands.
v.Setup()
// If the first command (after Settings and Outputs) is a Hide command, we can
// begin executing the commands before we start recording to avoid capturing
// any unwanted frames.
if cmds[offset].Type == token.HIDE {
for i, cmd := range cmds[offset:] {
if cmd.Type == token.SHOW {
offset += i
break
}
_, _ = fmt.Fprintln(out, Highlight(cmd, true))
err := Execute(cmd, &v)
if err != nil {
return []error{err}
}
}
}
// Begin recording frames as we are now in a recording state.
ctx, cancel := context.WithCancel(ctx)
ch := v.Record(ctx)
// Clean up temporary files at the end.
defer func() {
if v.Options.Video.Output.Frames != "" {
// Move the frames to the output directory.
_ = os.Rename(v.Options.Video.Input, v.Options.Video.Output.Frames)
}
_ = v.Cleanup()
}()
teardown := func() {
// Stop recording frames.
cancel()
// Read from channel to ensure recorder is done.
<-ch
}
// Log errors from the recording process.
go func() {
for err := range ch {
log.Print(err.Error())
}
}()
for _, cmd := range cmds[offset:] {
if ctx.Err() != nil {
teardown()
return []error{ctx.Err()}
}
// When changing the FontFamily, FontSize, LineHeight, Padding
// The xterm.js canvas changes dimensions and causes FFMPEG to not work
// correctly (specifically) with palettegen.
// It will be possible to change settings on the fly in the future, but
// it is currently not as it does not result in a proper render of the
// GIF as the frame sequence will change dimensions. This is fixable.
//
// We should remove if isSetting statement.
isSetting := cmd.Type == token.SET && cmd.Options != "TypingSpeed"
if isSetting {
fmt.Println(ErrorStyle.Render(fmt.Sprintf("WARN: 'Set %s %s' has been ignored. Move the directive to the top of the file.\nLearn more: https://github.com/charmbracelet/vhs#settings", cmd.Options, cmd.Args)))
}
if isSetting || cmd.Type == token.REQUIRE {
_, _ = fmt.Fprintln(out, Highlight(cmd, true))
continue
}
_, _ = fmt.Fprintln(out, Highlight(cmd, !v.recording || cmd.Type == token.SHOW || cmd.Type == token.HIDE || isSetting))
err := Execute(cmd, &v)
if err != nil {
teardown()
return []error{err}
}
}
// If running as an SSH server, the output file is a temporary file
// to use for the output.
//
// We need to set the GIF file path before it is created but after all of
// the settings and commands are executed. This is done in `serve.go`.
//
// Since the GIF creation is deferred, setting the output file here will
// achieve what we want.
for _, opt := range opts {
opt(&v)
}
teardown()
if err := v.Render(); err != nil {
return []error{err}
}
return nil
}