diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2020-10-04 17:41:21 +1100 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2020-10-10 00:23:01 +1100 |
commit | ece93e5eef487f6a8455205c7dbb919ccca43647 (patch) | |
tree | 6873b8ecc703d4d22d5bc435e4c2e8a2d1a35173 /pkg/gui/recording.go | |
parent | 37bb89dac33cb4236bf817e1e2e09cb1cbfade5c (diff) |
support recording sessions for testing purposes
Diffstat (limited to 'pkg/gui/recording.go')
-rw-r--r-- | pkg/gui/recording.go | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/pkg/gui/recording.go b/pkg/gui/recording.go new file mode 100644 index 000000000..5e0914303 --- /dev/null +++ b/pkg/gui/recording.go @@ -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) + } +} |