Repository: jesseduffield/lazygit
File: pkg/gui/controllers/global_controller.go (Go)

Code before:

func NewGlobalController(
	common *controllerCommon,
) *GlobalController {
	return &GlobalController{
		baseController:   baseController{},
		controllerCommon: common,
	}
}

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 !strings.HasPrefix(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.os.Cmd.NewShell(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)
}

Review comment (refactor):
I'd extract out `!strings.HasPrefix(command, " ")` into a method called `shouldSaveCommand(command)` to make things clearer, and I'd add a comment explaining that this mimics existing behaviour in shells (I didn't realise this upon first reviewing).

Code after:

func NewGlobalController(
	common *controllerCommon,
) *GlobalController {
	return &GlobalController{
		baseController:   baseController{},
		controllerCommon: common,
	}
}

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,
				)
			}

			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),
			)
		},
	})
}

// 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, " ")
}