74 lines
1.1 KiB
Go
74 lines
1.1 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"os"
|
||
|
"time"
|
||
|
|
||
|
"github.com/cirocosta/asciinema-edit/cast"
|
||
|
)
|
||
|
|
||
|
var (
|
||
|
playerCursor time.Duration
|
||
|
interrupt = make(chan struct{})
|
||
|
)
|
||
|
|
||
|
func loadCast(filePath string) (*cast.Cast, error) {
|
||
|
file, err := os.Open(filePath)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
c, err := cast.Decode(file)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
return c, nil
|
||
|
}
|
||
|
|
||
|
func playCast(c *cast.Cast, at time.Duration) {
|
||
|
resetScreen()
|
||
|
disableEcho()
|
||
|
|
||
|
start := time.Now().Add(at * -1)
|
||
|
for _, ev := range c.EventStream {
|
||
|
if ev.Type == "i" {
|
||
|
continue // TODO
|
||
|
}
|
||
|
|
||
|
select {
|
||
|
case <-interrupt:
|
||
|
playerCursor = time.Since(start)
|
||
|
return
|
||
|
default:
|
||
|
}
|
||
|
|
||
|
t := time.Duration(ev.Time * float64(time.Second))
|
||
|
if time.Since(start) < t {
|
||
|
t := time.NewTimer(t - time.Since(start))
|
||
|
select {
|
||
|
case <-interrupt:
|
||
|
playerCursor = time.Since(start)
|
||
|
return
|
||
|
case <-t.C:
|
||
|
}
|
||
|
playerCursor = time.Since(start)
|
||
|
}
|
||
|
fmt.Print(ev.Data)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func playFunc(c *cast.Cast, at time.Duration) func() {
|
||
|
return func() {
|
||
|
playCast(c, at)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func interruptPlayback() {
|
||
|
select {
|
||
|
case interrupt <- struct{}{}:
|
||
|
default:
|
||
|
}
|
||
|
}
|