mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-07-30 03:23:08 +03:00
add global controller
This commit is contained in:
68
pkg/gui/controllers/global_controller.go
Normal file
68
pkg/gui/controllers/global_controller.go
Normal 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
|
||||
}
|
Reference in New Issue
Block a user