diff options
author | Luka Markušić <luka.markusic@microblink.com> | 2022-11-25 22:09:02 +0100 |
---|---|---|
committer | Luka Markušić <luka.markusic@microblink.com> | 2022-11-25 22:46:36 +0100 |
commit | b3d086bdc1d1b95f735d8807f425f4f2225038eb (patch) | |
tree | e0961df4d03952e77d219b1d95a96b517f82c908 /pkg | |
parent | a6ebc5869e4f7ab5a88a9574b788dfbc47c421ef (diff) |
Resolve the prompt just before using it
In case a later command depends on a prompt input from a previous one we
need to evaluate it only after the previous prompt has been confirmed.
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/gui/services/custom_commands/handler_creator.go | 20 | ||||
-rw-r--r-- | pkg/integration/tests/custom_commands/menu_from_commands_output.go | 69 | ||||
-rw-r--r-- | pkg/integration/tests/tests.go | 1 |
3 files changed, 86 insertions, 4 deletions
diff --git a/pkg/gui/services/custom_commands/handler_creator.go b/pkg/gui/services/custom_commands/handler_creator.go index bad972bd6..4a81b08b5 100644 --- a/pkg/gui/services/custom_commands/handler_creator.go +++ b/pkg/gui/services/custom_commands/handler_creator.go @@ -67,26 +67,38 @@ func (self *HandlerCreator) call(customCommand config.CustomCommand) func() erro } resolveTemplate := self.getResolveTemplateFn(form, promptResponses, sessionState) - resolvedPrompt, err := self.resolver.resolvePrompt(&prompt, resolveTemplate) - if err != nil { - return self.c.Error(err) - } switch prompt.Type { case "input": f = func() error { + resolvedPrompt, err := self.resolver.resolvePrompt(&prompt, resolveTemplate) + if err != nil { + return self.c.Error(err) + } return self.inputPrompt(resolvedPrompt, wrappedF) } case "menu": f = func() error { + resolvedPrompt, err := self.resolver.resolvePrompt(&prompt, resolveTemplate) + if err != nil { + return self.c.Error(err) + } return self.menuPrompt(resolvedPrompt, wrappedF) } case "menuFromCommand": f = func() error { + resolvedPrompt, err := self.resolver.resolvePrompt(&prompt, resolveTemplate) + if err != nil { + return self.c.Error(err) + } return self.menuPromptFromCommand(resolvedPrompt, wrappedF) } case "confirm": f = func() error { + resolvedPrompt, err := self.resolver.resolvePrompt(&prompt, resolveTemplate) + if err != nil { + return self.c.Error(err) + } return self.confirmPrompt(resolvedPrompt, g) } default: diff --git a/pkg/integration/tests/custom_commands/menu_from_commands_output.go b/pkg/integration/tests/custom_commands/menu_from_commands_output.go new file mode 100644 index 000000000..fdd207c4c --- /dev/null +++ b/pkg/integration/tests/custom_commands/menu_from_commands_output.go @@ -0,0 +1,69 @@ +package custom_commands + +import ( + "github.com/jesseduffield/lazygit/pkg/config" + . "github.com/jesseduffield/lazygit/pkg/integration/components" +) + +var MenuFromCommandsOutput = NewIntegrationTest(NewIntegrationTestArgs{ + Description: "Using prompt response in menuFromCommand entries", + ExtraCmdArgs: "", + Skip: false, + SetupRepo: func(shell *Shell) { + shell. + EmptyCommit("foo"). + NewBranch("feature/foo"). + EmptyCommit("bar"). + NewBranch("feature/bar"). + EmptyCommit("baz") + }, + SetupConfig: func(cfg *config.AppConfig) { + cfg.UserConfig.CustomCommands = []config.CustomCommand{ + { + Key: "a", + Context: "localBranches", + Command: "git checkout {{ index .PromptResponses 1 }}", + Prompts: []config.CustomCommandPrompt{ + { + Type: "input", + Title: "Which git command do you want to run?", + InitialValue: "branch", + }, + { + Type: "menuFromCommand", + Title: "Branch:", + Command: `git {{ index .PromptResponses 0 }} --format='%(refname:short)'`, + Filter: "(?P<branch>.*)", + ValueFormat: `{{ .branch }}`, + LabelFormat: `{{ .branch | green }}`, + }, + }, + }, + } + }, + Run: func( + shell *Shell, + input *Input, + assert *Assert, + keys config.KeybindingConfig, + ) { + assert.WorkingTreeFileCount(0) + input.SwitchToBranchesWindow() + + input.PressKeys("a") + + assert.InPrompt() + assert.MatchCurrentViewTitle(Equals("Which git command do you want to run?")) + assert.MatchSelectedLine(Equals("branch")) + input.Confirm() + + assert.InMenu() + assert.MatchCurrentViewTitle(Equals("Branch:")) + input.NextItem() + input.NextItem() + assert.MatchSelectedLine(Equals("master")) + input.Confirm() + + assert.CurrentBranchName("master") + }, +}) diff --git a/pkg/integration/tests/tests.go b/pkg/integration/tests/tests.go index f097b032d..6c074bb10 100644 --- a/pkg/integration/tests/tests.go +++ b/pkg/integration/tests/tests.go @@ -39,6 +39,7 @@ var tests = []*components.IntegrationTest{ custom_commands.Basic, custom_commands.FormPrompts, custom_commands.MenuFromCommand, + custom_commands.MenuFromCommandsOutput, custom_commands.MultiplePrompts, file.DirWithUntrackedFile, interactive_rebase.AmendMerge, |