diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2022-02-05 11:00:57 +1100 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2022-03-17 19:13:40 +1100 |
commit | 8e3484d8e98faf12f8395eaf5f9e8381f77a8e52 (patch) | |
tree | 66334c4a08240e3f7dad0926bacea2a0e5b6def3 | |
parent | 226985bf7602763f7578ef236bdc4cec3a1494e9 (diff) |
add global controller
-rw-r--r-- | pkg/gui/controllers/files_controller.go | 31 | ||||
-rw-r--r-- | pkg/gui/controllers/global_controller.go | 68 | ||||
-rw-r--r-- | pkg/gui/controllers/suggestions_helper.go | 16 | ||||
-rw-r--r-- | pkg/gui/gui.go | 6 | ||||
-rw-r--r-- | pkg/gui/keybindings.go | 1 |
5 files changed, 79 insertions, 43 deletions
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 @@ -171,12 +171,6 @@ func (self *FilesController) GetKeybindings(opts types.KeybindingsOpts) []*types 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, Description: self.c.Tr.LcViewResetToUpstreamOptions, @@ -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 := "" |