Add property outputTitle to CustomCommand (#3579)

- **PR Description**

Add property `outputTitle` to CustomCommand. It can optionally be used
to set the title of the panel that shows the output of a command (when
`showOutput` is true). If left unset, the command string is used as the
title.

Closes #3576.
This commit is contained in:
Stefan Haller 2024-05-20 21:05:58 +02:00 committed by GitHub
commit 0fbed71f15
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 74 additions and 1 deletions

View file

@ -59,6 +59,7 @@ For a given custom command, here are the allowed fields:
| description | Label for the custom command when displayed in the keybindings menu | no |
| stream | Whether you want to stream the command's output to the Command Log panel | no |
| showOutput | Whether you want to show the command's output in a popup within Lazygit | no |
| outputTitle | The title to display in the popup panel if showOutput is true. If left unset, the command will be used as the title. | no |
| after | Actions to take after the command has completed | no |
Here are the options for the `after` key:

View file

@ -581,6 +581,8 @@ type CustomCommand struct {
Stream bool `yaml:"stream"`
// If true, show the command's output in a popup within Lazygit
ShowOutput bool `yaml:"showOutput"`
// The title to display in the popup panel if showOutput is true. If left unset, the command will be used as the title.
OutputTitle string `yaml:"outputTitle"`
// Actions to take after the command has completed
After CustomCommandAfterHook `yaml:"after"`
}

View file

@ -292,7 +292,15 @@ func (self *HandlerCreator) finalHandler(customCommand config.CustomCommand, ses
if strings.TrimSpace(output) == "" {
output = self.c.Tr.EmptyOutput
}
return self.c.Alert(cmdStr, output)
title := cmdStr
if customCommand.OutputTitle != "" {
title, err = resolveTemplate(customCommand.OutputTitle)
if err != nil {
return err
}
}
return self.c.Alert(title, output)
}
return nil

View file

@ -0,0 +1,57 @@
package custom_commands
import (
"fmt"
"github.com/jesseduffield/lazygit/pkg/config"
. "github.com/jesseduffield/lazygit/pkg/integration/components"
)
var ShowOutputInPanel = NewIntegrationTest(NewIntegrationTestArgs{
Description: "Run a command and show the output in a panel",
ExtraCmdArgs: []string{},
Skip: false,
SetupRepo: func(shell *Shell) {
shell.EmptyCommit("my change")
},
SetupConfig: func(cfg *config.AppConfig) {
cfg.UserConfig.CustomCommands = []config.CustomCommand{
{
Key: "X",
Context: "commits",
Command: "printf '%s' '{{ .SelectedLocalCommit.Name }}'",
ShowOutput: true,
},
{
Key: "Y",
Context: "commits",
Command: "printf '%s' '{{ .SelectedLocalCommit.Name }}'",
ShowOutput: true,
OutputTitle: "Subject of commit {{ .SelectedLocalCommit.Hash }}",
},
}
},
Run: func(t *TestDriver, keys config.KeybindingConfig) {
t.Views().Commits().
Focus().
Lines(
Contains("my change").IsSelected(),
).
Press("X")
t.ExpectPopup().Alert().
// Uses cmd string as title if no outputTitle is provided
Title(Equals("printf '%s' 'my change'")).
Content(Equals("my change")).
Confirm()
t.Views().Commits().
Press("Y")
hash := t.Git().GetCommitHash("HEAD")
t.ExpectPopup().Alert().
// Uses provided outputTitle with template fields resolved
Title(Equals(fmt.Sprintf("Subject of commit %s", hash))).
Content(Equals("my change"))
},
})

View file

@ -113,6 +113,7 @@ var tests = []*components.IntegrationTest{
custom_commands.MenuFromCommandsOutput,
custom_commands.MultiplePrompts,
custom_commands.OmitFromHistory,
custom_commands.ShowOutputInPanel,
custom_commands.SuggestionsCommand,
custom_commands.SuggestionsPreset,
demo.AmendOldCommit,

View file

@ -924,6 +924,10 @@
"type": "boolean",
"description": "If true, show the command's output in a popup within Lazygit"
},
"outputTitle": {
"type": "string",
"description": "The title to display in the popup panel if showOutput is true. If left unset, the command will be used as the title."
},
"after": {
"properties": {
"checkForConflicts": {