mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-05-11 04:15:48 +02:00
Add option to format filter matches to menuFromCommand prompts
This commit is contained in:
parent
77e9ee64a4
commit
f1ced5539a
3 changed files with 35 additions and 9 deletions
|
@ -46,7 +46,8 @@ customCommands:
|
||||||
- type: 'menuFromCommand'
|
- type: 'menuFromCommand'
|
||||||
title: 'Remote branch:'
|
title: 'Remote branch:'
|
||||||
command: 'git branch -r --list {{index .PromptResponses 0}}/*'
|
command: 'git branch -r --list {{index .PromptResponses 0}}/*'
|
||||||
filter: '.*{{index .PromptResponses 0}}/(.*)'
|
filter: '.*{{index .PromptResponses 0}}/(?P<branch>.*)'
|
||||||
|
format: '{{ .branch }}'
|
||||||
```
|
```
|
||||||
|
|
||||||
Looking at the command assigned to the 'n' key, here's what the result looks like:
|
Looking at the command assigned to the 'n' key, here's what the result looks like:
|
||||||
|
@ -99,8 +100,11 @@ The permitted prompt fields are:
|
||||||
| options | (only applicable to 'menu' prompts) the options to display in the menu | no |
|
| options | (only applicable to 'menu' prompts) the options to display in the menu | no |
|
||||||
| command | (only applicable to 'menuFromCommand' prompts) the command to run to generate | yes |
|
| command | (only applicable to 'menuFromCommand' prompts) the command to run to generate | yes |
|
||||||
| | menu options | |
|
| | menu options | |
|
||||||
| filter | (only applicable to 'menuFromCommand' prompts) the regexp to run specify groups | yes |
|
| filter | (only applicable to 'menuFromCommand' prompts) the regexp to run specifying | yes |
|
||||||
| | which are going to be kept from the command's output | |
|
| | 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 permitted option fields are:
|
The permitted option fields are:
|
||||||
| _field_ | _description_ | _required_ |
|
| _field_ | _description_ | _required_ |
|
||||||
|
|
|
@ -284,6 +284,7 @@ type CustomCommandPrompt struct {
|
||||||
// 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"`
|
||||||
|
Format string `yaml:"format"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type CustomCommandMenuOption struct {
|
type CustomCommandMenuOption struct {
|
||||||
|
|
|
@ -1,10 +1,13 @@
|
||||||
package gui
|
package gui
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"errors"
|
||||||
"log"
|
"log"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"text/template"
|
||||||
|
|
||||||
"github.com/fatih/color"
|
"github.com/fatih/color"
|
||||||
"github.com/jesseduffield/gocui"
|
"github.com/jesseduffield/gocui"
|
||||||
|
@ -168,6 +171,10 @@ func (gui *Gui) handleCustomCommandKeybinding(customCommand config.CustomCommand
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return gui.surfaceError(err)
|
return gui.surfaceError(err)
|
||||||
}
|
}
|
||||||
|
reg, err := regexp.Compile(filter)
|
||||||
|
if err != nil {
|
||||||
|
return gui.surfaceError(errors.New("unable to parse filter regex, error: " + err.Error()))
|
||||||
|
}
|
||||||
|
|
||||||
// Run and save output
|
// Run and save output
|
||||||
message, err := gui.GitCommand.RunCommandWithOutput(cmdStr)
|
message, err := gui.GitCommand.RunCommandWithOutput(cmdStr)
|
||||||
|
@ -177,17 +184,31 @@ func (gui *Gui) handleCustomCommandKeybinding(customCommand config.CustomCommand
|
||||||
|
|
||||||
// Need to make a menu out of what the cmd has displayed
|
// Need to make a menu out of what the cmd has displayed
|
||||||
candidates := []string{}
|
candidates := []string{}
|
||||||
reg := regexp.MustCompile(filter)
|
temp := template.Must(template.New("format").Parse(prompt.Format))
|
||||||
for _, str := range strings.Split(string(message), "\n") {
|
for _, str := range strings.Split(string(message), "\n") {
|
||||||
cand := ""
|
|
||||||
if str == "" {
|
if str == "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
for i := 1; i < (reg.NumSubexp() + 1); i++ {
|
buff := bytes.NewBuffer(nil)
|
||||||
keep := reg.ReplaceAllString(str, "${"+strconv.Itoa(i)+"}")
|
groupNames := reg.SubexpNames()
|
||||||
cand += keep
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
candidates = append(candidates, cand)
|
temp.Execute(buff, tmplData)
|
||||||
|
candidates = append(candidates, strings.TrimSpace(buff.String()))
|
||||||
}
|
}
|
||||||
|
|
||||||
menuItems := make([]*menuItem, len(candidates))
|
menuItems := make([]*menuItem, len(candidates))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue