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

Code before:

					return gui.createMenu(title, menuItems, createMenuOptions{showCancel: true})
				}
			case "menuFromCommand":
				f = func() error {
					// Collect cmd to run from config
					cmdStr, err := gui.resolveTemplate(prompt.Command, promptResponses)
					if err != nil {
						return gui.surfaceError(err)
					}

					// Collect Filter regexp
					filter, err := gui.resolveTemplate(prompt.Filter, promptResponses)
					if err != nil {
						return gui.surfaceError(err)
					}

					// Run and save output
					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 := []string{}
					reg := regexp.MustCompile(filter)
					for _, str := range strings.Split(string(message), "\n") {
						cand := ""
						if str == "" {
							continue
						}
						for i := 1; i < (reg.NumSubexp() + 1); i++ {
							keep := reg.ReplaceAllString(str, "${"+strconv.Itoa(i)+"}")
							cand += keep
						}
						candidates = append(candidates, cand)
					}

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

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

Review comment (suggestion):
As the filter is user input there is a change the regex is invalid and i would prefer to get a error message over a panic
```suggestion
					reg, err := regexp.Compile(filter)
					if err != nil {
						return gui.surfaceError(errors.New("unable to parse filter regex, error: " + err.Error()))
					}
```
*Note: you still need to import the `errors` package for this to work*

Code after:
		buff.Reset()
	}
	return candidates, err
}

func (gui *Gui) menuPromptFromCommand(prompt config.CustomCommandPrompt, promptResponses []string, responseIdx int, wrappedF func() error) error {
	// Collect cmd to run from config
	cmdStr, err := gui.resolveTemplate(prompt.Command, promptResponses)
	if err != nil {
		return gui.surfaceError(err)
	}

	// Collect Filter regexp
	filter, err := gui.resolveTemplate(prompt.Filter, promptResponses)
	if err != nil {
		return gui.surfaceError(err)
	}

	// Run and save output
	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 {