summaryrefslogtreecommitdiffstats
path: root/pkg
diff options
context:
space:
mode:
authorElwardi <elwardifadeli@gmail.com>2021-07-17 18:02:11 +0100
committerElwardi <elwardifadeli@gmail.com>2021-07-18 10:36:00 +0100
commitd18c8c8dc39fa028949470064dc9a596762f7f09 (patch)
tree346839e2cf210e1e06d89d71718ba9fa64b38515 /pkg
parent1573a449f84846657b7ac9e07756caa9db548b0e (diff)
Add prompt type: menuFromCommand
Diffstat (limited to 'pkg')
-rw-r--r--pkg/config/user_config.go4
-rw-r--r--pkg/gui/custom_commands.go59
2 files changed, 62 insertions, 1 deletions
diff --git a/pkg/config/user_config.go b/pkg/config/user_config.go
index 4855bf816..0f0e50fab 100644
--- a/pkg/config/user_config.go
+++ b/pkg/config/user_config.go
@@ -280,6 +280,10 @@ type CustomCommandPrompt struct {
// this only applies to menus
Options []CustomCommandMenuOption
+
+ // 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 06be97da0..a17d30dd2 100644
--- a/pkg/gui/custom_commands.go
+++ b/pkg/gui/custom_commands.go
@@ -1,8 +1,10 @@
package gui
import (
+ "fmt"
"log"
"strings"
+ "regexp"
"github.com/fatih/color"
"github.com/jesseduffield/gocui"
@@ -153,8 +155,63 @@ func (gui *Gui) handleCustomCommandKeybinding(customCommand config.CustomCommand
return gui.createMenu(title, menuItems, createMenuOptions{showCancel: true})
}
+ 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)
+ }
+
+ // 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 := str
+ if str != "" {
+ for i := 1; i < (reg.NumSubexp()+1); i++ {
+ trim := reg.ReplaceAllString(str, "${"+fmt.Sprint(i)+"}")
+ cand = strings.Trim(cand, trim)
+ }
+ candidates = append(candidates, cand)
+ }
+ }
+
+ menuItems := make([]*menuItem, len(candidates))
+ for i, option := range candidates {
+ option := option
+
+ menuItems[i] = &menuItem{
+ displayStrings: []string{option},
+ onPress: func() error {
+ promptResponses[idx] = option
+
+ return wrappedF()
+ },
+ }
+ }
+
+ title, err := gui.resolveTemplate(prompt.Title, promptResponses)
+ if err != nil {
+ return gui.surfaceError(err)
+ }
+
+ return gui.createMenu(title, menuItems, createMenuOptions{showCancel: true})
+ }
default:
- return gui.createErrorPanel("custom command prompt must have a type of 'input' or 'menu'")
+ return gui.createErrorPanel("custom command prompt must have a type of 'input', 'menu' or 'menuFromCommand'")
}
}