mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-04-25 12:24:47 +02:00
Show only labels in menuFromCommand prompts
This commit is contained in:
parent
906ec30cac
commit
dcd3b7c058
@ -104,13 +104,15 @@ The permitted prompt fields are:
|
|||||||
| filter | (only applicable to 'menuFromCommand' prompts) the regexp to run specifying | yes |
|
| filter | (only applicable to 'menuFromCommand' prompts) the regexp to run specifying | yes |
|
||||||
| | groups which are going to be kept from the command's output | |
|
| | groups which are going to be kept from the command's output | |
|
||||||
| valueFormat | (only applicable to 'menuFromCommand' prompts) how to format matched groups from | yes |
|
| valueFormat | (only applicable to 'menuFromCommand' prompts) how to format matched groups from | yes |
|
||||||
| | the filter to construct a menu item. You can use named groups, | yes |
|
| | the filter to construct a menu item's value (What gets appended to prompt | |
|
||||||
|
| | responses when the item is selected). You can use named groups, | |
|
||||||
| | or `{{ .group_GROUPID }}`. | |
|
| | or `{{ .group_GROUPID }}`. | |
|
||||||
| | PS: named groups keep first match only | yes |
|
| | PS: named groups keep first match only | |
|
||||||
| labelFormat | (only applicable to 'menuFromCommand' prompts) how to format matched groups from | yes |
|
| labelFormat | (only applicable to 'menuFromCommand' prompts) how to format matched groups from | no |
|
||||||
| | the filter to construct a menu item's description. You can use named groups, | yes |
|
| | the filter to construct the item's label (What's shown on screen). You can use | |
|
||||||
| | or `{{ .group_GROUPID }}`. | |
|
| | named groups, or `{{ .group_GROUPID }}`. If this is not specified, `valueFormat` | |
|
||||||
| | PS: named groups keep first match only | yes |
|
| | is shown instead. | |
|
||||||
|
| | PS: named groups keep first match only | |
|
||||||
|
|
||||||
The permitted option fields are:
|
The permitted option fields are:
|
||||||
| _field_ | _description_ | _required_ |
|
| _field_ | _description_ | _required_ |
|
||||||
|
@ -125,20 +125,24 @@ func (gui *Gui) menuPrompt(prompt config.CustomCommandPrompt, promptResponses []
|
|||||||
|
|
||||||
func (gui *Gui) GenerateMenuCandidates(commandOutput, filter, valueFormat, labelFormat string) ([]CommandMenuEntry, error) {
|
func (gui *Gui) GenerateMenuCandidates(commandOutput, filter, valueFormat, labelFormat string) ([]CommandMenuEntry, error) {
|
||||||
candidates := []CommandMenuEntry{}
|
candidates := []CommandMenuEntry{}
|
||||||
|
|
||||||
reg, err := regexp.Compile(filter)
|
reg, err := regexp.Compile(filter)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return candidates, gui.surfaceError(errors.New("unable to parse filter regex, error: " + err.Error()))
|
return candidates, gui.surfaceError(errors.New("unable to parse filter regex, error: " + err.Error()))
|
||||||
}
|
}
|
||||||
buffItem := bytes.NewBuffer(nil)
|
|
||||||
tempItem, err := template.New("format").Parse(valueFormat)
|
valueBuff := bytes.NewBuffer(nil)
|
||||||
|
valueTemp, err := template.New("format").Parse(valueFormat)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return candidates, gui.surfaceError(errors.New("unable to parse item format, error: " + err.Error()))
|
return candidates, gui.surfaceError(errors.New("unable to parse value format, error: " + err.Error()))
|
||||||
}
|
}
|
||||||
buffDescr := bytes.NewBuffer(nil)
|
|
||||||
tempDescr, err := template.New("format").Parse(labelFormat)
|
descBuff := bytes.NewBuffer(nil)
|
||||||
|
descTemp, err := template.New("format").Parse(labelFormat)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return candidates, gui.surfaceError(errors.New("unable to parse item description format, error: " + err.Error()))
|
return candidates, gui.surfaceError(errors.New("unable to parse label format, error: " + err.Error()))
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, str := range strings.Split(string(commandOutput), "\n") {
|
for _, str := range strings.Split(string(commandOutput), "\n") {
|
||||||
if str == "" {
|
if str == "" {
|
||||||
continue
|
continue
|
||||||
@ -156,26 +160,29 @@ func (gui *Gui) GenerateMenuCandidates(commandOutput, filter, valueFormat, label
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
err = tempItem.Execute(buffItem, tmplData)
|
|
||||||
if err != nil {
|
err = valueTemp.Execute(valueBuff, tmplData)
|
||||||
return candidates, gui.surfaceError(err)
|
|
||||||
}
|
|
||||||
err = tempDescr.Execute(buffDescr, tmplData)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return candidates, gui.surfaceError(err)
|
return candidates, gui.surfaceError(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Populate menu entry
|
if labelFormat != "" {
|
||||||
// label formatted as labelFormat
|
err = descTemp.Execute(descBuff, tmplData)
|
||||||
// value as valueFormat
|
if err != nil {
|
||||||
|
return candidates, gui.surfaceError(err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
descBuff.Write(valueBuff.Bytes())
|
||||||
|
}
|
||||||
|
|
||||||
entry := CommandMenuEntry{
|
entry := CommandMenuEntry{
|
||||||
strings.TrimSpace(buffDescr.String()),
|
strings.TrimSpace(descBuff.String()),
|
||||||
//"Description",
|
strings.TrimSpace(valueBuff.String()),
|
||||||
strings.TrimSpace(buffItem.String()),
|
|
||||||
}
|
}
|
||||||
candidates = append(candidates, entry)
|
candidates = append(candidates, entry)
|
||||||
buffItem.Reset()
|
|
||||||
buffDescr.Reset()
|
valueBuff.Reset()
|
||||||
|
descBuff.Reset()
|
||||||
}
|
}
|
||||||
return candidates, err
|
return candidates, err
|
||||||
}
|
}
|
||||||
@ -208,8 +215,7 @@ func (gui *Gui) menuPromptFromCommand(prompt config.CustomCommandPrompt, promptR
|
|||||||
menuItems := make([]*menuItem, len(candidates))
|
menuItems := make([]*menuItem, len(candidates))
|
||||||
for i := range candidates {
|
for i := range candidates {
|
||||||
menuItems[i] = &menuItem{
|
menuItems[i] = &menuItem{
|
||||||
// Put in candidate and its description
|
displayStrings: []string{candidates[i].label},
|
||||||
displayStrings: []string{candidates[i].value, style.FgYellow.Sprint(candidates[i].label)},
|
|
||||||
onPress: func() error {
|
onPress: func() error {
|
||||||
promptResponses[responseIdx] = candidates[i].value
|
promptResponses[responseIdx] = candidates[i].value
|
||||||
return wrappedF()
|
return wrappedF()
|
||||||
|
@ -105,7 +105,7 @@ func TestGuiGenerateMenuCandidates(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"Multiple named groups with empty description",
|
"Multiple named groups with empty labelFormat",
|
||||||
"upstream/pr-1",
|
"upstream/pr-1",
|
||||||
"(?P<remote>[a-z]*)/(?P<branch>.*)",
|
"(?P<remote>[a-z]*)/(?P<branch>.*)",
|
||||||
"{{ .branch }}|{{ .remote }}",
|
"{{ .branch }}|{{ .remote }}",
|
||||||
@ -113,7 +113,7 @@ func TestGuiGenerateMenuCandidates(t *testing.T) {
|
|||||||
func(actualEntry []CommandMenuEntry, err error) {
|
func(actualEntry []CommandMenuEntry, err error) {
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.EqualValues(t, "pr-1|upstream", actualEntry[0].value)
|
assert.EqualValues(t, "pr-1|upstream", actualEntry[0].value)
|
||||||
assert.EqualValues(t, "", actualEntry[0].label)
|
assert.EqualValues(t, "pr-1|upstream", actualEntry[0].label)
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user