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.

87 lines
1.5 KiB

1 year ago
package main
import (
"fmt"
"os"
"time"
"github.com/cirocosta/asciinema-edit/cast"
)
var (
playerCursor time.Duration
loadedCast *cast.Cast
1 year ago
interrupt = make(chan struct{})
)
func loadCast(filePath string) error {
1 year ago
file, err := os.Open(filePath)
if err != nil {
return err
1 year ago
}
c, err := cast.Decode(file)
if err != nil {
return err
1 year ago
}
loadedCast = c
return nil
1 year ago
}
// TODO maintain playing goroutine, unpause / fast forward when possible rather than reinstancing
func play(at time.Duration) {
1 year ago
resetScreen()
disableEcho()
var lastPing time.Time
1 year ago
start := time.Now().Add(at * -1)
for _, ev := range loadedCast.EventStream {
1 year ago
if ev.Type == "i" {
continue // TODO
}
select {
case <-interrupt:
playerCursor = time.Since(start)
sendCommand(&command{commandStatus, playerCursor, ""})
1 year ago
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)
sendCommand(&command{commandStatus, playerCursor, ""})
1 year ago
return
case <-t.C:
}
playerCursor = time.Since(start)
if time.Since(lastPing) >= 1*time.Second {
sendCommand(&command{commandStatus, playerCursor, ""})
lastPing = time.Now()
}
1 year ago
}
fmt.Print(ev.Data)
}
resetScreen()
fmt.Print("Playback complete.")
1 year ago
}
func playFunc(at time.Duration) func() {
1 year ago
return func() {
play(at)
1 year ago
}
}
func interruptPlayback() {
select {
case interrupt <- struct{}{}:
default:
}
}