Skip to content

Commit

Permalink
feat: example story for replay
Browse files Browse the repository at this point in the history
  • Loading branch information
cfoust committed Nov 10, 2023
1 parent cc7db9f commit 500be34
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 17 deletions.
2 changes: 1 addition & 1 deletion cmd/cy/stories.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func startStories() {
log.Logger = log.Output(logs)

ctx := context.Background()
screen, err := stories.Initialize(ctx, "smoke")
screen, err := stories.Initialize(ctx, "search-time-forward")
if err != nil {
panic(err)
}
Expand Down
58 changes: 43 additions & 15 deletions pkg/mux/screen/replay/replay_stories.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,52 @@ import (
"github.com/cfoust/cy/pkg/mux"
"github.com/cfoust/cy/pkg/sessions"
"github.com/cfoust/cy/pkg/stories"
"github.com/cfoust/cy/pkg/taro"

tea "github.com/charmbracelet/bubbletea"
"github.com/xo/terminfo"
)

var Smoke stories.Story = func(ctx context.Context) mux.Screen {
replay := New(
func createTestSession() []sessions.Event {
return sessions.NewSimulator().
Add(
"\033[20h", // CRLF -- why is this everywhere?
geom.DEFAULT_SIZE,
"test string please ignore",
).
Term(terminfo.ClearScreen).
Add("take two").
Term(terminfo.ClearScreen).
Add("test").
Events()
}

func createStory(ctx context.Context, events []sessions.Event, msgs ...interface{}) mux.Screen {
replay := New(ctx, events, bind.NewBindScope())

var realMsg tea.Msg
for _, msg := range msgs {
realMsg = msg
switch msg := msg.(type) {
case ActionType:
realMsg = ActionEvent{Type: msg}
case string:
keyMsgs := taro.KeysToMsg(msg)
if len(keyMsgs) == 1 {
realMsg = keyMsgs[0]
}
}
replay.Send(realMsg)
}

return replay
}

var SearchTimeForward stories.Story = func(ctx context.Context) mux.Screen {
replay := createStory(
ctx,
sessions.NewSimulator().
Add(
"\033[20h", // CRLF -- why is this everywhere?
geom.DEFAULT_SIZE,
"test string please ignore",
).
Term(terminfo.ClearScreen).
Add("take two").
Term(terminfo.ClearScreen).
Add("test").
Events(),
bind.NewBindScope(),
createTestSession(),
ActionSearchForward,
)

return replay
Expand All @@ -38,5 +66,5 @@ func init() {
config := stories.Config{
Size: geom.DEFAULT_SIZE,
}
stories.Register("smoke", Smoke, config)
stories.Register("search-time-forward", SearchTimeForward, config)
}
1 change: 0 additions & 1 deletion pkg/sessions/simulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ type Simulator struct {

func (s *Simulator) store(delta time.Duration, data P.Message) {
event := Event{
Stamp: time.Now(),
Message: data,
}

Expand Down
1 change: 1 addition & 0 deletions pkg/stories/viewer.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func (s *StoryViewer) View(state *tty.State) {
W: storySize.C,
})
tty.Copy(storyPos, state, contents)
state.CursorVisible = contents.CursorVisible
}

func (s *StoryViewer) Update(msg tea.Msg) (taro.Model, tea.Cmd) {
Expand Down

0 comments on commit 500be34

Please sign in to comment.