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

Review comment (suggestion):
I think this is a more go'ish way of doing this  
```suggestion
                                        candidates := []string{}
```

Code after:
		candidates = append(candidates, strings.TrimSpace(buff.String()))
		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 {