-
Notifications
You must be signed in to change notification settings - Fork 261
/
record.go
210 lines (185 loc) · 5.74 KB
/
record.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
package main
import (
"bytes"
"fmt"
"io"
"log"
"os"
"os/exec"
"regexp"
"strings"
"time"
"github.com/charmbracelet/vhs/token"
"github.com/creack/pty"
"github.com/spf13/cobra"
"golang.org/x/term"
)
// sleepThreshold is the time at which if there has been no activity in the
// tape file we insert a Sleep command
const sleepThreshold = 500 * time.Millisecond
// EscapeSequences is a map of escape sequences to their VHS commands.
var EscapeSequences = map[string]string{
"\x1b[A": token.UP,
"\x1b[B": token.DOWN,
"\x1b[C": token.RIGHT,
"\x1b[D": token.LEFT,
"\x1b[1~": token.HOME,
"\x1b[2~": token.INSERT,
"\x1b[3~": token.DELETE,
"\x1b[4~": token.END,
"\x1b[5~": token.PAGEUP,
"\x1b[6~": token.PAGEDOWN,
"\x01": token.CTRL + "+A",
"\x02": token.CTRL + "+B",
"\x03": token.CTRL + "+C",
"\x04": token.CTRL + "+D",
"\x05": token.CTRL + "+E",
"\x06": token.CTRL + "+F",
"\x07": token.CTRL + "+G",
"\x08": token.BACKSPACE,
"\x09": token.TAB,
"\x0b": token.CTRL + "+K",
"\x0c": token.CTRL + "+L",
"\x0d": token.ENTER,
"\x0e": token.CTRL + "+N",
"\x0f": token.CTRL + "+O",
"\x10": token.CTRL + "+P",
"\x11": token.CTRL + "+Q",
"\x12": token.CTRL + "+R",
"\x13": token.CTRL + "+S",
"\x14": token.CTRL + "+T",
"\x15": token.CTRL + "+U",
"\x16": token.CTRL + "+V",
"\x17": token.CTRL + "+W",
"\x18": token.CTRL + "+X",
"\x19": token.CTRL + "+Y",
"\x1a": token.CTRL + "+Z",
"\x1b": token.ESCAPE,
"\x7f": token.BACKSPACE,
}
// Record is a command that starts a pseudo-terminal for the user to begin
// writing to, it records all the key presses on stdin and uses them to write
// Tape commands.
//
// vhs record > file.tape
func Record(_ *cobra.Command, _ []string) error {
command := exec.Command(shell)
command.Env = append(command.Env, "VHS_RECORD=true")
terminal, err := pty.Start(command)
if err != nil {
return err
}
if err := pty.InheritSize(os.Stdin, terminal); err != nil {
log.Printf("error resizing pty: %s", err)
}
prevState, err := term.MakeRaw(int(os.Stdin.Fd())) //nolint: gosec
if err != nil {
return err
}
// We'll need to display the stdin on the screen but we'll also need a copy to
// analyze later and create a tape file.
tape := &bytes.Buffer{}
in := io.MultiWriter(tape, terminal)
if shell != defaultShell {
tape.WriteString(fmt.Sprintf("%s Shell %s\n", token.SET, shell))
}
go func() {
var length int
for {
length = tape.Len()
time.Sleep(sleepThreshold)
if length == tape.Len() {
// Tape has not changed in a while, write a Sleep command.
tape.WriteString(fmt.Sprintf("\n%s\n", token.SLEEP))
}
}
}()
// Write to the buffer and PTY's stdin and stderr so that stdout is reserved
// for the output tape file.
go func() { _, _ = io.Copy(in, os.Stdin) }()
_, _ = io.Copy(os.Stderr, terminal)
// PTY cleanup and restore terminal
_ = terminal.Close()
_ = term.Restore(int(os.Stdin.Fd()), prevState) //nolint: gosec
fmt.Println(inputToTape(tape.String()))
return nil
}
var (
cursorResponse = regexp.MustCompile(`\x1b\[\d+;\d+R`)
oscResponse = regexp.MustCompile(`\x1b\]\d+;rgb:....\/....\/....(\x07|\x1b\\)`)
)
// inputToTape takes input from a PTY stdin and converts it into a tape file.
func inputToTape(input string) string {
// If the user exited the shell by typing exit don't record this in the
// command.
//
// NOTE: this is not very robust as if someone types exii<BS>t it will not work
// correctly and the exit will show up. In this case, the user should edit the
// tape file.
s := strings.TrimSuffix(strings.TrimSpace(input), "exit")
// Remove cursor / osc responses
s = cursorResponse.ReplaceAllString(s, "")
s = oscResponse.ReplaceAllString(s, "")
// Substitute escape sequences for commands
for sequence, command := range EscapeSequences {
s = strings.ReplaceAll(s, sequence, "\n"+command+"\n")
}
s = strings.ReplaceAll(s, "\n\n", "\n")
var sanitized strings.Builder
lines := strings.Split(s, "\n")
for i := 0; i < len(lines)-1; i++ {
// Group repeated commands to compress file and make it more readable.
repeat := 1
for lines[i] == lines[i+repeat] {
repeat++
if i+repeat == len(lines) {
break
}
}
i += repeat - 1
// We've encountered some non-command, assume that we need to type these
// characters.
if token.Type(lines[i]) == token.SLEEP {
sleep := sleepThreshold * time.Duration(repeat)
if sleep >= time.Minute {
sanitized.WriteString(fmt.Sprintf("%s %gs", token.Type(token.SLEEP), sleep.Seconds()))
} else {
sanitized.WriteString(fmt.Sprintf("%s %s", token.Type(token.SLEEP), sleep))
}
} else if strings.HasPrefix(lines[i], token.CTRL) {
for j := 0; j < repeat; j++ {
sanitized.WriteString("Ctrl" + strings.TrimPrefix(lines[i], token.CTRL) + "\n")
}
continue
} else if strings.HasPrefix(lines[i], token.ALT) {
for j := 0; j < repeat; j++ {
sanitized.WriteString("Alt" + strings.TrimPrefix(lines[i], token.ALT) + "\n")
}
continue
} else if strings.HasPrefix(lines[i], token.SET) {
sanitized.WriteString("Set" + strings.TrimPrefix(lines[i], token.SET))
} else if token.IsCommand(token.Type(lines[i])) {
sanitized.WriteString(fmt.Sprint(token.Type(lines[i])))
if repeat > 1 {
sanitized.WriteString(fmt.Sprint(" ", repeat))
}
} else {
if lines[i] != "" {
sanitized.WriteString(fmt.Sprintln(token.Type(token.TYPE), quote(lines[i])))
}
continue
}
sanitized.WriteRune('\n')
}
return sanitized.String()
}
// quote wraps a string in (single or double) quotes
func quote(s string) string {
if strings.ContainsRune(s, '"') && strings.ContainsRune(s, '\'') {
return fmt.Sprintf("`%s`", s)
}
if strings.ContainsRune(s, '"') {
return fmt.Sprintf(`'%s'`, s)
}
return fmt.Sprintf(`"%s"`, s)
}