diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2023-03-23 21:35:11 +1100 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2023-04-30 13:19:53 +1000 |
commit | 820b1e811d09fce5b0e578a7a304cca9c781d42a (patch) | |
tree | 10eee2a7ce8353234da2d3c692450b8fbe396b7a /pkg | |
parent | 4a33fede7bd74451db1e220eb0eb8649b03cbd0a (diff) |
move custom command action into its own file
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/gui/controllers/custom_command_action.go | 53 | ||||
-rw-r--r-- | pkg/gui/controllers/global_controller.go | 42 |
2 files changed, 54 insertions, 41 deletions
diff --git a/pkg/gui/controllers/custom_command_action.go b/pkg/gui/controllers/custom_command_action.go new file mode 100644 index 000000000..4b3aaa885 --- /dev/null +++ b/pkg/gui/controllers/custom_command_action.go @@ -0,0 +1,53 @@ +package controllers + +import ( + "strings" + + "github.com/jesseduffield/generics/slices" + "github.com/jesseduffield/lazygit/pkg/gui/controllers/helpers" + "github.com/jesseduffield/lazygit/pkg/gui/types" + "github.com/jesseduffield/lazygit/pkg/utils" + "github.com/samber/lo" +) + +type CustomCommandAction struct { + c *ControllerCommon +} + +func (self *CustomCommandAction) Call() 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, + ) + } + + 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.c.OS().Cmd.NewShell(command), + ) + }, + }) +} + +func (self *CustomCommandAction) GetCustomCommandsHistorySuggestionsFunc() func(string) []*types.Suggestion { + // reversing so that we display the latest command first + history := slices.Reverse(self.c.GetAppState().CustomCommandsHistory) + + return helpers.FuzzySearchFunc(history) +} + +// this mimics the shell functionality `ignorespace` +// which doesn't save a command to history if it starts with a space +func (self *CustomCommandAction) shouldSaveCommand(command string) bool { + return !strings.HasPrefix(command, " ") +} diff --git a/pkg/gui/controllers/global_controller.go b/pkg/gui/controllers/global_controller.go index 1f5d26e3f..6a901d738 100644 --- a/pkg/gui/controllers/global_controller.go +++ b/pkg/gui/controllers/global_controller.go @@ -1,13 +1,7 @@ package controllers import ( - "strings" - - "github.com/jesseduffield/generics/slices" - "github.com/jesseduffield/lazygit/pkg/gui/controllers/helpers" "github.com/jesseduffield/lazygit/pkg/gui/types" - "github.com/jesseduffield/lazygit/pkg/utils" - "github.com/samber/lo" ) type GlobalController struct { @@ -35,41 +29,7 @@ func (self *GlobalController) GetKeybindings(opts types.KeybindingsOpts) []*type } 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, - ) - } - - 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.c.OS().Cmd.NewShell(command), - ) - }, - }) -} - -// 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, " ") -} - -func (self *GlobalController) GetCustomCommandsHistorySuggestionsFunc() func(string) []*types.Suggestion { - // reversing so that we display the latest command first - history := slices.Reverse(self.c.GetAppState().CustomCommandsHistory) - - return helpers.FuzzySearchFunc(history) + return (&CustomCommandAction{c: self.c}).Call() } func (self *GlobalController) Context() types.Context { |