1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-06-21 00:30:00 +02:00

Apply suggestions from @mjarkk for menyFromCommands

This commit is contained in:
Elwardi
2021-07-18 18:38:06 +01:00
parent 9daa47fb2d
commit 77e9ee64a4
2 changed files with 36 additions and 38 deletions

View File

@ -281,9 +281,9 @@ type CustomCommandPrompt struct {
// this only applies to menus // this only applies to menus
Options []CustomCommandMenuOption Options []CustomCommandMenuOption
// this only applies to menuFromCommand // this only applies to menuFromCommand
Command string `yaml:"command"` Command string `yaml:"command"`
Filter string `yaml:"filter"` Filter string `yaml:"filter"`
} }
type CustomCommandMenuOption struct { type CustomCommandMenuOption struct {

View File

@ -1,10 +1,10 @@
package gui package gui
import ( import (
"fmt"
"log" "log"
"regexp"
"strconv"
"strings" "strings"
"regexp"
"github.com/fatih/color" "github.com/fatih/color"
"github.com/jesseduffield/gocui" "github.com/jesseduffield/gocui"
@ -157,47 +157,45 @@ func (gui *Gui) handleCustomCommandKeybinding(customCommand config.CustomCommand
} }
case "menuFromCommand": case "menuFromCommand":
f = func() error { f = func() error {
// Collect cmd to run from config // Collect cmd to run from config
cmdStr, err := gui.resolveTemplate(prompt.Command, promptResponses) cmdStr, err := gui.resolveTemplate(prompt.Command, promptResponses)
if err != nil { if err != nil {
return gui.surfaceError(err) return gui.surfaceError(err)
} }
// Collect Filter regexp // Collect Filter regexp
filter, err := gui.resolveTemplate(prompt.Filter, promptResponses) filter, err := gui.resolveTemplate(prompt.Filter, promptResponses)
if err != nil { if err != nil {
return gui.surfaceError(err) return gui.surfaceError(err)
} }
// Run and save output // Run and save output
message,err := gui.GitCommand.RunCommandWithOutput(cmdStr) message, err := gui.GitCommand.RunCommandWithOutput(cmdStr)
if err != nil { if err != nil {
return gui.surfaceError(err) return gui.surfaceError(err)
} }
// Need to make a menu out of what the cmd has displayed // Need to make a menu out of what the cmd has displayed
var candidates []string candidates := []string{}
reg := regexp.MustCompile(filter) reg := regexp.MustCompile(filter)
for _,str := range strings.Split(string(message), "\n"){ for _, str := range strings.Split(string(message), "\n") {
cand := "" cand := ""
if str != "" { if str == "" {
for i := 1; i < (reg.NumSubexp()+1); i++ { continue
keep := reg.ReplaceAllString(str, "${"+fmt.Sprint(i)+"}") }
cand += keep for i := 1; i < (reg.NumSubexp() + 1); i++ {
} keep := reg.ReplaceAllString(str, "${"+strconv.Itoa(i)+"}")
candidates = append(candidates, cand) cand += keep
} }
} candidates = append(candidates, cand)
}
menuItems := make([]*menuItem, len(candidates)) menuItems := make([]*menuItem, len(candidates))
for i, option := range candidates { for i := range candidates {
option := option
menuItems[i] = &menuItem{ menuItems[i] = &menuItem{
displayStrings: []string{option}, displayStrings: []string{candidates[i]},
onPress: func() error { onPress: func() error {
promptResponses[idx] = option promptResponses[idx] = candidates[i]
return wrappedF() return wrappedF()
}, },
} }