1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-14 11:23:09 +02:00
lazygit/pkg/gui/controllers/global_controller.go

78 lines
2.0 KiB
Go
Raw Normal View History

2022-02-05 02:00:57 +02:00
package controllers
import (
"strings"
2022-03-19 06:36:46 +02:00
"github.com/jesseduffield/generics/slices"
2022-02-06 06:54:26 +02:00
"github.com/jesseduffield/lazygit/pkg/gui/controllers/helpers"
2022-02-05 02:00:57 +02:00
"github.com/jesseduffield/lazygit/pkg/gui/types"
"github.com/jesseduffield/lazygit/pkg/utils"
2022-03-19 03:26:30 +02:00
"github.com/samber/lo"
2022-02-05 02:00:57 +02:00
)
type GlobalController struct {
2022-02-05 05:42:56 +02:00
baseController
2022-02-06 06:54:26 +02:00
*controllerCommon
2022-02-05 02:00:57 +02:00
}
func NewGlobalController(
2022-02-06 06:54:26 +02:00
common *controllerCommon,
2022-02-05 02:00:57 +02:00
) *GlobalController {
return &GlobalController{
2022-02-06 06:54:26 +02:00
baseController: baseController{},
controllerCommon: common,
2022-02-05 02:00:57 +02:00
}
}
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 {
if self.shouldSaveCommand(command) {
self.c.GetAppState().CustomCommandsHistory = utils.Limit(
lo.Uniq(append(self.c.GetAppState().CustomCommandsHistory, command)),
1000,
)
}
2022-02-05 02:00:57 +02:00
err := self.c.SaveAppState()
if err != nil {
self.c.Log.Error(err)
}
self.c.LogAction(self.c.Tr.Actions.CustomCommand)
return self.c.RunSubprocessAndRefresh(
2023-03-23 04:04:57 +02:00
self.c.OS().Cmd.NewShell(command),
2022-02-05 02:00:57 +02:00
)
},
})
}
// this mimics the shell functionality `ignorespace`
// which doesn't save a command to history if it starts with a space
func (self *GlobalController) shouldSaveCommand(command string) bool {
return !strings.HasPrefix(command, " ")
}
2022-02-05 02:00:57 +02:00
func (self *GlobalController) GetCustomCommandsHistorySuggestionsFunc() func(string) []*types.Suggestion {
// reversing so that we display the latest command first
2022-03-19 06:36:46 +02:00
history := slices.Reverse(self.c.GetAppState().CustomCommandsHistory)
2022-02-05 02:00:57 +02:00
2022-02-06 06:54:26 +02:00
return helpers.FuzzySearchFunc(history)
2022-02-05 02:00:57 +02:00
}
func (self *GlobalController) Context() types.Context {
return nil
}