From dcd3b7c058f9c1289234c2ec174d6dd9802e96e2 Mon Sep 17 00:00:00 2001 From: Elwardi Date: Fri, 6 Aug 2021 18:38:26 +0100 Subject: [PATCH] Show only labels in menuFromCommand prompts --- docs/Custom_Command_Keybindings.md | 16 +++++----- pkg/gui/custom_commands.go | 48 +++++++++++++++++------------- pkg/gui/gui_test.go | 4 +-- 3 files changed, 38 insertions(+), 30 deletions(-) diff --git a/docs/Custom_Command_Keybindings.md b/docs/Custom_Command_Keybindings.md index 9a5c89609..c48442362 100644 --- a/docs/Custom_Command_Keybindings.md +++ b/docs/Custom_Command_Keybindings.md @@ -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_ | diff --git a/pkg/gui/custom_commands.go b/pkg/gui/custom_commands.go index bad6f3c36..bac65801d 100644 --- a/pkg/gui/custom_commands.go +++ b/pkg/gui/custom_commands.go @@ -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() diff --git a/pkg/gui/gui_test.go b/pkg/gui/gui_test.go index d4e0415c7..e3989ba24 100644 --- a/pkg/gui/gui_test.go +++ b/pkg/gui/gui_test.go @@ -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[a-z]*)/(?P.*)", "{{ .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) }, }, {