Interactive asciicast editor
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
1.1 KiB

1 year ago
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:
}
}