1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-07-31 14:24:25 +03:00

Add Key field to CustomCommandPrompt struct

Add Form field to CustomCommandObjects struct

Write user prompts responses to Form field

Ensure that map keys exists

Add form prompts integration test

Remove redundant index
This commit is contained in:
Mihai22125
2022-08-09 19:52:19 +03:00
committed by Jesse Duffield
parent c81333fefe
commit 7e9dffe1b9
18 changed files with 122 additions and 6 deletions

View File

@ -314,6 +314,8 @@ type CustomCommand struct {
} }
type CustomCommandPrompt struct { type CustomCommandPrompt struct {
Key string `yaml:"key"`
// one of 'input', 'menu', 'confirm', or 'menuFromCommand' // one of 'input', 'menu', 'confirm', or 'menuFromCommand'
Type string `yaml:"type"` Type string `yaml:"type"`

View File

@ -45,8 +45,9 @@ func (self *HandlerCreator) call(customCommand config.CustomCommand) func() erro
return func() error { return func() error {
sessionState := self.sessionStateLoader.call() sessionState := self.sessionStateLoader.call()
promptResponses := make([]string, len(customCommand.Prompts)) promptResponses := make([]string, len(customCommand.Prompts))
form := make(map[string]string)
f := func() error { return self.finalHandler(customCommand, sessionState, promptResponses) } f := func() error { return self.finalHandler(customCommand, sessionState, promptResponses, form) }
// if we have prompts we'll recursively wrap our confirm handlers with more prompts // if we have prompts we'll recursively wrap our confirm handlers with more prompts
// until we reach the actual command // until we reach the actual command
@ -60,10 +61,11 @@ func (self *HandlerCreator) call(customCommand config.CustomCommand) func() erro
wrappedF := func(response string) error { wrappedF := func(response string) error {
promptResponses[idx] = response promptResponses[idx] = response
form[prompt.Key] = response
return g() return g()
} }
resolveTemplate := self.getResolveTemplateFn(promptResponses, sessionState) resolveTemplate := self.getResolveTemplateFn(form, promptResponses, sessionState)
resolvedPrompt, err := self.resolver.resolvePrompt(&prompt, resolveTemplate) resolvedPrompt, err := self.resolver.resolvePrompt(&prompt, resolveTemplate)
if err != nil { if err != nil {
return self.c.Error(err) return self.c.Error(err)
@ -154,19 +156,21 @@ func (self *HandlerCreator) menuPromptFromCommand(prompt *config.CustomCommandPr
type CustomCommandObjects struct { type CustomCommandObjects struct {
*SessionState *SessionState
PromptResponses []string PromptResponses []string
Form map[string]string
} }
func (self *HandlerCreator) getResolveTemplateFn(promptResponses []string, sessionState *SessionState) func(string) (string, error) { func (self *HandlerCreator) getResolveTemplateFn(form map[string]string, promptResponses []string, sessionState *SessionState) func(string) (string, error) {
objects := CustomCommandObjects{ objects := CustomCommandObjects{
SessionState: sessionState, SessionState: sessionState,
PromptResponses: promptResponses, PromptResponses: promptResponses,
Form: form,
} }
return func(templateStr string) (string, error) { return utils.ResolveTemplate(templateStr, objects) } return func(templateStr string) (string, error) { return utils.ResolveTemplate(templateStr, objects) }
} }
func (self *HandlerCreator) finalHandler(customCommand config.CustomCommand, sessionState *SessionState, promptResponses []string) error { func (self *HandlerCreator) finalHandler(customCommand config.CustomCommand, sessionState *SessionState, promptResponses []string, form map[string]string) error {
resolveTemplate := self.getResolveTemplateFn(promptResponses, sessionState) resolveTemplate := self.getResolveTemplateFn(form, promptResponses, sessionState)
cmdStr, err := resolveTemplate(customCommand.Command) cmdStr, err := resolveTemplate(customCommand.Command)
if err != nil { if err != nil {
return self.c.Error(err) return self.c.Error(err)

View File

@ -0,0 +1,88 @@
package custom_commands
import (
"github.com/jesseduffield/lazygit/pkg/config"
. "github.com/jesseduffield/lazygit/pkg/integration/components"
)
var FormPrompts = NewIntegrationTest(NewIntegrationTestArgs{
Description: "Using a custom command reffering prompt responses by name",
ExtraCmdArgs: "",
Skip: false,
SetupRepo: func(shell *Shell) {
shell.EmptyCommit("blah")
},
SetupConfig: func(cfg *config.AppConfig) {
cfg.UserConfig.CustomCommands = []config.CustomCommand{
{
Key: "a",
Context: "files",
Command: `echo "{{.Form.FileContent}}" > {{.Form.FileName}}`,
Prompts: []config.CustomCommandPrompt{
{
Key: "FileName",
Type: "input",
Title: "Enter a file name",
},
{
Key: "FileContent",
Type: "menu",
Title: "Choose file content",
Options: []config.CustomCommandMenuOption{
{
Name: "foo",
Description: "Foo",
Value: "FOO",
},
{
Name: "bar",
Description: "Bar",
Value: "BAR",
},
{
Name: "baz",
Description: "Baz",
Value: "BAZ",
},
},
},
{
Type: "confirm",
Title: "Are you sure?",
Body: "Are you REALLY sure you want to make this file? Up to you buddy.",
},
},
},
}
},
Run: func(
shell *Shell,
input *Input,
assert *Assert,
keys config.KeybindingConfig,
) {
assert.WorkingTreeFileCount(0)
input.PressKeys("a")
assert.InPrompt()
assert.MatchCurrentViewTitle(Equals("Enter a file name"))
input.Type("myfile")
input.Confirm()
assert.InMenu()
assert.MatchCurrentViewTitle(Equals("Choose file content"))
assert.MatchSelectedLine(Contains("foo"))
input.NextItem()
assert.MatchSelectedLine(Contains("bar"))
input.Confirm()
assert.InConfirm()
assert.MatchCurrentViewTitle(Equals("Are you sure?"))
input.Confirm()
assert.WorkingTreeFileCount(1)
assert.MatchSelectedLine(Contains("myfile"))
assert.MatchMainViewContent(Contains("BAR"))
},
})

View File

@ -36,6 +36,7 @@ var tests = []*components.IntegrationTest{
bisect.FromOtherBranch, bisect.FromOtherBranch,
cherry_pick.CherryPick, cherry_pick.CherryPick,
cherry_pick.CherryPickConflicts, cherry_pick.CherryPickConflicts,
custom_commands.FormPrompts,
} }
func GetTests() []*components.IntegrationTest { func GetTests() []*components.IntegrationTest {

View File

@ -7,7 +7,7 @@ import (
) )
func ResolveTemplate(templateStr string, object interface{}) (string, error) { func ResolveTemplate(templateStr string, object interface{}) (string, error) {
tmpl, err := template.New("template").Parse(templateStr) tmpl, err := template.New("template").Option("missingkey=error").Parse(templateStr)
if err != nil { if err != nil {
return "", err return "", err
} }

View File

@ -0,0 +1 @@
ref: refs/heads/master

View File

@ -0,0 +1,8 @@
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[user]
email = CI@example.com
name = CI

View File

@ -0,0 +1 @@
Unnamed repository; edit this file 'description' to name the repository.

View File

@ -0,0 +1,6 @@
# git ls-files --others --exclude-from=.git/info/exclude
# Lines that start with '#' are comments.
# For a project mostly in C, the following would be a good set of
# exclude patterns (uncomment them if you want to use them):
# *.[oa]
# *~

View File

@ -0,0 +1 @@
0000000000000000000000000000000000000000 6cd61dc75eb17cf3e01d4d5f8f2b38a73ed9be90 CI <CI@example.com> 1660591942 +0000 commit (initial): blah

View File

@ -0,0 +1 @@
0000000000000000000000000000000000000000 6cd61dc75eb17cf3e01d4d5f8f2b38a73ed9be90 CI <CI@example.com> 1660591942 +0000 commit (initial): blah

View File

@ -0,0 +1 @@
6cd61dc75eb17cf3e01d4d5f8f2b38a73ed9be90