From b92ff3ee3fdde05ac84f2790e6b10a10db0c4d2a Mon Sep 17 00:00:00 2001 From: Elwardi Date: Mon, 19 Jul 2021 13:06:00 +0100 Subject: [PATCH] Consider first match only in menuFromCommand prompt --- docs/Custom_Command_Keybindings.md | 4 ++-- pkg/gui/custom_commands.go | 25 +++++++++++-------------- 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/docs/Custom_Command_Keybindings.md b/docs/Custom_Command_Keybindings.md index 77fb90be5..9dc42a9da 100644 --- a/docs/Custom_Command_Keybindings.md +++ b/docs/Custom_Command_Keybindings.md @@ -103,8 +103,8 @@ The permitted prompt fields are: | filter | (only applicable to 'menuFromCommand' prompts) the regexp to run specifying | yes | | | groups which are going to be kept from the command's output | | | format | (only applicable to 'menuFromCommand' prompts) how to format matched groups from | yes | -| | the filter. You can use named groups, or `{{ .group_GROUPID_MATCHID }}`. | yes | -| | PS: named groups keep last non-empty match | yes | +| | the filter. You can use named groups, or `{{ .group_GROUPID }}`. | yes | +| | PS: named groups keep first match only | yes | The permitted option fields are: | _field_ | _description_ | _required_ | diff --git a/pkg/gui/custom_commands.go b/pkg/gui/custom_commands.go index fbe92a72f..1ac92f49c 100644 --- a/pkg/gui/custom_commands.go +++ b/pkg/gui/custom_commands.go @@ -184,31 +184,28 @@ func (gui *Gui) handleCustomCommandKeybinding(customCommand config.CustomCommand // Need to make a menu out of what the cmd has displayed candidates := []string{} + buff := bytes.NewBuffer(nil) 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 - } + 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] = group + // Record last named group non-empty matches as group matches + if group != "" { + tmplData[group] = out[0][idx] } } } temp.Execute(buff, tmplData) candidates = append(candidates, strings.TrimSpace(buff.String())) + buff.Reset() } menuItems := make([]*menuItem, len(candidates))