From 46ae55f91e4feab67b01fcd63631dbaf47b3665f Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Tue, 9 Aug 2022 20:27:44 +1000 Subject: introduce gui adapter --- pkg/gui/gui_adapter_impl.go | 73 ++++++++++++++++++++++++++++ pkg/gui/services/custom_commands/resolver.go | 31 ++++++++++++ pkg/gui/test_mode.go | 16 ++---- 3 files changed, 109 insertions(+), 11 deletions(-) create mode 100644 pkg/gui/gui_adapter_impl.go (limited to 'pkg/gui') diff --git a/pkg/gui/gui_adapter_impl.go b/pkg/gui/gui_adapter_impl.go new file mode 100644 index 000000000..dc811f92e --- /dev/null +++ b/pkg/gui/gui_adapter_impl.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 GuiAdapterImpl struct { + gui *Gui +} + +var _ integrationTypes.GuiAdapter = &GuiAdapterImpl{} + +func (self *GuiAdapterImpl) 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 *GuiAdapterImpl) Keys() config.KeybindingConfig { + return self.gui.Config.GetUserConfig().Keybinding +} + +func (self *GuiAdapterImpl) CurrentContext() types.Context { + return self.gui.c.CurrentContext() +} + +func (self *GuiAdapterImpl) Model() *types.Model { + return self.gui.State.Model +} + +func (self *GuiAdapterImpl) 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 *GuiAdapterImpl) Log(message string) { + self.gui.c.Log.Warn(message) +} + +// logs in the actual UI (in the commands panel) +func (self *GuiAdapterImpl) LogUI(message string) { + self.gui.c.LogAction(message) +} + +func (self *GuiAdapterImpl) CheckedOutRef() *models.Branch { + return self.gui.helpers.Refs.GetCheckedOutRef() +} diff --git a/pkg/gui/services/custom_commands/resolver.go b/pkg/gui/services/custom_commands/resolver.go index 35ebbd9d1..1b80987c1 100644 --- a/pkg/gui/services/custom_commands/resolver.go +++ b/pkg/gui/services/custom_commands/resolver.go @@ -1,6 +1,10 @@ package custom_commands import ( + "bytes" + "fmt" + "text/template" + "github.com/jesseduffield/lazygit/pkg/common" "github.com/jesseduffield/lazygit/pkg/config" ) @@ -101,3 +105,30 @@ func (self *Resolver) resolveMenuOption(option *config.CustomCommandMenuOption, Value: value, }, nil } + +func main() { + fmt.Println(ResolveTemplate("old approach: {{index .PromptResponses 0}}, new approach: {{ .Form.a }}", CustomCommandObject{ + PromptResponses: []string{"a"}, + Form: map[string]string{"a": "B"}, + })) +} + +type CustomCommandObject struct { + // deprecated. Use Responses instead + PromptResponses []string + Form map[string]string +} + +func ResolveTemplate(templateStr string, object interface{}) (string, error) { + tmpl, err := template.New("template").Parse(templateStr) + if err != nil { + return "", err + } + + var buf bytes.Buffer + if err := tmpl.Execute(&buf, object); err != nil { + return "", err + } + + return buf.String(), nil +} diff --git a/pkg/gui/test_mode.go b/pkg/gui/test_mode.go index 7f57a4be8..0252aa198 100644 --- a/pkg/gui/test_mode.go +++ b/pkg/gui/test_mode.go @@ -10,6 +10,10 @@ import ( "github.com/jesseduffield/lazygit/pkg/utils" ) +type IntegrationTest interface { + Run(guiAdapter *GuiAdapterImpl) +} + func (gui *Gui) handleTestMode() { if integration.PlayingIntegrationTest() { test, ok := integration.CurrentIntegrationTest() @@ -21,17 +25,7 @@ func (gui *Gui) handleTestMode() { go func() { time.Sleep(time.Millisecond * 100) - shell := &integration.ShellImpl{} - assert := &AssertImpl{gui: gui} - keys := gui.Config.GetUserConfig().Keybinding - input := NewInputImpl(gui, keys, assert, integration.KeyPressDelay()) - - test.Run( - shell, - input, - assert, - gui.c.UserConfig.Keybinding, - ) + test.Run(&GuiAdapterImpl{gui: gui}) gui.g.Update(func(*gocui.Gui) error { return gocui.ErrQuit -- cgit v1.2.3