summaryrefslogtreecommitdiffstats
path: root/pkg/gui/gui_driver.go
diff options
context:
space:
mode:
authorJesse Duffield <jessedduffield@gmail.com>2022-08-13 12:56:04 +1000
committerJesse Duffield <jessedduffield@gmail.com>2022-08-13 13:56:50 +1000
commit304d74370e720473ee384e171a8e8f00956fc72f (patch)
tree79de40af6990ee00a426f67f12d1d9d7fcd8067b /pkg/gui/gui_driver.go
parent2bdefe20498f58fe3e8e41a2be08650576ec6d6f (diff)
refactor to ensure code doesn't depend on integration code
Diffstat (limited to 'pkg/gui/gui_driver.go')
-rw-r--r--pkg/gui/gui_driver.go73
1 files changed, 73 insertions, 0 deletions
diff --git a/pkg/gui/gui_driver.go b/pkg/gui/gui_driver.go
new file mode 100644
index 000000000..860c6c9b8
--- /dev/null
+++ b/pkg/gui/gui_driver.go
@@ -0,0 +1,73 @@
+package gui
+
+import (
+ "time"
+
+ "github.com/gdamore/tcell/v2"
+ "github.com/jesseduffield/gocui"
+ "github.com/jesseduffield/lazygit/pkg/commands/models"
+ "github.com/jesseduffield/lazygit/pkg/config"
+ "github.com/jesseduffield/lazygit/pkg/gui/keybindings"
+ "github.com/jesseduffield/lazygit/pkg/gui/types"
+ integrationTypes "github.com/jesseduffield/lazygit/pkg/integration/types"
+)
+
+// this gives our integration test a way of interacting with the gui for sending keypresses
+// and reading state.
+type GuiDriver struct {
+ gui *Gui
+}
+
+var _ integrationTypes.GuiDriver = &GuiDriver{}
+
+func (self *GuiDriver) PressKey(keyStr string) {
+ key := keybindings.GetKey(keyStr)
+
+ var r rune
+ var tcellKey tcell.Key
+ switch v := key.(type) {
+ case rune:
+ r = v
+ tcellKey = tcell.KeyRune
+ case gocui.Key:
+ tcellKey = tcell.Key(v)
+ }
+
+ self.gui.g.ReplayedEvents.Keys <- gocui.NewTcellKeyEventWrapper(
+ tcell.NewEventKey(tcellKey, r, tcell.ModNone),
+ 0,
+ )
+}
+
+func (self *GuiDriver) Keys() config.KeybindingConfig {
+ return self.gui.Config.GetUserConfig().Keybinding
+}
+
+func (self *GuiDriver) CurrentContext() types.Context {
+ return self.gui.c.CurrentContext()
+}
+
+func (self *GuiDriver) Model() *types.Model {
+ return self.gui.State.Model
+}
+
+func (self *GuiDriver) Fail(message string) {
+ self.gui.g.Close()
+ // need to give the gui time to close
+ time.Sleep(time.Millisecond * 100)
+ panic(message)
+}
+
+// logs to the normal place that you log to i.e. viewable with `lazygit --logs`
+func (self *GuiDriver) Log(message string) {
+ self.gui.c.Log.Warn(message)
+}
+
+// logs in the actual UI (in the commands panel)
+func (self *GuiDriver) LogUI(message string) {
+ self.gui.c.LogAction(message)
+}
+
+func (self *GuiDriver) CheckedOutRef() *models.Branch {
+ return self.gui.helpers.Refs.GetCheckedOutRef()
+}