2020-10-04 17:41:21 +11:00
|
|
|
package gui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
2020-10-05 20:55:15 +11:00
|
|
|
"strconv"
|
2020-10-05 19:55:39 +11:00
|
|
|
|
|
|
|
"github.com/jesseduffield/gocui"
|
2020-10-04 17:41:21 +11:00
|
|
|
)
|
|
|
|
|
|
|
|
func recordingEvents() bool {
|
2020-10-04 18:41:33 +11:00
|
|
|
return recordEventsTo() != ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func recordEventsTo() string {
|
|
|
|
return os.Getenv("RECORD_EVENTS_TO")
|
2020-10-04 17:41:21 +11:00
|
|
|
}
|
|
|
|
|
2021-04-05 08:52:09 +10:00
|
|
|
func replaying() bool {
|
|
|
|
return os.Getenv("REPLAY_EVENTS_FROM") != ""
|
2020-10-04 17:41:21 +11:00
|
|
|
}
|
|
|
|
|
2021-04-05 08:52:09 +10:00
|
|
|
func getRecordingSpeed() int {
|
2020-10-04 18:41:33 +11:00
|
|
|
// humans are slow so this speeds things up.
|
2020-10-05 20:55:15 +11:00
|
|
|
speed := 1
|
|
|
|
envReplaySpeed := os.Getenv("REPLAY_SPEED")
|
|
|
|
if envReplaySpeed != "" {
|
|
|
|
var err error
|
|
|
|
speed, err = strconv.Atoi(envReplaySpeed)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2021-04-05 08:52:09 +10:00
|
|
|
return speed
|
2020-10-04 17:41:21 +11:00
|
|
|
}
|
|
|
|
|
2021-04-05 08:52:09 +10:00
|
|
|
func (gui *Gui) loadRecording() (*gocui.Recording, error) {
|
2020-10-04 17:41:21 +11:00
|
|
|
path := os.Getenv("REPLAY_EVENTS_FROM")
|
|
|
|
|
|
|
|
data, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-04-05 08:52:09 +10:00
|
|
|
recording := &gocui.Recording{}
|
2020-10-04 17:41:21 +11:00
|
|
|
|
2021-04-05 08:52:09 +10:00
|
|
|
err = json.Unmarshal(data, &recording)
|
2020-10-04 17:41:21 +11:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-04-05 08:52:09 +10:00
|
|
|
return recording, nil
|
2020-10-04 17:41:21 +11:00
|
|
|
}
|
|
|
|
|
2021-04-05 08:52:09 +10:00
|
|
|
func (gui *Gui) saveRecording(recording *gocui.Recording) error {
|
2020-10-04 17:41:21 +11:00
|
|
|
if !recordingEvents() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-05 08:52:09 +10:00
|
|
|
jsonEvents, err := json.Marshal(recording)
|
2020-10-04 17:41:21 +11:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-04 18:41:33 +11:00
|
|
|
path := recordEventsTo()
|
|
|
|
|
|
|
|
return ioutil.WriteFile(path, jsonEvents, 0600)
|
2020-10-04 17:41:21 +11:00
|
|
|
}
|