This repository has been archived by the owner on Jan 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
260 lines (225 loc) · 5.11 KB
/
main.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
package main
import (
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"os/signal"
"strconv"
"strings"
"time"
)
const CtrlPrefix = "#$"
var (
Usage = "usage: " + os.Args[0] + " <script>"
ErrUnknownCtrl = errors.New("unknown control command")
ErrNoArgs = errors.New("no arguments given to command")
ErrBadArg = errors.New("invalid command argument")
)
func main() {
if len(os.Args) < 2 || os.Args[1] == "-h" || os.Args[1] == "--help" {
log.Fatal(Usage)
}
if exec.Command("asciinema", "-h").Run() != nil {
log.Fatal("can't find asciinema executable")
}
s, err := NewScript(os.Args[1], os.Args[2:])
if err != nil {
log.Fatal("parsing script failed: ", err)
}
if err := s.Start(); err != nil {
log.Fatal("couldn't start recording: ", err)
}
defer func() {
if err := s.Stop(); err != nil {
log.Fatal("couldn't stop recording: ", err)
}
}()
s.Execute()
}
// Command is an action to be run.
type Command interface {
Run(*Script)
}
// Shell is a shell command to execute.
type Shell struct {
Cmd string
}
// NewShell creates a new Shell.
func NewShell(cmd string) Shell {
if !strings.HasSuffix(cmd, "\n") {
cmd += "\n"
}
return Shell{Cmd: cmd}
}
// Run runs the shell command.
func (s Shell) Run(sc *Script) {
for _, c := range s.Cmd {
if _, err := sc.Stdin.Write([]byte(fmt.Sprintf("%s", string(c)))); err != nil {
os.Exit(1)
}
time.Sleep(sc.Delay)
}
}
// Wait is a command to change the interval between commands.
type Wait struct {
Duration time.Duration
}
// NewWait creates a new Wait.
func NewWait(opts []string) (Wait, error) {
if len(opts) == 0 {
return Wait{}, ErrNoArgs
}
ms, err := strconv.ParseInt(strings.TrimSpace(opts[0]), 10, 64)
if err != nil {
return Wait{}, ErrBadArg
}
return Wait{Duration: time.Millisecond * time.Duration(ms)}, nil
}
// Run changes the wait for subsequent commands.
func (w Wait) Run(s *Script) {
s.Wait = w.Duration
}
// Delay is a command to change the typing speed of subsequent commands.
type Delay struct {
Interval time.Duration
}
// NewDelay creates a new Delay.
func NewDelay(opts []string) (Delay, error) {
if len(opts) == 0 {
return Delay{}, ErrNoArgs
}
ms, err := strconv.ParseInt(strings.TrimSpace(opts[0]), 10, 64)
if err != nil {
return Delay{}, ErrBadArg
}
return Delay{Interval: time.Millisecond * time.Duration(ms)}, nil
}
// Run changes the typing speed for subsequent commands.
func (s Delay) Run(sc *Script) {
sc.Delay = s.Interval
}
// NewCtrl creates a new control command.
func NewCtrl(cmd string) (Command, error) {
tokens := strings.Split(cmd, " ")
switch strings.TrimSpace(tokens[0]) {
case "delay":
return NewDelay(tokens[1:])
case "wait":
return NewWait(tokens[1:])
default:
return nil, ErrUnknownCtrl
}
}
// Script is a shell script to be run and recorded by asciinema.
type Script struct {
Args []string
Commands []Command
Delay time.Duration
Wait time.Duration
Cmd *exec.Cmd
Stdin io.WriteCloser
Stdout io.ReadCloser
Stderr io.ReadCloser
}
// NewScript parses a new Script from the script file at path.
func NewScript(path string, args []string) (*Script, error) {
b, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
s := &Script{
Args: args,
Delay: time.Millisecond * 40,
Wait: time.Millisecond * 100,
}
lines := strings.Split(string(b), "\n")
for i, line := range lines {
if line == "" {
continue
}
if strings.HasPrefix(line, CtrlPrefix) {
ctrl, err := NewCtrl(strings.TrimSpace(line[len(CtrlPrefix):]))
if err != nil {
return nil, fmt.Errorf("%v (line %d)", err, i+1)
}
s.Commands = append(s.Commands, ctrl)
} else {
s.Commands = append(s.Commands, NewShell(line))
}
}
return s, nil
}
// Start starts recording.
func (s *Script) Start() error {
args := append([]string{"rec"}, s.Args...)
s.Cmd = exec.Command("asciinema", args...)
var err error
if s.Stdin, err = s.Cmd.StdinPipe(); err != nil {
return err
}
if s.Stdout, err = s.Cmd.StdoutPipe(); err != nil {
return err
}
if s.Stderr, err = s.Cmd.StderrPipe(); err != nil {
return err
}
if err = s.Cmd.Start(); err != nil {
return err
}
go echo(s.Stdout)
go echo(s.Stderr)
return nil
}
// Stop stops recording.
func (s *Script) Stop() error {
defer s.Cmd.Wait()
if _, err := s.Stdin.Write([]byte("exit\n")); err != nil {
return err
}
if len(s.Args) == 0 || strings.HasPrefix(s.Args[0], "-") {
s.endDialog()
}
return nil
}
func (s *Script) endDialog() {
handler := make(chan os.Signal, 1)
sig := make(chan os.Signal, 1)
stdin := make(chan bool, 1)
signal.Notify(handler, os.Interrupt)
go func() {
sig <- <-handler
signal.Stop(handler)
}()
go func() {
fmt.Scanln()
stdin <- true
}()
select {
case int := <-sig:
s.Cmd.Process.Signal(int)
case <-stdin:
s.Stdin.Write([]byte{'\n'})
}
}
// Execute runs the script's commands.
func (s *Script) Execute() {
for _, c := range s.Commands {
c.Run(s)
time.Sleep(s.Wait)
}
}
// echo prints out output continously.
func echo(r io.Reader) {
buf := make([]byte, 1024)
for {
n, err := r.Read(buf)
if err != nil {
return
}
fmt.Print(string(buf[:n]))
}
}