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

Code before:
	}
	for _, str := range strings.Split(string(commandOutput), "\n") {
		if str == "" {
			continue
		}
		tmplData := map[string]string{}
		out := reg.FindAllStringSubmatch(str, -1)
		if len(out) > 0 {
			for groupIdx, group := range reg.SubexpNames() {
				// Record matched group with group ids
				matchName := "group_" + strconv.Itoa(groupIdx)
				tmplData[matchName] = out[0][groupIdx]
				// Record last named group non-empty matches as group matches
				if group != "" {
					tmplData[group] = out[0][groupIdx]
				}
			}
		}
		err = tempItem.Execute(buffItem, tmplData)
		if err != nil {
			return candidates, gui.surfaceError(err)
		}
		err = tempDescr.Execute(buffDescr, tmplData)
		if err != nil {
			return candidates, gui.surfaceError(err)
		}

		// Populate menu entry
		// label formatted as labelFormat
		// value as valueFormat
		entry := CommandMenuEntry{
			strings.TrimSpace(buffDescr.String()),
			//"Description",
			strings.TrimSpace(buffItem.String()),
		}
		candidates = append(candidates, entry)
		buffItem.Reset()
		buffDescr.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)

Review comment (suggestion):
In some comment below i suggested to only use the label in the output
In that case we should fallback to the value of the label is not set.
```suggestion
		if labelFormat != "" {
			err = tempDescr.Execute(buffDescr, tmplData)
			if err != nil {
				return candidates, gui.surfaceError(err)
			}
		} else {
			buffDescr.Write(buffItem.Bytes())
		}
```
note that we also need to reflect this change in the docs

Code after:
		return nil, gui.surfaceError(errors.New("unable to parse label format, error: " + err.Error()))
	}

	candidates := []commandMenuEntry{}
	for _, str := range strings.Split(string(commandOutput), "\n") {
		if str == "" {
			continue
		}

		tmplData := map[string]string{}
		out := reg.FindAllStringSubmatch(str, -1)
		if len(out) > 0 {
			for groupIdx, group := range reg.SubexpNames() {
				// Record matched group with group ids
				matchName := "group_" + strconv.Itoa(groupIdx)
				tmplData[matchName] = out[0][groupIdx]
				// Record last named group non-empty matches as group matches
				if group != "" {
					tmplData[group] = out[0][groupIdx]
				}
			}
		}

		err = valueTemp.Execute(buff, tmplData)
		if err != nil {
			return candidates, gui.surfaceError(err)
		}
		entry := commandMenuEntry{
			value: strings.TrimSpace(buff.String()),
		}

		if labelFormat != "" {
			buff.Reset()
			err = descTemp.Execute(buff, tmplData)
			if err != nil {
				return candidates, gui.surfaceError(err)
			}
			entry.label = strings.TrimSpace(buff.String())
		} else {
			entry.label = entry.value
		}

		candidates = append(candidates, entry)

		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