Skip to content

Commit

Permalink
fix: quick scrolling through lots of previews
Browse files Browse the repository at this point in the history
  • Loading branch information
cfoust committed Nov 8, 2024
1 parent 602f5ea commit 65d9221
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
18 changes: 16 additions & 2 deletions pkg/replay/loader/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,16 @@ func (l *Loader) Init() tea.Cmd {
}
}

ctx := l.Lifetime.Ctx()

events := make([]sessions.Event, 0)
for {
if ctx.Err() != nil {
return loadedEvent{
err: ctx.Err(),
}
}

event, err := reader.Read()
if err == io.EOF || err == io.ErrUnexpectedEOF {
break
Expand All @@ -65,10 +73,16 @@ func (l *Loader) Init() tea.Cmd {
events = append(events, event)
}

ctx := l.Lifetime.Ctx()
p, err := player.FromEventsContext(ctx, events)
if err != nil {
return loadedEvent{
err: err,
}
}

replay := replay.New(
ctx,
player.FromEvents(events),
p,
l.timeBinds,
l.copyBinds,
l.options...,
Expand Down
21 changes: 21 additions & 0 deletions pkg/replay/player/module.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package player

import (
"context"

"github.com/cfoust/cy/pkg/emu"
"github.com/cfoust/cy/pkg/geom"
"github.com/cfoust/cy/pkg/geom/tty"
Expand Down Expand Up @@ -183,3 +185,22 @@ func FromEvents(events []sessions.Event) *Player {

return player
}

// FromEventsContext creates a Player from the given events, but stops if the
// context is cancelled.
func FromEventsContext(
ctx context.Context,
events []sessions.Event,
) (*Player, error) {
player := New()

for _, event := range events {
if ctx.Err() != nil {
return nil, ctx.Err()
}

player.Process(event)
}

return player, nil
}

0 comments on commit 65d9221

Please sign in to comment.