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

Code before:
					// Collect Filter regexp
					filter, err := gui.resolveTemplate(prompt.Filter, promptResponses)
					if err != nil {
						return gui.surfaceError(err)
					}
					reg, err := regexp.Compile(filter)
					if err != nil {
						return gui.surfaceError(errors.New("unable to parse filter regex, error: " + err.Error()))
					}

					// 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{}
					temp := template.Must(template.New("format").Parse(prompt.Format))
					for _, str := range strings.Split(string(message), "\n") {
						if str == "" {
							continue
						}
						buff := bytes.NewBuffer(nil)
						groupNames := reg.SubexpNames()
						tmplData := map[string]string{}
						for matchNum, match := range reg.FindAllStringSubmatch(str, -1) {
							if len(match) > 0 {
								for groupIdx, group := range match {
									// Record matched group with group and match ids
									matchName := "group_" + strconv.Itoa(groupIdx) + "_" + strconv.Itoa(matchNum)
									tmplData[matchName] = group
									// Record last named group non-empty matches as group matches
									name := groupNames[groupIdx]
									_, ok := tmplData[name]
									if name != "" && group != "" && !ok {
										tmplData[name] = group
									}
								}
							}
						}
						temp.Execute(buff, tmplData)
						candidates = append(candidates, strings.TrimSpace(buff.String()))
					}

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

Review comment (suggestion):
Resetting the buffer is faster than creating a new buffer every loop iteration  
Also groupNames doesn't change for every iteration of the for loop
```suggestion
					buff := bytes.NewBuffer(nil)
					groupNames := reg.SubexpNames()
					for _, str := range strings.Split(string(message), "\n") {
						if str == "" {
							continue
						}
						buff.Reset()
						tmplData := map[string]string{}
```

Code after:
	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 {
		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 == "" {