summaryrefslogtreecommitdiffstats
path: root/pkg/gui
diff options
context:
space:
mode:
authorJesse Duffield <jessedduffield@gmail.com>2021-12-06 21:08:36 +1100
committerJesse Duffield <jessedduffield@gmail.com>2021-12-06 22:37:28 +1100
commit18283ad41bbb2ac17c8482ddaaf0f31ea9d38a26 (patch)
treeb1690cacecb4605edf3c857052141bbf0199d6e9 /pkg/gui
parent1996eddd9124709431ced14b1650ae5a50963be7 (diff)
add popup handler for easier testing
Diffstat (limited to 'pkg/gui')
-rw-r--r--pkg/gui/confirmation_panel.go34
-rw-r--r--pkg/gui/context_test.go6
-rw-r--r--pkg/gui/diff_context_size_test.go47
-rw-r--r--pkg/gui/dummies.go8
-rw-r--r--pkg/gui/gui.go3
-rw-r--r--pkg/gui/popup_handler.go87
6 files changed, 134 insertions, 51 deletions
diff --git a/pkg/gui/confirmation_panel.go b/pkg/gui/confirmation_panel.go
index e1b78bc3a..c540aa0d0 100644
--- a/pkg/gui/confirmation_panel.go
+++ b/pkg/gui/confirmation_panel.go
@@ -1,5 +1,3 @@
-// lots of this has been directly ported from one of the example files, will brush up later
-
package gui
import (
@@ -7,7 +5,6 @@ import (
"strings"
"github.com/jesseduffield/gocui"
- "github.com/jesseduffield/lazygit/pkg/gui/style"
"github.com/jesseduffield/lazygit/pkg/gui/types"
"github.com/jesseduffield/lazygit/pkg/theme"
"github.com/jesseduffield/lazygit/pkg/utils"
@@ -44,30 +41,15 @@ type promptOpts struct {
}
func (gui *Gui) ask(opts askOpts) error {
- return gui.createPopupPanel(createPopupPanelOpts{
- title: opts.title,
- prompt: opts.prompt,
- handleConfirm: opts.handleConfirm,
- handleClose: opts.handleClose,
- handlersManageFocus: opts.handlersManageFocus,
- })
+ return gui.PopupHandler.Ask(opts)
}
func (gui *Gui) prompt(opts promptOpts) error {
- return gui.createPopupPanel(createPopupPanelOpts{
- title: opts.title,
- prompt: opts.initialContent,
- editable: true,
- handleConfirmPrompt: opts.handleConfirm,
- findSuggestionsFunc: opts.findSuggestionsFunc,
- })
+ return gui.PopupHandler.Prompt(opts)
}
func (gui *Gui) createLoaderPanel(prompt string) error {
- return gui.createPopupPanel(createPopupPanelOpts{
- prompt: prompt,
- hasLoader: true,
- })
+ return gui.PopupHandler.Loader(prompt)
}
func (gui *Gui) wrappedConfirmationFunction(handlersManageFocus bool, function func() error) func() error {
@@ -339,15 +321,7 @@ func (gui *Gui) wrappedHandler(f func() error) func(g *gocui.Gui, v *gocui.View)
}
func (gui *Gui) createErrorPanel(message string) error {
- coloredMessage := style.FgRed.Sprint(strings.TrimSpace(message))
- if err := gui.refreshSidePanels(refreshOptions{mode: ASYNC}); err != nil {
- return err
- }
-
- return gui.ask(askOpts{
- title: gui.Tr.Error,
- prompt: coloredMessage,
- })
+ return gui.PopupHandler.Error(message)
}
func (gui *Gui) surfaceError(err error) error {
diff --git a/pkg/gui/context_test.go b/pkg/gui/context_test.go
index abfcab4cd..7f03f7484 100644
--- a/pkg/gui/context_test.go
+++ b/pkg/gui/context_test.go
@@ -8,7 +8,7 @@ import (
)
func TestCanDeactivatePopupContextsWithoutViews(t *testing.T) {
- contexts := []func(gui *Gui) Context {
+ contexts := []func(gui *Gui) Context{
func(gui *Gui) Context { return gui.State.Contexts.Credentials },
func(gui *Gui) Context { return gui.State.Contexts.Confirmation },
func(gui *Gui) Context { return gui.State.Contexts.CommitMessage },
@@ -20,7 +20,7 @@ func TestCanDeactivatePopupContextsWithoutViews(t *testing.T) {
context := c(gui)
gui.g = &gocui.Gui{}
- gui.deactivateContext(context)
+ _ = gui.deactivateContext(context)
// This really only checks a prerequisit, not the effect of deactivateContext
view, _ := gui.g.View(context.GetViewName())
@@ -32,7 +32,7 @@ func TestCanDeactivateCommitFilesContextsWithoutViews(t *testing.T) {
gui := NewDummyGui()
gui.g = &gocui.Gui{}
- gui.deactivateContext(gui.State.Contexts.CommitFiles)
+ _ = gui.deactivateContext(gui.State.Contexts.CommitFiles)
// This really only checks a prerequisite, not the effect of deactivateContext
view, _ := gui.g.View(gui.State.Contexts.CommitFiles.GetViewName())
diff --git a/pkg/gui/diff_context_size_test.go b/pkg/gui/diff_context_size_test.go
index bd5758bf7..6007228a4 100644
--- a/pkg/gui/diff_context_size_test.go
+++ b/pkg/gui/diff_context_size_test.go
@@ -28,7 +28,7 @@ func setupGuiForTest(gui *Gui) {
gui.Views.Main, _ = gui.prepareView("main")
gui.Views.Secondary, _ = gui.prepareView("secondary")
gui.GitCommand.PatchManager = &patch.PatchManager{}
- gui.refreshLineByLinePanel(diffForTest, "", false, 11)
+ _, _ = gui.refreshLineByLinePanel(diffForTest, "", false, 11)
}
func TestIncreasesContextInDiffViewByOneInContextWithDiff(t *testing.T) {
@@ -47,9 +47,9 @@ func TestIncreasesContextInDiffViewByOneInContextWithDiff(t *testing.T) {
context := c(gui)
setupGuiForTest(gui)
gui.Config.GetUserConfig().Git.DiffContextSize = 1
- gui.pushContextDirect(context)
+ _ = gui.pushContextDirect(context)
- gui.IncreaseContextInDiffView()
+ _ = gui.IncreaseContextInDiffView()
assert.Equal(t, 2, gui.Config.GetUserConfig().Git.DiffContextSize, string(context.GetKey()))
}
@@ -73,9 +73,9 @@ func TestDoesntIncreaseContextInDiffViewInContextWithoutDiff(t *testing.T) {
context := c(gui)
setupGuiForTest(gui)
gui.Config.GetUserConfig().Git.DiffContextSize = 1
- gui.pushContextDirect(context)
+ _ = gui.pushContextDirect(context)
- gui.IncreaseContextInDiffView()
+ _ = gui.IncreaseContextInDiffView()
assert.Equal(t, 1, gui.Config.GetUserConfig().Git.DiffContextSize, string(context.GetKey()))
}
@@ -97,9 +97,9 @@ func TestDecreasesContextInDiffViewByOneInContextWithDiff(t *testing.T) {
context := c(gui)
setupGuiForTest(gui)
gui.Config.GetUserConfig().Git.DiffContextSize = 2
- gui.pushContextDirect(context)
+ _ = gui.pushContextDirect(context)
- gui.DecreaseContextInDiffView()
+ _ = gui.DecreaseContextInDiffView()
assert.Equal(t, 1, gui.Config.GetUserConfig().Git.DiffContextSize, string(context.GetKey()))
}
@@ -123,9 +123,9 @@ func TestDoesntDecreaseContextInDiffViewInContextWithoutDiff(t *testing.T) {
context := c(gui)
setupGuiForTest(gui)
gui.Config.GetUserConfig().Git.DiffContextSize = 2
- gui.pushContextDirect(context)
+ _ = gui.pushContextDirect(context)
- gui.DecreaseContextInDiffView()
+ _ = gui.DecreaseContextInDiffView()
assert.Equal(t, 2, gui.Config.GetUserConfig().Git.DiffContextSize, string(context.GetKey()))
}
@@ -135,11 +135,21 @@ func TestDoesntIncreaseContextInDiffViewInContextWhenInPatchBuildingMode(t *test
gui := NewDummyGui()
setupGuiForTest(gui)
gui.Config.GetUserConfig().Git.DiffContextSize = 2
- gui.pushContextDirect(gui.State.Contexts.CommitFiles)
+ _ = gui.pushContextDirect(gui.State.Contexts.CommitFiles)
gui.GitCommand.PatchManager.Start("from", "to", false, false)
- gui.IncreaseContextInDiffView()
+ errorCount := 0
+ gui.PopupHandler = &TestPopupHandler{
+ onError: func(message string) error {
+ assert.Equal(t, gui.Tr.CantChangeContextSizeError, message)
+ errorCount += 1
+ return nil
+ },
+ }
+
+ _ = gui.IncreaseContextInDiffView()
+ assert.Equal(t, 1, errorCount)
assert.Equal(t, 2, gui.Config.GetUserConfig().Git.DiffContextSize)
}
@@ -147,10 +157,19 @@ func TestDoesntDecreaseContextInDiffViewInContextWhenInPatchBuildingMode(t *test
gui := NewDummyGui()
setupGuiForTest(gui)
gui.Config.GetUserConfig().Git.DiffContextSize = 2
- gui.pushContextDirect(gui.State.Contexts.CommitFiles)
+ _ = gui.pushContextDirect(gui.State.Contexts.CommitFiles)
gui.GitCommand.PatchManager.Start("from", "to", false, false)
- gui.DecreaseContextInDiffView()
+ errorCount := 0
+ gui.PopupHandler = &TestPopupHandler{
+ onError: func(message string) error {
+ assert.Equal(t, gui.Tr.CantChangeContextSizeError, message)
+ errorCount += 1
+ return nil
+ },
+ }
+
+ _ = gui.DecreaseContextInDiffView()
assert.Equal(t, 2, gui.Config.GetUserConfig().Git.DiffContextSize)
}
@@ -160,7 +179,7 @@ func TestDecreasesContextInDiffViewNoFurtherThanOne(t *testing.T) {
setupGuiForTest(gui)
gui.Config.GetUserConfig().Git.DiffContextSize = 1
- gui.DecreaseContextInDiffView()
+ _ = gui.DecreaseContextInDiffView()
assert.Equal(t, 1, gui.Config.GetUserConfig().Git.DiffContextSize)
}
diff --git a/pkg/gui/dummies.go b/pkg/gui/dummies.go
index 0e37bbbc8..3c993836a 100644
--- a/pkg/gui/dummies.go
+++ b/pkg/gui/dummies.go
@@ -12,12 +12,12 @@ import (
// NewDummyGui creates a new dummy GUI for testing
func NewDummyUpdater() *updates.Updater {
newAppConfig := config.NewDummyAppConfig()
- DummyUpdater, _ := updates.NewUpdater(utils.NewDummyLog(), newAppConfig, oscommands.NewDummyOSCommand(), i18n.NewTranslationSet(utils.NewDummyLog(), newAppConfig.GetUserConfig().Gui.Language))
- return DummyUpdater
+ dummyUpdater, _ := updates.NewUpdater(utils.NewDummyLog(), newAppConfig, oscommands.NewDummyOSCommand(), i18n.NewTranslationSet(utils.NewDummyLog(), newAppConfig.GetUserConfig().Gui.Language))
+ return dummyUpdater
}
func NewDummyGui() *Gui {
newAppConfig := config.NewDummyAppConfig()
- DummyGui, _ := NewGui(utils.NewDummyLog(), commands.NewDummyGitCommand(), oscommands.NewDummyOSCommand(), i18n.NewTranslationSet(utils.NewDummyLog(), newAppConfig.GetUserConfig().Gui.Language), newAppConfig, NewDummyUpdater(), "", false)
- return DummyGui
+ dummyGui, _ := NewGui(utils.NewDummyLog(), commands.NewDummyGitCommand(), oscommands.NewDummyOSCommand(), i18n.NewTranslationSet(utils.NewDummyLog(), newAppConfig.GetUserConfig().Gui.Language), newAppConfig, NewDummyUpdater(), "", false)
+ return dummyGui
}
diff --git a/pkg/gui/gui.go b/pkg/gui/gui.go
index 35b497fc9..4a2248935 100644
--- a/pkg/gui/gui.go
+++ b/pkg/gui/gui.go
@@ -121,6 +121,8 @@ type Gui struct {
ShowExtrasWindow bool
suggestionsAsyncHandler *tasks.AsyncHandler
+
+ PopupHandler PopupHandler
}
type listPanelState struct {
@@ -455,6 +457,7 @@ func NewGui(log *logrus.Entry, gitCommand *commands.GitCommand, oSCommand *oscom
onRunCommand := gui.GetOnRunCommand()
oSCommand.SetOnRunCommand(onRunCommand)
gui.OnRunCommand = onRunCommand
+ gui.PopupHandler = &RealPopupHandler{gui: gui}
authors.SetCustomAuthors(gui.Config.GetUserConfig().Gui.AuthorColors)
diff --git a/pkg/gui/popup_handler.go b/pkg/gui/popup_handler.go
new file mode 100644
index 000000000..9cacc3574
--- /dev/null
+++ b/pkg/gui/popup_handler.go
@@ -0,0 +1,87 @@
+package gui
+
+import (
+ "strings"
+
+ "github.com/jesseduffield/lazygit/pkg/gui/style"
+)
+
+type PopupHandler interface {
+ Error(message string) error
+ Ask(opts askOpts) error
+ Prompt(opts promptOpts) error
+ Loader(message string) error
+}
+
+type RealPopupHandler struct {
+ gui *Gui
+}
+
+func (self *RealPopupHandler) Error(message string) error {
+ gui := self.gui
+
+ coloredMessage := style.FgRed.Sprint(strings.TrimSpace(message))
+ if err := gui.refreshSidePanels(refreshOptions{mode: ASYNC}); err != nil {
+ return err
+ }
+
+ return self.Ask(askOpts{
+ title: gui.Tr.Error,
+ prompt: coloredMessage,
+ })
+}
+
+func (self *RealPopupHandler) Ask(opts askOpts) error {
+ gui := self.gui
+
+ return gui.createPopupPanel(createPopupPanelOpts{
+ title: opts.title,
+ prompt: opts.prompt,
+ handleConfirm: opts.handleConfirm,
+ handleClose: opts.handleClose,
+ handlersManageFocus: opts.handlersManageFocus,
+ })
+}
+
+func (self *RealPopupHandler) Prompt(opts promptOpts) error {
+ gui := self.gui
+
+ return gui.createPopupPanel(createPopupPanelOpts{
+ title: opts.title,
+ prompt: opts.initialContent,
+ editable: true,
+ handleConfirmPrompt: opts.handleConfirm,
+ findSuggestionsFunc: opts.findSuggestionsFunc,
+ })
+}
+
+func (self *RealPopupHandler) Loader(message string) error {
+ gui := self.gui
+
+ return gui.createPopupPanel(createPopupPanelOpts{
+ prompt: message,
+ hasLoader: true,
+ })
+}
+
+type TestPopupHandler struct {
+ onError func(message string) error
+ onAsk func(opts askOpts) error
+ onPrompt func(opts promptOpts) error
+}
+
+func (self *TestPopupHandler) Error(message string) error {
+ return self.onError(message)
+}
+
+func (self *TestPopupHandler) Ask(opts askOpts) error {
+ return self.onAsk(opts)
+}
+
+func (self *TestPopupHandler) Prompt(opts promptOpts) error {
+ return self.onPrompt(opts)
+}
+
+func (self *TestPopupHandler) Loader(message string) error {
+ return nil
+}