diff options
author | Elwardi <elwardifadeli@gmail.com> | 2021-07-18 18:38:06 +0100 |
---|---|---|
committer | Elwardi <elwardifadeli@gmail.com> | 2021-07-18 18:42:42 +0100 |
commit | 77e9ee64a45bbee3f0e2367a8670b2e7d6a59c6f (patch) | |
tree | a8fccbeb30057de17f7e2f8ecda3cec26ba83a12 /pkg | |
parent | 9daa47fb2df8b58a3edc82ed48687205c2bd85e9 (diff) |
Apply suggestions from @mjarkk for menyFromCommands
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/config/user_config.go | 6 | ||||
-rw-r--r-- | pkg/gui/custom_commands.go | 72 |
2 files changed, 38 insertions, 40 deletions
diff --git a/pkg/config/user_config.go b/pkg/config/user_config.go index 0f0e50fab..f98bde28e 100644 --- a/pkg/config/user_config.go +++ b/pkg/config/user_config.go @@ -281,9 +281,9 @@ type CustomCommandPrompt struct { // this only applies to menus Options []CustomCommandMenuOption - // this only applies to menuFromCommand - Command string `yaml:"command"` - Filter string `yaml:"filter"` + // this only applies to menuFromCommand + Command string `yaml:"command"` + Filter string `yaml:"filter"` } type CustomCommandMenuOption struct { diff --git a/pkg/gui/custom_commands.go b/pkg/gui/custom_commands.go index a07bd396b..7a70eeb95 100644 --- a/pkg/gui/custom_commands.go +++ b/pkg/gui/custom_commands.go @@ -1,10 +1,10 @@ package gui import ( - "fmt" "log" + "regexp" + "strconv" "strings" - "regexp" "github.com/fatih/color" "github.com/jesseduffield/gocui" @@ -157,47 +157,45 @@ func (gui *Gui) handleCustomCommandKeybinding(customCommand config.CustomCommand } case "menuFromCommand": f = func() 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) - if err != nil { - return gui.surfaceError(err) - } - - // Run and save output - message,err := gui.GitCommand.RunCommandWithOutput(cmdStr) - if err != nil { - return gui.surfaceError(err) - } + // 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) + 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 - var candidates []string - reg := regexp.MustCompile(filter) - for _,str := range strings.Split(string(message), "\n"){ - cand := "" - if str != "" { - for i := 1; i < (reg.NumSubexp()+1); i++ { - keep := reg.ReplaceAllString(str, "${"+fmt.Sprint(i)+"}") - cand += keep - } - candidates = append(candidates, cand) - } - } + candidates := []string{} + reg := regexp.MustCompile(filter) + for _, str := range strings.Split(string(message), "\n") { + cand := "" + if str == "" { + continue + } + for i := 1; i < (reg.NumSubexp() + 1); i++ { + keep := reg.ReplaceAllString(str, "${"+strconv.Itoa(i)+"}") + cand += keep + } + candidates = append(candidates, cand) + } menuItems := make([]*menuItem, len(candidates)) - for i, option := range candidates { - option := option - + for i := range candidates { menuItems[i] = &menuItem{ - displayStrings: []string{option}, + displayStrings: []string{candidates[i]}, onPress: func() error { - promptResponses[idx] = option - + promptResponses[idx] = candidates[i] return wrappedF() }, } |