diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2022-10-02 12:42:28 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-02 12:42:28 -0700 |
commit | 8a022ddf0e7eddfb825e491fdbd7ea2361cad65d (patch) | |
tree | 7f7f6668761500e31c279e88feed25436d82bd57 | |
parent | 092363a986219da58e4f437b4f3f67c4e4e1ebec (diff) | |
parent | e16f1ba84f84213a8e4af4c7c4c310b8034a8ed3 (diff) |
Merge pull request #2193 from Ryooooooga/feature/template-funcs
15 files changed, 25 insertions, 36 deletions
diff --git a/docs/Custom_Command_Keybindings.md b/docs/Custom_Command_Keybindings.md index ca966799e..09708d54c 100644 --- a/docs/Custom_Command_Keybindings.md +++ b/docs/Custom_Command_Keybindings.md @@ -8,7 +8,7 @@ customCommands: command: 'hub browse -- "commit/{{.SelectedLocalCommit.Sha}}"' context: 'commits' - key: 'a' - command: "git {{if .SelectedFile.HasUnstagedChanges}} add {{else}} reset {{end}} {{.SelectedFile.Name}}" + command: "git {{if .SelectedFile.HasUnstagedChanges}} add {{else}} reset {{end}} {{.SelectedFile.Name | Quote}}" context: 'files' description: 'toggle file staged' - key: 'C' diff --git a/pkg/gui/services/custom_commands/handler_creator.go b/pkg/gui/services/custom_commands/handler_creator.go index 839f70adf..9b30041e3 100644 --- a/pkg/gui/services/custom_commands/handler_creator.go +++ b/pkg/gui/services/custom_commands/handler_creator.go @@ -2,6 +2,7 @@ package custom_commands import ( "strings" + "text/template" "github.com/jesseduffield/generics/slices" "github.com/jesseduffield/lazygit/pkg/commands" @@ -166,7 +167,11 @@ func (self *HandlerCreator) getResolveTemplateFn(form map[string]string, promptR Form: form, } - return func(templateStr string) (string, error) { return utils.ResolveTemplate(templateStr, objects) } + funcs := template.FuncMap{ + "Quote": self.os.Quote, + } + + return func(templateStr string) (string, error) { return utils.ResolveTemplate(templateStr, objects, funcs) } } func (self *HandlerCreator) finalHandler(customCommand config.CustomCommand, sessionState *SessionState, promptResponses []string, form map[string]string) error { diff --git a/pkg/gui/services/custom_commands/resolver.go b/pkg/gui/services/custom_commands/resolver.go index 4702d36c4..4c32a51ca 100644 --- a/pkg/gui/services/custom_commands/resolver.go +++ b/pkg/gui/services/custom_commands/resolver.go @@ -1,9 +1,6 @@ package custom_commands import ( - "bytes" - "text/template" - "github.com/jesseduffield/lazygit/pkg/common" "github.com/jesseduffield/lazygit/pkg/config" ) @@ -110,17 +107,3 @@ type CustomCommandObject struct { PromptResponses []string Form map[string]string } - -func ResolveTemplate(templateStr string, object interface{}) (string, error) { - tmpl, err := template.New("template").Parse(templateStr) - if err != nil { - return "", err - } - - var buf bytes.Buffer - if err := tmpl.Execute(&buf, object); err != nil { - return "", err - } - - return buf.String(), nil -} diff --git a/pkg/integration/tests/custom_commands/form_prompts.go b/pkg/integration/tests/custom_commands/form_prompts.go index e4712dbcc..4148c767a 100644 --- a/pkg/integration/tests/custom_commands/form_prompts.go +++ b/pkg/integration/tests/custom_commands/form_prompts.go @@ -17,7 +17,7 @@ var FormPrompts = NewIntegrationTest(NewIntegrationTestArgs{ { Key: "a", Context: "files", - Command: `echo "{{.Form.FileContent}}" > {{.Form.FileName}}`, + Command: `echo {{.Form.FileContent | Quote}} > {{.Form.FileName | Quote}}`, Prompts: []config.CustomCommandPrompt{ { Key: "FileName", @@ -37,7 +37,7 @@ var FormPrompts = NewIntegrationTest(NewIntegrationTestArgs{ { Name: "bar", Description: "Bar", - Value: "BAR", + Value: `"BAR"`, }, { Name: "baz", @@ -67,7 +67,7 @@ var FormPrompts = NewIntegrationTest(NewIntegrationTestArgs{ assert.InPrompt() assert.MatchCurrentViewTitle(Equals("Enter a file name")) - input.Type("myfile") + input.Type("my file") input.Confirm() assert.InMenu() @@ -82,7 +82,7 @@ var FormPrompts = NewIntegrationTest(NewIntegrationTestArgs{ input.Confirm() assert.WorkingTreeFileCount(1) - assert.MatchSelectedLine(Contains("myfile")) - assert.MatchMainViewContent(Contains("BAR")) + assert.MatchSelectedLine(Contains("my file")) + assert.MatchMainViewContent(Contains(`"BAR"`)) }, }) diff --git a/pkg/utils/template.go b/pkg/utils/template.go index 41388ae3a..d98a68b99 100644 --- a/pkg/utils/template.go +++ b/pkg/utils/template.go @@ -6,8 +6,8 @@ import ( "text/template" ) -func ResolveTemplate(templateStr string, object interface{}) (string, error) { - tmpl, err := template.New("template").Option("missingkey=error").Parse(templateStr) +func ResolveTemplate(templateStr string, object interface{}, funcs template.FuncMap) (string, error) { + tmpl, err := template.New("template").Funcs(funcs).Option("missingkey=error").Parse(templateStr) if err != nil { return "", err } diff --git a/test/integration_new/custom_commands/form_prompts/expected/repo/.git_keep/commit-template.txt b/test/integration_new/custom_commands/form_prompts/expected/repo/.git_keep/commit-template.txt new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/test/integration_new/custom_commands/form_prompts/expected/repo/.git_keep/commit-template.txt diff --git a/test/integration_new/custom_commands/form_prompts/expected/repo/.git_keep/config b/test/integration_new/custom_commands/form_prompts/expected/repo/.git_keep/config index 596ebaeb3..8a748ce32 100644 --- a/test/integration_new/custom_commands/form_prompts/expected/repo/.git_keep/config +++ b/test/integration_new/custom_commands/form_prompts/expected/repo/.git_keep/config @@ -3,6 +3,10 @@ filemode = true bare = false logallrefupdates = true + ignorecase = true + precomposeunicode = true [user] email = CI@example.com name = CI +[commit] + gpgSign = false diff --git a/test/integration_new/custom_commands/form_prompts/expected/repo/.git_keep/info/exclude b/test/integration_new/custom_commands/form_prompts/expected/repo/.git_keep/info/exclude index a5196d1be..e69de29bb 100644 --- a/test/integration_new/custom_commands/form_prompts/expected/repo/.git_keep/info/exclude +++ b/test/integration_new/custom_commands/form_prompts/expected/repo/.git_keep/info/exclude @@ -1,6 +0,0 @@ -# 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] -# *~ diff --git a/test/integration_new/custom_commands/form_prompts/expected/repo/.git_keep/logs/HEAD b/test/integration_new/custom_commands/form_prompts/expected/repo/.git_keep/logs/HEAD index 153f2ddc4..e0854d95a 100644 --- a/test/integration_new/custom_commands/form_prompts/expected/repo/.git_keep/logs/HEAD +++ b/test/integration_new/custom_commands/form_prompts/expected/repo/.git_keep/logs/HEAD @@ -1 +1 @@ -0000000000000000000000000000000000000000 6cd61dc75eb17cf3e01d4d5f8f2b38a73ed9be90 CI <CI@example.com> 1660591942 +0000 commit (initial): blah +0000000000000000000000000000000000000000 4687e94a43ed02b2ba08f3e6160ee22b92e64413 CI <CI@example.com> 1664625021 +0900 commit (initial): blah diff --git a/test/integration_new/custom_commands/form_prompts/expected/repo/.git_keep/logs/refs/heads/master b/test/integration_new/custom_commands/form_prompts/expected/repo/.git_keep/logs/refs/heads/master index 153f2ddc4..e0854d95a 100644 --- a/test/integration_new/custom_commands/form_prompts/expected/repo/.git_keep/logs/refs/heads/master +++ b/test/integration_new/custom_commands/form_prompts/expected/repo/.git_keep/logs/refs/heads/master @@ -1 +1 @@ -0000000000000000000000000000000000000000 6cd61dc75eb17cf3e01d4d5f8f2b38a73ed9be90 CI <CI@example.com> 1660591942 +0000 commit (initial): blah +0000000000000000000000000000000000000000 4687e94a43ed02b2ba08f3e6160ee22b92e64413 CI <CI@example.com> 1664625021 +0900 commit (initial): blah diff --git a/test/integration_new/custom_commands/form_prompts/expected/repo/.git_keep/objects/46/87e94a43ed02b2ba08f3e6160ee22b92e64413 b/test/integration_new/custom_commands/form_prompts/expected/repo/.git_keep/objects/46/87e94a43ed02b2ba08f3e6160ee22b92e64413 new file mode 100644 index 000000000..00eeb9d5f --- /dev/null +++ b/test/integration_new/custom_commands/form_prompts/expected/repo/.git_keep/objects/46/87e94a43ed02b2ba08f3e6160ee22b92e64413 @@ -0,0 +1,3 @@ +x1 +0@>BUYPB Sa9 +)ĸz]?~i;x~aRXHW5oi4ۣ:a ]t;N+
\ No newline at end of file diff --git a/test/integration_new/custom_commands/form_prompts/expected/repo/.git_keep/objects/6c/d61dc75eb17cf3e01d4d5f8f2b38a73ed9be90 b/test/integration_new/custom_commands/form_prompts/expected/repo/.git_keep/objects/6c/d61dc75eb17cf3e01d4d5f8f2b38a73ed9be90 Binary files differdeleted file mode 100644 index 8d0c394f8..000000000 --- a/test/integration_new/custom_commands/form_prompts/expected/repo/.git_keep/objects/6c/d61dc75eb17cf3e01d4d5f8f2b38a73ed9be90 +++ /dev/null diff --git a/test/integration_new/custom_commands/form_prompts/expected/repo/.git_keep/refs/heads/master b/test/integration_new/custom_commands/form_prompts/expected/repo/.git_keep/refs/heads/master index 4c2ab0871..97a2c1d6c 100644 --- a/test/integration_new/custom_commands/form_prompts/expected/repo/.git_keep/refs/heads/master +++ b/test/integration_new/custom_commands/form_prompts/expected/repo/.git_keep/refs/heads/master @@ -1 +1 @@ -6cd61dc75eb17cf3e01d4d5f8f2b38a73ed9be90 +4687e94a43ed02b2ba08f3e6160ee22b92e64413 diff --git a/test/integration_new/custom_commands/form_prompts/expected/repo/my file b/test/integration_new/custom_commands/form_prompts/expected/repo/my file new file mode 100644 index 000000000..d6efee909 --- /dev/null +++ b/test/integration_new/custom_commands/form_prompts/expected/repo/my file @@ -0,0 +1 @@ +"BAR" diff --git a/test/integration_new/custom_commands/form_prompts/expected/repo/myfile b/test/integration_new/custom_commands/form_prompts/expected/repo/myfile deleted file mode 100644 index ba578e48b..000000000 --- a/test/integration_new/custom_commands/form_prompts/expected/repo/myfile +++ /dev/null @@ -1 +0,0 @@ -BAR |