diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2022-01-29 19:09:20 +1100 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2022-03-17 19:13:40 +1100 |
commit | 138be04e6575f2bd087630e49d122af578c78bf6 (patch) | |
tree | ed038641d6871e49ff426096f300e6b3b3ab4f1e /pkg/gui | |
parent | 1a74ed32143f826104e1d60f4392d2d8ba53cd80 (diff) |
refactor contexts code
Diffstat (limited to 'pkg/gui')
60 files changed, 1154 insertions, 602 deletions
diff --git a/pkg/gui/basic_context.go b/pkg/gui/basic_context.go index 1043cca89..e9a3ca933 100644 --- a/pkg/gui/basic_context.go +++ b/pkg/gui/basic_context.go @@ -1,71 +1,48 @@ package gui import ( + "github.com/jesseduffield/lazygit/pkg/gui/context" "github.com/jesseduffield/lazygit/pkg/gui/types" ) -type BasicContext struct { +type SimpleContext struct { OnFocus func(opts ...types.OnFocusOpts) error OnFocusLost func() error OnRender func() error // this is for pushing some content to the main view - OnRenderToMain func(opts ...types.OnFocusOpts) error - Kind types.ContextKind - Key types.ContextKey - ViewName string - WindowName string - OnGetOptionsMap func() map[string]string + OnRenderToMain func(opts ...types.OnFocusOpts) error - ParentContext types.Context - // we can't know on the calling end whether a Context is actually a nil value without reflection, so we're storing this flag here to tell us. There has got to be a better way around this - hasParent bool + *context.BaseContext } -var _ types.Context = &BasicContext{} - -func (self *BasicContext) GetOptionsMap() map[string]string { - if self.OnGetOptionsMap != nil { - return self.OnGetOptionsMap() - } - return nil -} - -func (self *BasicContext) SetParentContext(context types.Context) { - self.ParentContext = context - self.hasParent = true -} - -func (self *BasicContext) GetParentContext() (types.Context, bool) { - return self.ParentContext, self.hasParent -} - -func (self *BasicContext) SetWindowName(windowName string) { - self.WindowName = windowName +type NewSimpleContextOpts struct { + OnFocus func(opts ...types.OnFocusOpts) error + OnFocusLost func() error + OnRender func() error + // this is for pushing some content to the main view + OnRenderToMain func(opts ...types.OnFocusOpts) error } -func (self *BasicContext) GetWindowName() string { - windowName := self.WindowName - - if windowName != "" { - return windowName +func NewSimpleContext(baseContext *context.BaseContext, opts NewSimpleContextOpts) *SimpleContext { + return &SimpleContext{ + OnFocus: opts.OnFocus, + OnFocusLost: opts.OnFocusLost, + OnRender: opts.OnRender, + OnRenderToMain: opts.OnRenderToMain, + BaseContext: baseContext, } - - // TODO: actually set this for everything so we don't default to the view name - return self.ViewName } -func (self *BasicContext) HandleRender() error { +var _ types.Context = &SimpleContext{} + +func (self *SimpleContext) HandleRender() error { if self.OnRender != nil { return self.OnRender() } return nil } -func (self *BasicContext) GetViewName() string { - return self.ViewName -} - -func (self *BasicContext) HandleFocus(opts ...types.OnFocusOpts) error { +func (self *SimpleContext) HandleFocus(opts ...types.OnFocusOpts) error { if self.OnFocus != nil { if err := self.OnFocus(opts...); err != nil { return err @@ -81,25 +58,17 @@ func (self *BasicContext) HandleFocus(opts ...types.OnFocusOpts) error { return nil } -func (self *BasicContext) HandleFocusLost() error { +func (self *SimpleContext) HandleFocusLost() error { if self.OnFocusLost != nil { return self.OnFocusLost() } return nil } -func (self *BasicContext) HandleRenderToMain() error { +func (self *SimpleContext) HandleRenderToMain() error { if self.OnRenderToMain != nil { return self.OnRenderToMain() } return nil } - -func (self *BasicContext) GetKind() types.ContextKind { - return self.Kind -} - -func (self *BasicContext) GetKey() types.ContextKey { - return self.Key -} diff --git a/pkg/gui/branches_panel.go b/pkg/gui/branches_panel.go index 18094b297..7b179e2ff 100644 --- a/pkg/gui/branches_panel.go +++ b/pkg/gui/branches_panel.go @@ -8,7 +8,6 @@ import ( "github.com/jesseduffield/lazygit/pkg/commands/git_commands" "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/gui/controllers" - "github.com/jesseduffield/lazygit/pkg/gui/popup" "github.com/jesseduffield/lazygit/pkg/gui/types" "github.com/jesseduffield/lazygit/pkg/utils" ) @@ -145,7 +144,7 @@ func (gui *Gui) handleForceCheckout() error { message := gui.c.Tr.SureForceCheckout title := gui.c.Tr.ForceCheckoutBranch - return gui.c.Ask(popup.AskOpts{ + return gui.c.Ask(types.AskOpts{ Title: title, Prompt: message, HandleConfirm: func() error { @@ -159,14 +158,14 @@ func (gui *Gui) handleForceCheckout() error { } func (gui *Gui) handleCheckoutByName() error { - return gui.c.Prompt(popup.PromptOpts{ + return gui.c.Prompt(types.PromptOpts{ Title: gui.c.Tr.BranchName + ":", FindSuggestionsFunc: gui.suggestionsHelper.GetRefsSuggestionsFunc(), HandleConfirm: func(response string) error { gui.c.LogAction("Checkout branch") return gui.refHelper.CheckoutRef(response, types.CheckoutRefOptions{ OnRefNotFound: func(ref string) error { - return gui.c.Ask(popup.AskOpts{ + return gui.c.Ask(types.AskOpts{ Title: gui.c.Tr.BranchNotFoundTitle, Prompt: fmt.Sprintf("%s %s%s", gui.c.Tr.BranchNotFoundPrompt, ref, "?"), HandleConfirm: func() error { @@ -232,7 +231,7 @@ func (gui *Gui) deleteNamedBranch(selectedBranch *models.Branch, force bool) err }, ) - return gui.c.Ask(popup.AskOpts{ + return gui.c.Ask(types.AskOpts{ Title: title, Prompt: message, HandleConfirm: func() error { @@ -265,7 +264,7 @@ func (gui *Gui) mergeBranchIntoCheckedOutBranch(branchName string) error { }, ) - return gui.c.Ask(popup.AskOpts{ + return gui.c.Ask(types.AskOpts{ Title: gui.c.Tr.MergingTitle, Prompt: prompt, HandleConfirm: func() error { @@ -299,7 +298,7 @@ func (gui *Gui) handleRebaseOntoBranch(selectedBranchName string) error { }, ) - return gui.c.Ask(popup.AskOpts{ + return gui.c.Ask(types.AskOpts{ Title: gui.c.Tr.RebasingTitle, Prompt: prompt, HandleConfirm: func() error { @@ -368,7 +367,7 @@ func (gui *Gui) handleRenameBranch() error { } promptForNewName := func() error { - return gui.c.Prompt(popup.PromptOpts{ + return gui.c.Prompt(types.PromptOpts{ Title: gui.c.Tr.NewBranchNamePrompt + " " + branch.Name + ":", InitialContent: branch.Name, HandleConfirm: func(newBranchName string) error { @@ -402,7 +401,7 @@ func (gui *Gui) handleRenameBranch() error { return promptForNewName() } - return gui.c.Ask(popup.AskOpts{ + return gui.c.Ask(types.AskOpts{ Title: gui.c.Tr.LcRenameBranch, Prompt: gui.c.Tr.RenameBranchWarning, HandleConfirm: promptForNewName, @@ -430,7 +429,7 @@ func (gui *Gui) handleNewBranchOffCurrentItem() error { prefilledName = strings.SplitAfterN(item.ID(), "/", 2)[1] } - return gui.c.Prompt(popup.PromptOpts{ + return gui.c.Prompt(types.PromptOpts{ Title: message, InitialContent: prefilledName, HandleConfirm: func(response string) error { diff --git a/pkg/gui/cherry_picking.go b/pkg/gui/cherry_picking.go index 28554edce..be6fe379d 100644 --- a/pkg/gui/cherry_picking.go +++ b/pkg/gui/cherry_picking.go @@ -2,7 +2,6 @@ package gui import ( "github.com/jesseduffield/lazygit/pkg/commands/models" - "github.com/jesseduffield/lazygit/pkg/gui/popup" "github.com/jesseduffield/lazygit/pkg/gui/types" ) @@ -135,7 +134,7 @@ func (gui *Gui) handleCopyCommitRange() error { // HandlePasteCommits begins a cherry-pick rebase with the commits the user has copied func (gui *Gui) HandlePasteCommits() error { - return gui.c.Ask(popup.AskOpts{ + return gui.c.Ask(types.AskOpts{ Title: gui.c.Tr.CherryPick, Prompt: gui.c.Tr.SureCherryPick, HandleConfirm: func() error { diff --git a/pkg/gui/commit_files_panel.go b/pkg/gui/commit_files_panel.go index abae8196f..ef2c94bef 100644 --- a/pkg/gui/commit_files_panel.go +++ b/pkg/gui/commit_files_panel.go @@ -5,7 +5,6 @@ import ( "github.com/jesseduffield/lazygit/pkg/commands/patch" "github.com/jesseduffield/lazygit/pkg/gui/controllers" "github.com/jesseduffield/lazygit/pkg/gui/filetree" - "github.com/jesseduffield/lazygit/pkg/gui/popup" "github.com/jesseduffield/lazygit/pkg/gui/types" ) @@ -81,7 +80,7 @@ func (gui *Gui) handleDiscardOldFileChange() error { fileName := gui.getSelectedCommitFileName() - return gui.c.Ask(popup.AskOpts{ + return gui.c.Ask(types.AskOpts{ Title: gui.c.Tr.DiscardFileChangesTitle, Prompt: gui.c.Tr.DiscardFileChangesPrompt, HandleConfirm: func() error { @@ -181,7 +180,7 @@ func (gui *Gui) handleToggleFileForPatch() error { } if gui.git.Patch.PatchManager.Active() && gui.git.Patch.PatchManager.To != gui.State.CommitFileTreeViewModel.GetParent() { - return gui.c.Ask(popup.AskOpts{ + return gui.c.Ask(types.AskOpts{ Title: gui.c.Tr.DiscardPatch, Prompt: gui.c.Tr.DiscardPatchConfirm, HandleConfirm: func() error { @@ -229,7 +228,7 @@ func (gui *Gui) enterCommitFile(opts types.OnFocusOpts) error { } if gui.git.Patch.PatchManager.Active() && gui.git.Patch.PatchManager.To != gui.State.CommitFileTreeViewModel.GetParent() { - return gui.c.Ask(popup.AskOpts{ + return gui.c.Ask(types.AskOpts{ Title: gui.c.Tr.DiscardPatch, Prompt: gui.c.Tr.DiscardPatchConfirm, HandleConfirm: func() error { diff --git a/pkg/gui/confirmation_panel.go b/pkg/gui/confirmation_panel.go index 1d865348f..51c71b30b 100644 --- a/pkg/gui/confirmation_panel.go +++ b/pkg/gui/confirmation_panel.go @@ -191,12 +191,6 @@ func (gui *Gui) setKeyBindings(opts popup.CreatePopupPanelOpts) error { onConfirm = gui.wrappedConfirmationFunction(opts.HandlersManageFocus, opts.HandleConfirm) } - type confirmationKeybinding struct { - viewName string - key interface{} - handler func() error - } - keybindingConfig := gui.c.UserConfig.Keybinding onSuggestionConfirm := gui.wrappedPromptConfirmationFunction( opts.HandlersManageFocus, @@ -204,26 +198,30 @@ func (gui *Gui) setKeyBindings(opts popup.CreatePopupPanelOpts) error { gui.getSelectedSuggestionValue, ) - confirmationKeybindings := []confirmationKeybinding{ + bindings := []*types.Binding{ { - viewName: "confirmation", - key: gui.getKey(keybindingConfig.Universal.Confirm), - handler: onConfirm, + ViewName: "confirmation", + Contexts: []string{string(CONFIRMATION_CONTEXT_KEY)}, + Key: gui.getKey(keybindingConfig.Universal.Confirm), + Handler: onConfirm, }, { - viewName: "confirmation", - key: gui.getKey(keybindingConfig.Universal.ConfirmAlt1), - handler: onConfirm, + ViewName: "confirmation", + Contexts: []string{string(CONFIRMATION_CONTEXT_KEY)}, + Key: gui.getKey(keybindingConfig.Universal.ConfirmAlt1), + Handler: onConfirm, }, { - viewName: "confirmation", - key: gui.getKey(keybindingConfig.Universal.Return), - handler: gui.wrappedConfirmationFunction(opts.HandlersManageFocus, opts.HandleClose), + ViewName: "confirmation", + Contexts: []string{string(CONFIRMATION_CONTEXT_KEY)}, + Key: gui.getKey(keybindingConfig.Universal.Return), + Handler: gui.wrappedConfirmationFunction(opts.HandlersManageFocus, opts.HandleClose), }, { - viewName: "confirmation", - key: gui.getKey(keybindingConfig.Universal.TogglePanel), - handler: func() error { + ViewName: "confirmation", + Contexts: []string{string(CONFIRMATION_CONTEXT_KEY)}, + Key: gui.getKey(keybindingConfig.Universal.TogglePanel), + Handler: func() error { if len(gui.State.Suggestions) > 0 { return gui.replaceContext(gui.State.Contexts.Suggestions) } @@ -231,29 +229,33 @@ func (gui *Gui) setKeyBindings(opts popup.CreatePopupPanelOpts) error { }, }, { - viewName: "suggestions", - key: gui.getKey(keybindingConfig.Universal.Confirm), - handler: onSuggestionConfirm, + ViewName: "suggestions", + Contexts: []string{string(CONFIRMATION_CONTEXT_KEY)}, + Key: gui.getKey(keybindingConfig.Universal.Confirm), + Handler: onSuggestionConfirm, }, { - viewName: "suggestions", - key: gui.getKey(keybindingConfig.Universal.ConfirmAlt1), - handler: onSuggestionConfirm, + ViewName: "suggestions", + Contexts: []string{string(CONFIRMATION_CONTEXT_KEY)}, + Key: gui.getKey(keybindingConfig.Universal.ConfirmAlt1), + Handler: onSuggestionConfirm, }, { - viewName: "suggestions", - key: gui.getKey(keybindingConfig.Universal.Return), - handler: gui.wrappedConfirmationFunction(opts.HandlersManageFocus, opts.HandleClose), + ViewName: "suggestions", + Contexts: []string{string(CONFIRMATION_CONTEXT_KEY)}, + Key: gui.getKey(keybindingConfig.Universal.Return), + Handler: gui.wrappedConfirmationFunction(opts.HandlersManageFocus, opts.HandleClose), }, { - viewName: "suggestions", - key: gui.getKey(keybindingConfig.Universal.TogglePanel), - handler: func() error { return gui.replaceContext(gui.State.Contexts.Confirmation) }, + ViewName: "suggestions", + Contexts: []string{string(CONFIRMATION_CONTEXT_KEY)}, + Key: gui.getKey(keybindingConfig.Universal.TogglePanel), + Handler: func() error { return gui.replaceContext(gui.State.Contexts.Confirmation) }, }, } - for _, binding := range confirmationKeybindings { - if err := gui.g.SetKeybinding(binding.viewName, nil, binding.key, gocui.ModNone, gui.wrappedHandler(binding.handler)); err != nil { + for _, binding := range bindings { + if err := gui.SetKeybinding(binding); err != nil { return err } } @@ -271,12 +273,6 @@ func (gui *Gui) clearConfirmationViewKeyBindings() { _ = gui.g.DeleteKeybinding("suggestions", gui.getKey(keybindingConfig.Universal.Return), gocui.ModNone) } -func (gui *Gui) wrappedHandler(f func() error) func(g *gocui.Gui, v *gocui.View) error { - return func(g *gocui.Gui, v *gocui.View) error { - return f() - } -} - func (gui *Gui) refreshSuggestions() { gui.suggestionsAsyncHandler.Do(func() func() { suggestions := gui.findSuggestions(gui.c.GetPromptInput()) diff --git a/pkg/gui/context/base_context.go b/pkg/gui/context/base_context.go new file mode 100644 index 000000000..e95b298a4 --- /dev/null +++ b/pkg/gui/context/base_context.go @@ -0,0 +1,67 @@ +package context + +import "github.com/jesseduffield/lazygit/pkg/gui/types" + +type BaseContext struct { + Kind types.ContextKind + Key types.ContextKey + ViewName string + WindowName string + OnGetOptionsMap func() map[string]string + + *ParentContextMgr +} + +func (self *BaseContext) GetOptionsMap() map[string]string { + if self.OnGetOptionsMap != nil { + return self.OnGetOptionsMap() + } + return nil +} + +func (self *BaseContext) SetWindowName(windowName string) { + self.WindowName = windowName +} + +func (self *BaseContext) GetWindowName() string { + windowName := self.WindowName + + if windowName != "" { + return windowName + } + + // TODO: actually set this for everything so we don't default to the view name |