1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-04-13 11:50:28 +02:00

Show only labels in menuFromCommand prompts

This commit is contained in:
Elwardi 2021-08-06 18:38:26 +01:00
parent 906ec30cac
commit dcd3b7c058
3 changed files with 38 additions and 30 deletions

View File

@ -103,14 +103,16 @@ The permitted prompt fields are:
| | menu options | |
| filter | (only applicable to 'menuFromCommand' prompts) the regexp to run specifying | yes |
| | groups which are going to be kept from the command's output | |
| valueFormat | (only applicable to 'menuFromCommand' prompts) how to format matched groups from | yes |
| | the filter to construct a menu item. You can use named groups, | yes |
| valueFormat | (only applicable to 'menuFromCommand' prompts) how to format matched groups from | yes |
| | the filter to construct a menu item's value (What gets appended to prompt | |
| | responses when the item is selected). You can use named groups, | |
| | or `{{ .group_GROUPID }}`. | |
| | PS: named groups keep first match only | yes |
| labelFormat | (only applicable to 'menuFromCommand' prompts) how to format matched groups from | yes |
| | the filter to construct a menu item's description. You can use named groups, | yes |
| | or `{{ .group_GROUPID }}`. | |
| | PS: named groups keep first match only | yes |
| | PS: named groups keep first match only | |
| labelFormat | (only applicable to 'menuFromCommand' prompts) how to format matched groups from | no |
| | the filter to construct the item's label (What's shown on screen). You can use | |
| | named groups, or `{{ .group_GROUPID }}`. If this is not specified, `valueFormat` | |
| | is shown instead. | |
| | PS: named groups keep first match only | |
The permitted option fields are:
| _field_ | _description_ | _required_ |

View File

@ -125,20 +125,24 @@ func (gui *Gui) menuPrompt(prompt config.CustomCommandPrompt, promptResponses []
func (gui *Gui) GenerateMenuCandidates(commandOutput, filter, valueFormat, labelFormat string) ([]CommandMenuEntry, error) {
candidates := []CommandMenuEntry{}
reg, err := regexp.Compile(filter)
if err != nil {
return candidates, gui.surfaceError(errors.New("unable to parse filter regex, error: " + err.Error()))
}
buffItem := bytes.NewBuffer(nil)
tempItem, err := template.New("format").Parse(valueFormat)
valueBuff := bytes.NewBuffer(nil)
valueTemp, err := template.New("format").Parse(valueFormat)
if err != nil {
return candidates, gui.surfaceError(errors.New("unable to parse item format, error: " + err.Error()))
return candidates, gui.surfaceError(errors.New("unable to parse value format, error: " + err.Error()))
}
buffDescr := bytes.NewBuffer(nil)
tempDescr, err := template.New("format").Parse(labelFormat)
descBuff := bytes.NewBuffer(nil)
descTemp, err := template.New("format").Parse(labelFormat)
if err != nil {
return candidates, gui.surfaceError(errors.New("unable to parse item description format, error: " + err.Error()))
return candidates, gui.surfaceError(errors.New("unable to parse label format, error: " + err.Error()))
}
for _, str := range strings.Split(string(commandOutput), "\n") {
if str == "" {
continue
@ -156,26 +160,29 @@ func (gui *Gui) GenerateMenuCandidates(commandOutput, filter, valueFormat, label
}
}
}
err = tempItem.Execute(buffItem, tmplData)
if err != nil {
return candidates, gui.surfaceError(err)
}
err = tempDescr.Execute(buffDescr, tmplData)
err = valueTemp.Execute(valueBuff, tmplData)
if err != nil {
return candidates, gui.surfaceError(err)
}
// Populate menu entry
// label formatted as labelFormat
// value as valueFormat
if labelFormat != "" {
err = descTemp.Execute(descBuff, tmplData)
if err != nil {
return candidates, gui.surfaceError(err)
}
} else {
descBuff.Write(valueBuff.Bytes())
}
entry := CommandMenuEntry{
strings.TrimSpace(buffDescr.String()),
//"Description",
strings.TrimSpace(buffItem.String()),
strings.TrimSpace(descBuff.String()),
strings.TrimSpace(valueBuff.String()),
}
candidates = append(candidates, entry)
buffItem.Reset()
buffDescr.Reset()
valueBuff.Reset()
descBuff.Reset()
}
return candidates, err
}
@ -208,8 +215,7 @@ func (gui *Gui) menuPromptFromCommand(prompt config.CustomCommandPrompt, promptR
menuItems := make([]*menuItem, len(candidates))
for i := range candidates {
menuItems[i] = &menuItem{
// Put in candidate and its description
displayStrings: []string{candidates[i].value, style.FgYellow.Sprint(candidates[i].label)},
displayStrings: []string{candidates[i].label},
onPress: func() error {
promptResponses[responseIdx] = candidates[i].value
return wrappedF()

View File

@ -105,7 +105,7 @@ func TestGuiGenerateMenuCandidates(t *testing.T) {
},
},
{
"Multiple named groups with empty description",
"Multiple named groups with empty labelFormat",
"upstream/pr-1",
"(?P<remote>[a-z]*)/(?P<branch>.*)",
"{{ .branch }}|{{ .remote }}",
@ -113,7 +113,7 @@ func TestGuiGenerateMenuCandidates(t *testing.T) {
func(actualEntry []CommandMenuEntry, err error) {
assert.NoError(t, err)
assert.EqualValues(t, "pr-1|upstream", actualEntry[0].value)
assert.EqualValues(t, "", actualEntry[0].label)
assert.EqualValues(t, "pr-1|upstream", actualEntry[0].label)
},
},
{