forked from charmbracelet/vhs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluator.go
128 lines (112 loc) · 3.51 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
package main
import (
"context"
"errors"
"fmt"
"io"
"log"
"os"
"strings"
)
// 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(tape string, out io.Writer, opts ...EvaluatorOption) error {
l := NewLexer(tape)
p := NewParser(l)
cmds := p.Parse()
errs := p.Errors()
if len(errs) != 0 || len(cmds) == 0 {
lines := strings.Split(tape, "\n")
for _, err := range errs {
fmt.Fprint(out, LineNumber(err.Token.Line))
fmt.Fprintln(out, lines[err.Token.Line-1])
fmt.Fprint(out, strings.Repeat(" ", err.Token.Column+ErrorColumnOffset))
fmt.Fprintln(out, Underline(len(err.Token.Literal)), err.Msg)
fmt.Fprintln(out)
}
return errors.New("parse error")
}
v := New()
defer func() { _ = v.close() }()
// Run Output and Set commands as they only modify options on the VHS instance.
var offset int
for i, cmd := range cmds {
if cmd.Type == SET || cmd.Type == OUTPUT || cmd.Type == REQUIRE {
fmt.Fprintln(out, cmd.Highlight(false))
cmd.Execute(&v)
} else {
offset = i
break
}
}
video := v.Options.Video
if video.Height < 2*video.Padding || video.Width < 2*video.Padding {
v.Errors = append(v.Errors, fmt.Errorf("height and width must be greater than %d", 2*video.Padding))
}
if len(v.Errors) > 0 {
for _, err := range v.Errors {
fmt.Fprintln(out, ErrorStyle.Render(err.Error()))
}
os.Exit(1)
}
// 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 == HIDE {
for i, cmd := range cmds[offset:] {
if cmd.Type == SHOW {
offset += i
break
}
fmt.Fprintln(out, cmd.Highlight(true))
cmd.Execute(&v)
}
}
// Begin recording frames as we are now in a recording state.
ctx, cancel := context.WithCancel(context.Background())
ch := v.Record(ctx)
// Log errors from the recording process.
go func() {
for err := range ch {
log.Print(err.Error())
}
}()
for _, cmd := range cmds[offset:] {
// 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 == SET && cmd.Options != "TypingSpeed"
if isSetting || cmd.Type == REQUIRE {
fmt.Fprintln(out, cmd.Highlight(true))
continue
}
fmt.Fprintln(out, cmd.Highlight(!v.recording || cmd.Type == SHOW || cmd.Type == HIDE || isSetting))
cmd.Execute(&v)
}
// 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)
}
// Stop recording frames.
cancel()
// Read from channel to ensure recorder is done.
<-ch
v.Cleanup()
return nil
}