1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-02-03 13:21:56 +02:00

add global controller

This commit is contained in:
Jesse Duffield 2022-02-05 11:00:57 +11:00
parent 226985bf76
commit 8e3484d8e9
5 changed files with 79 additions and 43 deletions

View File

@ -170,12 +170,6 @@ func (self *FilesController) GetKeybindings(opts types.KeybindingsOpts) []*types
Handler: self.enter, Handler: self.enter,
Description: self.c.Tr.FileEnter, 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), Key: opts.GetKey(opts.Config.Commits.ViewResetOptions),
Handler: self.createResetMenu, Handler: self.createResetMenu,
@ -577,31 +571,6 @@ func (self *FilesController) switchToMerge() error {
return self.switchToMergeFn(file.Name) 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 { func (self *FilesController) createStashMenu() error {
return self.c.Menu(types.CreateMenuOptions{ return self.c.Menu(types.CreateMenuOptions{
Title: self.c.Tr.LcStashOptions, Title: self.c.Tr.LcStashOptions,

View File

@ -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
}

View File

@ -27,7 +27,6 @@ type ISuggestionsHelper interface {
GetFilePathSuggestionsFunc() func(string) []*types.Suggestion GetFilePathSuggestionsFunc() func(string) []*types.Suggestion
GetRemoteBranchesSuggestionsFunc(separator string) func(string) []*types.Suggestion GetRemoteBranchesSuggestionsFunc(separator string) func(string) []*types.Suggestion
GetRefsSuggestionsFunc() func(string) []*types.Suggestion GetRefsSuggestionsFunc() func(string) []*types.Suggestion
GetCustomCommandsHistorySuggestionsFunc() func(string) []*types.Suggestion
} }
type SuggestionsHelper struct { type SuggestionsHelper struct {
@ -73,7 +72,7 @@ func matchesToSuggestions(matches []string) []*types.Suggestion {
func (self *SuggestionsHelper) GetRemoteSuggestionsFunc() func(string) []*types.Suggestion { func (self *SuggestionsHelper) GetRemoteSuggestionsFunc() func(string) []*types.Suggestion {
remoteNames := self.getRemoteNames() remoteNames := self.getRemoteNames()
return fuzzySearchFunc(remoteNames) return FuzzySearchFunc(remoteNames)
} }
func (self *SuggestionsHelper) getBranchNames() []string { 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 { 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 { 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...) refNames := append(append(append(remoteBranchNames, localBranchNames...), tagNames...), additionalRefNames...)
return fuzzySearchFunc(refNames) return FuzzySearchFunc(refNames)
} }
func (self *SuggestionsHelper) GetCustomCommandsHistorySuggestionsFunc() func(string) []*types.Suggestion { func FuzzySearchFunc(options []string) 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 {
return func(input string) []*types.Suggestion { return func(input string) []*types.Suggestion {
var matches []string var matches []string
if input == "" { if input == "" {

View File

@ -222,6 +222,7 @@ type Controllers struct {
Bisect *controllers.BisectController Bisect *controllers.BisectController
Undo *controllers.UndoController Undo *controllers.UndoController
Sync *controllers.SyncController Sync *controllers.SyncController
Global *controllers.GlobalController
} }
type listPanelState struct { type listPanelState struct {
@ -591,6 +592,10 @@ func (gui *Gui) resetControllers() {
gui.Controllers = Controllers{ gui.Controllers = Controllers{
Submodules: submodulesController, Submodules: submodulesController,
Global: controllers.NewGlobalController(
controllerCommon,
osCommand,
),
Files: controllers.NewFilesController( Files: controllers.NewFilesController(
controllerCommon, controllerCommon,
gui.State.Contexts.Files, gui.State.Contexts.Files,
@ -674,6 +679,7 @@ func (gui *Gui) resetControllers() {
} }
gui.State.Contexts.Submodules.AddKeybindingsFn(gui.Controllers.Submodules.GetKeybindings) 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.Files.AddKeybindingsFn(gui.Controllers.Files.GetKeybindings)
gui.State.Contexts.Tags.AddKeybindingsFn(gui.Controllers.Tags.GetKeybindings) gui.State.Contexts.Tags.AddKeybindingsFn(gui.Controllers.Tags.GetKeybindings)
// TODO: commit to one name here: local commits or branch commits // TODO: commit to one name here: local commits or branch commits

View File

@ -1365,6 +1365,7 @@ func (gui *Gui) GetInitialKeybindings() []*types.Binding {
for _, controller := range []types.IController{ for _, controller := range []types.IController{
gui.Controllers.Sync, gui.Controllers.Sync,
gui.Controllers.Undo, gui.Controllers.Undo,
gui.Controllers.Global,
} { } {
context := controller.Context() context := controller.Context()
viewName := "" viewName := ""