-
Notifications
You must be signed in to change notification settings - Fork 8
/
player.go
51 lines (45 loc) · 872 Bytes
/
player.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
package main
import (
"github.com/sugyan/ttyread"
"io"
"os"
)
// Terminal interface
type Terminal interface {
Write([]byte) (int, error)
}
// TtyPlay reads ttyrecord file and play
func (g *GifGenerator) TtyPlay(input string, term Terminal, capture func(int32) error) (err error) {
inFile, err := os.Open(input)
if err != nil {
return
}
defer inFile.Close()
reader := ttyread.NewTtyReader(inFile)
var prevTv *ttyread.TimeVal
for {
var data *ttyread.TtyData
data, err = reader.ReadData()
if err != nil {
if err == io.EOF {
break
} else {
return
}
}
var diff ttyread.TimeVal
if prevTv != nil {
diff = data.TimeVal.Subtract(*prevTv)
}
prevTv = &data.TimeVal
err = capture(diff.Sec*1000000 + diff.Usec)
if err != nil {
return
}
_, err = term.Write(*data.Buffer)
if err != nil {
return
}
}
return nil
}