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

Code before:
			                if err != nil {
			                	return gui.surfaceError(err)
			                }

					// Need to make a menu out of what the cmd has displayed
                                        var candidates []string
                                        reg := regexp.MustCompile(filter)
                                        for _,str := range strings.Split(string(message), "\n"){
                                            cand := ""
                                            if str != "" {
					        for i := 1; i < (reg.NumSubexp()+1); i++ {
                                                    keep := reg.ReplaceAllString(str, "${"+fmt.Sprint(i)+"}")
                                                    cand += keep
                                                }
                                                candidates = append(candidates, cand)
                                            }
                                        }

					menuItems := make([]*menuItem, len(candidates))
					for i, option := range candidates {
						option := option

						menuItems[i] = &menuItem{
							displayStrings: []string{option},
							onPress: func() error {
								promptResponses[idx] = option

								return wrappedF()
							},
						}
					}

					title, err := gui.resolveTemplate(prompt.Title, promptResponses)
					if err != nil {
						return gui.surfaceError(err)
					}

					return gui.createMenu(title, menuItems, createMenuOptions{showCancel: true})
				}
			default:
				return gui.createErrorPanel("custom command prompt must have a type of 'input', 'menu' or 'menuFromCommand'")
			}

		}

		return f()
	}
}

func (gui *Gui) GetCustomCommandKeybindings() []*Binding {
	bindings := []*Binding{}

Review comment (suggestion):
I think this is a bit better than `option := option`
```suggestion
					for i := range candidates {
						menuItems[i] = &menuItem{
							displayStrings: []string{candidates[i]},
							onPress: func() error {
								promptResponses[idx] = candidates[i]
```

Code after:
	message, err := gui.GitCommand.RunCommandWithOutput(cmdStr)
	if err != nil {
		return gui.surfaceError(err)
	}

	// Need to make a menu out of what the cmd has displayed
	candidates, err := gui.GenerateMenuCandidates(message, filter, prompt.Format)
	if err != nil {
		return gui.surfaceError(err)
	}

	menuItems := make([]*menuItem, len(candidates))
	for i := range candidates {
		menuItems[i] = &menuItem{
			displayStrings: []string{candidates[i]},
			onPress: func() error {
				promptResponses[responseIdx] = candidates[i]
				return wrappedF()
			},
		}
	}

	title, err := gui.resolveTemplate(prompt.Title, promptResponses)
	if err != nil {
		return gui.surfaceError(err)
	}

	return gui.createMenu(title, menuItems, createMenuOptions{showCancel: true})
}

func (gui *Gui) handleCustomCommandKeybinding(customCommand config.CustomCommand) func() error {
	return func() error {
		promptResponses := make([]string, len(customCommand.Prompts))

		f := func() error {
			cmdStr, err := gui.resolveTemplate(customCommand.Command, promptResponses)
			if err != nil {
				return gui.surfaceError(err)
			}

			if customCommand.Subprocess {
				return gui.runSubprocessWithSuspenseAndRefresh(gui.OSCommand.PrepareShellSubProcess(cmdStr))
			}

			loadingText := customCommand.LoadingText
			if loadingText == "" {
				loadingText = gui.Tr.LcRunningCustomCommandStatus
			}
			return gui.WithWaitingStatus(loadingText, func() error {
				if err := gui.OSCommand.WithSpan(gui.Tr.Spans.CustomCommand).RunShellCommand(cmdStr); err != nil {
					return gui.surfaceError(err)