Skip to content

Commit

Permalink
support recording sessions for testing purposes
Browse files Browse the repository at this point in the history
  • Loading branch information
jesseduffield committed Oct 9, 2020
1 parent 37bb89d commit ece93e5
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 6 deletions.
27 changes: 26 additions & 1 deletion pkg/gui/gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/jesseduffield/lazygit/pkg/theme"
"github.com/jesseduffield/lazygit/pkg/updates"
"github.com/jesseduffield/lazygit/pkg/utils"
"github.com/jesseduffield/termbox-go"
"github.com/mattn/go-runewidth"
"github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -107,6 +108,16 @@ type Gui struct {
showRecentRepos bool
Contexts ContextTree
ViewTabContextMap map[string][]tabContext

// this array either includes the events that we're recording in this session
// or the events we've recorded in a prior session
RecordedEvents []RecordedEvent
StartTime time.Time
}

type RecordedEvent struct {
Timestamp int64
Event *termbox.Event
}

type listPanelState struct {
Expand Down Expand Up @@ -399,6 +410,7 @@ func NewGui(log *logrus.Entry, gitCommand *commands.GitCommand, oSCommand *oscom
statusManager: &statusManager{},
viewBufferManagerMap: map[string]*tasks.ViewBufferManager{},
showRecentRepos: showRecentRepos,
RecordedEvents: []RecordedEvent{},
}

gui.resetState()
Expand All @@ -417,12 +429,18 @@ func NewGui(log *logrus.Entry, gitCommand *commands.GitCommand, oSCommand *oscom
func (gui *Gui) Run() error {
gui.resetState()

g, err := gocui.NewGui(gocui.Output256, OverlappingEdges)
recordEvents := recordingEvents()

g, err := gocui.NewGui(gocui.Output256, OverlappingEdges, recordEvents)
if err != nil {
return err
}
defer g.Close()

if recordEvents {
go gui.recordEvents()
}

if gui.State.Modes.Filtering.Active() {
gui.State.ScreenMode = SCREEN_HALF
} else {
Expand Down Expand Up @@ -475,6 +493,9 @@ func (gui *Gui) Run() error {
// if the error returned from a run is a ErrSubProcess, it runs the subprocess
// otherwise it handles the error, possibly by quitting the application
func (gui *Gui) RunWithSubprocesses() error {
gui.StartTime = time.Now()
go gui.replayRecordedEvents()

for {
gui.stopChan = make(chan struct{})
if err := gui.Run(); err != nil {
Expand All @@ -497,6 +518,10 @@ func (gui *Gui) RunWithSubprocesses() error {
}
}

if err := gui.saveRecordedEvents(); err != nil {
return err
}

return nil
case gui.Errors.ErrSwitchRepo, gui.Errors.ErrRestart:
continue
Expand Down
85 changes: 85 additions & 0 deletions pkg/gui/recording.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package gui

import (
"encoding/json"
"io/ioutil"
"log"
"os"
"time"
)

func recordingEvents() bool {
return os.Getenv("RECORD_EVENTS") == "true"
}

func (gui *Gui) timeSinceStart() int64 {
return time.Since(gui.StartTime).Milliseconds()
}

func (gui *Gui) replayRecordedEvents() {
if os.Getenv("REPLAY_EVENTS_FROM") == "" {
return
}

events, err := gui.loadRecordedEvents()
if err != nil {
log.Fatal(err)
}

ticker := time.NewTicker(time.Millisecond)
defer ticker.Stop()

var leeway int64 = 1000

for _, event := range events {
for range ticker.C {
now := gui.timeSinceStart() - leeway
if gui.g != nil && now >= event.Timestamp {
gui.g.ReplayedEvents <- *event.Event
break
}
}
}
}

func (gui *Gui) loadRecordedEvents() ([]RecordedEvent, error) {
path := os.Getenv("REPLAY_EVENTS_FROM")

data, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}

events := []RecordedEvent{}

err = json.Unmarshal(data, &events)
if err != nil {
return nil, err
}

return events, nil
}

func (gui *Gui) saveRecordedEvents() error {
if !recordingEvents() {
return nil
}

jsonEvents, err := json.Marshal(gui.RecordedEvents)
if err != nil {
return err
}

return ioutil.WriteFile("recorded_events.json", jsonEvents, 0600)
}

func (gui *Gui) recordEvents() {
for event := range gui.g.RecordedEvents {
recordedEvent := RecordedEvent{
Timestamp: gui.timeSinceStart(),
Event: event,
}

gui.RecordedEvents = append(gui.RecordedEvents, recordedEvent)
}
}
28 changes: 25 additions & 3 deletions vendor/github.com/jesseduffield/gocui/gui.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 9 additions & 2 deletions vendor/github.com/jesseduffield/gocui/view.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ece93e5

Please sign in to comment.