From 8e3484d8e98faf12f8395eaf5f9e8381f77a8e52 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Sat, 5 Feb 2022 11:00:57 +1100 Subject: [PATCH] add global controller --- pkg/gui/controllers/files_controller.go | 31 ----------- pkg/gui/controllers/global_controller.go | 68 +++++++++++++++++++++++ pkg/gui/controllers/suggestions_helper.go | 16 ++---- pkg/gui/gui.go | 6 ++ pkg/gui/keybindings.go | 1 + 5 files changed, 79 insertions(+), 43 deletions(-) create mode 100644 pkg/gui/controllers/global_controller.go diff --git a/pkg/gui/controllers/files_controller.go b/pkg/gui/controllers/files_controller.go index 1f9521867..c8af30e62 100644 --- a/pkg/gui/controllers/files_controller.go +++ b/pkg/gui/controllers/files_controller.go @@ -170,12 +170,6 @@ func (self *FilesController) GetKeybindings(opts types.KeybindingsOpts) []*types Handler: self.enter, Description: self.c.Tr.FileEnter, }, - { - ViewName: "", - Key: opts.GetKey(opts.Config.Universal.ExecuteCustomCommand), - Handler: self.handleCustomCommand, - Description: self.c.Tr.LcExecuteCustomCommand, - }, { Key: opts.GetKey(opts.Config.Commits.ViewResetOptions), Handler: self.createResetMenu, @@ -577,31 +571,6 @@ func (self *FilesController) switchToMerge() error { return self.switchToMergeFn(file.Name) } -func (self *FilesController) handleCustomCommand() error { - return self.c.Prompt(types.PromptOpts{ - Title: self.c.Tr.CustomCommand, - FindSuggestionsFunc: self.suggestionsHelper.GetCustomCommandsHistorySuggestionsFunc(), - HandleConfirm: func(command string) error { - self.c.GetAppState().CustomCommandsHistory = utils.Limit( - utils.Uniq( - append(self.c.GetAppState().CustomCommandsHistory, command), - ), - 1000, - ) - - err := self.c.SaveAppState() - if err != nil { - self.c.Log.Error(err) - } - - self.c.LogAction(self.c.Tr.Actions.CustomCommand) - return self.c.RunSubprocessAndRefresh( - self.os.Cmd.NewShell(command), - ) - }, - }) -} - func (self *FilesController) createStashMenu() error { return self.c.Menu(types.CreateMenuOptions{ Title: self.c.Tr.LcStashOptions, diff --git a/pkg/gui/controllers/global_controller.go b/pkg/gui/controllers/global_controller.go new file mode 100644 index 000000000..3560a0412 --- /dev/null +++ b/pkg/gui/controllers/global_controller.go @@ -0,0 +1,68 @@ +package controllers + +import ( + "github.com/jesseduffield/lazygit/pkg/commands/oscommands" + "github.com/jesseduffield/lazygit/pkg/gui/types" + "github.com/jesseduffield/lazygit/pkg/utils" +) + +type GlobalController struct { + c *types.ControllerCommon + os *oscommands.OSCommand +} + +func NewGlobalController( + c *types.ControllerCommon, + os *oscommands.OSCommand, +) *GlobalController { + return &GlobalController{ + c: c, + os: os, + } +} + +func (self *GlobalController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding { + return []*types.Binding{ + { + Key: opts.GetKey(opts.Config.Universal.ExecuteCustomCommand), + Handler: self.customCommand, + Description: self.c.Tr.LcExecuteCustomCommand, + }, + } +} + +func (self *GlobalController) customCommand() error { + return self.c.Prompt(types.PromptOpts{ + Title: self.c.Tr.CustomCommand, + FindSuggestionsFunc: self.GetCustomCommandsHistorySuggestionsFunc(), + HandleConfirm: func(command string) error { + self.c.GetAppState().CustomCommandsHistory = utils.Limit( + utils.Uniq( + append(self.c.GetAppState().CustomCommandsHistory, command), + ), + 1000, + ) + + err := self.c.SaveAppState() + if err != nil { + self.c.Log.Error(err) + } + + self.c.LogAction(self.c.Tr.Actions.CustomCommand) + return self.c.RunSubprocessAndRefresh( + self.os.Cmd.NewShell(command), + ) + }, + }) +} + +func (self *GlobalController) GetCustomCommandsHistorySuggestionsFunc() func(string) []*types.Suggestion { + // reversing so that we display the latest command first + history := utils.Reverse(self.c.GetAppState().CustomCommandsHistory) + + return FuzzySearchFunc(history) +} + +func (self *GlobalController) Context() types.Context { + return nil +} diff --git a/pkg/gui/controllers/suggestions_helper.go b/pkg/gui/controllers/suggestions_helper.go index 8a58e0e56..e696fdd8e 100644 --- a/pkg/gui/controllers/suggestions_helper.go +++ b/pkg/gui/controllers/suggestions_helper.go @@ -27,7 +27,6 @@ type ISuggestionsHelper interface { GetFilePathSuggestionsFunc() func(string) []*types.Suggestion GetRemoteBranchesSuggestionsFunc(separator string) func(string) []*types.Suggestion GetRefsSuggestionsFunc() func(string) []*types.Suggestion - GetCustomCommandsHistorySuggestionsFunc() func(string) []*types.Suggestion } type SuggestionsHelper struct { @@ -73,7 +72,7 @@ func matchesToSuggestions(matches []string) []*types.Suggestion { func (self *SuggestionsHelper) GetRemoteSuggestionsFunc() func(string) []*types.Suggestion { remoteNames := self.getRemoteNames() - return fuzzySearchFunc(remoteNames) + return FuzzySearchFunc(remoteNames) } func (self *SuggestionsHelper) getBranchNames() []string { @@ -172,7 +171,7 @@ func (self *SuggestionsHelper) getRemoteBranchNames(separator string) []string { } func (self *SuggestionsHelper) GetRemoteBranchesSuggestionsFunc(separator string) func(string) []*types.Suggestion { - return fuzzySearchFunc(self.getRemoteBranchNames(separator)) + return FuzzySearchFunc(self.getRemoteBranchNames(separator)) } func (self *SuggestionsHelper) getTagNames() []string { @@ -191,17 +190,10 @@ func (self *SuggestionsHelper) GetRefsSuggestionsFunc() func(string) []*types.Su refNames := append(append(append(remoteBranchNames, localBranchNames...), tagNames...), additionalRefNames...) - return fuzzySearchFunc(refNames) + return FuzzySearchFunc(refNames) } -func (self *SuggestionsHelper) GetCustomCommandsHistorySuggestionsFunc() func(string) []*types.Suggestion { - // reversing so that we display the latest command first - history := utils.Reverse(self.c.GetAppState().CustomCommandsHistory) - - return fuzzySearchFunc(history) -} - -func fuzzySearchFunc(options []string) func(string) []*types.Suggestion { +func FuzzySearchFunc(options []string) func(string) []*types.Suggestion { return func(input string) []*types.Suggestion { var matches []string if input == "" { diff --git a/pkg/gui/gui.go b/pkg/gui/gui.go index 116fcee2d..2b4fe66ca 100644 --- a/pkg/gui/gui.go +++ b/pkg/gui/gui.go @@ -222,6 +222,7 @@ type Controllers struct { Bisect *controllers.BisectController Undo *controllers.UndoController Sync *controllers.SyncController + Global *controllers.GlobalController } type listPanelState struct { @@ -591,6 +592,10 @@ func (gui *Gui) resetControllers() { gui.Controllers = Controllers{ Submodules: submodulesController, + Global: controllers.NewGlobalController( + controllerCommon, + osCommand, + ), Files: controllers.NewFilesController( controllerCommon, gui.State.Contexts.Files, @@ -674,6 +679,7 @@ func (gui *Gui) resetControllers() { } gui.State.Contexts.Submodules.AddKeybindingsFn(gui.Controllers.Submodules.GetKeybindings) + gui.Controllers.Files.Attach(gui.State.Contexts.Files) gui.State.Contexts.Files.AddKeybindingsFn(gui.Controllers.Files.GetKeybindings) gui.State.Contexts.Tags.AddKeybindingsFn(gui.Controllers.Tags.GetKeybindings) // TODO: commit to one name here: local commits or branch commits diff --git a/pkg/gui/keybindings.go b/pkg/gui/keybindings.go index 68b878a13..e72e604a1 100644 --- a/pkg/gui/keybindings.go +++ b/pkg/gui/keybindings.go @@ -1365,6 +1365,7 @@ func (gui *Gui) GetInitialKeybindings() []*types.Binding { for _, controller := range []types.IController{ gui.Controllers.Sync, gui.Controllers.Undo, + gui.Controllers.Global, } { context := controller.Context() viewName := ""