Skip to content

Commit 8a022dd

Browse files
Merge pull request #2193 from Ryooooooga/feature/template-funcs
2 parents 092363a + e16f1ba commit 8a022dd

File tree

15 files changed

+25
-36
lines changed

15 files changed

+25
-36
lines changed

docs/Custom_Command_Keybindings.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ customCommands:
88
command: 'hub browse -- "commit/{{.SelectedLocalCommit.Sha}}"'
99
context: 'commits'
1010
- key: 'a'
11-
command: "git {{if .SelectedFile.HasUnstagedChanges}} add {{else}} reset {{end}} {{.SelectedFile.Name}}"
11+
command: "git {{if .SelectedFile.HasUnstagedChanges}} add {{else}} reset {{end}} {{.SelectedFile.Name | Quote}}"
1212
context: 'files'
1313
description: 'toggle file staged'
1414
- key: 'C'

pkg/gui/services/custom_commands/handler_creator.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package custom_commands
22

33
import (
44
"strings"
5+
"text/template"
56

67
"github.com/jesseduffield/generics/slices"
78
"github.com/jesseduffield/lazygit/pkg/commands"
@@ -166,7 +167,11 @@ func (self *HandlerCreator) getResolveTemplateFn(form map[string]string, promptR
166167
Form: form,
167168
}
168169

169-
return func(templateStr string) (string, error) { return utils.ResolveTemplate(templateStr, objects) }
170+
funcs := template.FuncMap{
171+
"Quote": self.os.Quote,
172+
}
173+
174+
return func(templateStr string) (string, error) { return utils.ResolveTemplate(templateStr, objects, funcs) }
170175
}
171176

172177
func (self *HandlerCreator) finalHandler(customCommand config.CustomCommand, sessionState *SessionState, promptResponses []string, form map[string]string) error {

pkg/gui/services/custom_commands/resolver.go

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package custom_commands
22

33
import (
4-
"bytes"
5-
"text/template"
6-
74
"github.com/jesseduffield/lazygit/pkg/common"
85
"github.com/jesseduffield/lazygit/pkg/config"
96
)
@@ -110,17 +107,3 @@ type CustomCommandObject struct {
110107
PromptResponses []string
111108
Form map[string]string
112109
}
113-
114-
func ResolveTemplate(templateStr string, object interface{}) (string, error) {
115-
tmpl, err := template.New("template").Parse(templateStr)
116-
if err != nil {
117-
return "", err
118-
}
119-
120-
var buf bytes.Buffer
121-
if err := tmpl.Execute(&buf, object); err != nil {
122-
return "", err
123-
}
124-
125-
return buf.String(), nil
126-
}

pkg/integration/tests/custom_commands/form_prompts.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var FormPrompts = NewIntegrationTest(NewIntegrationTestArgs{
1717
{
1818
Key: "a",
1919
Context: "files",
20-
Command: `echo "{{.Form.FileContent}}" > {{.Form.FileName}}`,
20+
Command: `echo {{.Form.FileContent | Quote}} > {{.Form.FileName | Quote}}`,
2121
Prompts: []config.CustomCommandPrompt{
2222
{
2323
Key: "FileName",
@@ -37,7 +37,7 @@ var FormPrompts = NewIntegrationTest(NewIntegrationTestArgs{
3737
{
3838
Name: "bar",
3939
Description: "Bar",
40-
Value: "BAR",
40+
Value: `"BAR"`,
4141
},
4242
{
4343
Name: "baz",
@@ -67,7 +67,7 @@ var FormPrompts = NewIntegrationTest(NewIntegrationTestArgs{
6767

6868
assert.InPrompt()
6969
assert.MatchCurrentViewTitle(Equals("Enter a file name"))
70-
input.Type("myfile")
70+
input.Type("my file")
7171
input.Confirm()
7272

7373
assert.InMenu()
@@ -82,7 +82,7 @@ var FormPrompts = NewIntegrationTest(NewIntegrationTestArgs{
8282
input.Confirm()
8383

8484
assert.WorkingTreeFileCount(1)
85-
assert.MatchSelectedLine(Contains("myfile"))
86-
assert.MatchMainViewContent(Contains("BAR"))
85+
assert.MatchSelectedLine(Contains("my file"))
86+
assert.MatchMainViewContent(Contains(`"BAR"`))
8787
},
8888
})

pkg/utils/template.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import (
66
"text/template"
77
)
88

9-
func ResolveTemplate(templateStr string, object interface{}) (string, error) {
10-
tmpl, err := template.New("template").Option("missingkey=error").Parse(templateStr)
9+
func ResolveTemplate(templateStr string, object interface{}, funcs template.FuncMap) (string, error) {
10+
tmpl, err := template.New("template").Funcs(funcs).Option("missingkey=error").Parse(templateStr)
1111
if err != nil {
1212
return "", err
1313
}

test/integration_new/custom_commands/form_prompts/expected/repo/.git_keep/commit-template.txt

Whitespace-only changes.

test/integration_new/custom_commands/form_prompts/expected/repo/.git_keep/config

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
filemode = true
44
bare = false
55
logallrefupdates = true
6+
ignorecase = true
7+
precomposeunicode = true
68
[user]
79
810
name = CI
11+
[commit]
12+
gpgSign = false
Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +0,0 @@
1-
# git ls-files --others --exclude-from=.git/info/exclude
2-
# Lines that start with '#' are comments.
3-
# For a project mostly in C, the following would be a good set of
4-
# exclude patterns (uncomment them if you want to use them):
5-
# *.[oa]
6-
# *~
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0000000000000000000000000000000000000000 6cd61dc75eb17cf3e01d4d5f8f2b38a73ed9be90 CI <[email protected]> 1660591942 +0000 commit (initial): blah
1+
0000000000000000000000000000000000000000 4687e94a43ed02b2ba08f3e6160ee22b92e64413 CI <[email protected]> 1664625021 +0900 commit (initial): blah
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0000000000000000000000000000000000000000 6cd61dc75eb17cf3e01d4d5f8f2b38a73ed9be90 CI <[email protected]> 1660591942 +0000 commit (initial): blah
1+
0000000000000000000000000000000000000000 4687e94a43ed02b2ba08f3e6160ee22b92e64413 CI <[email protected]> 1664625021 +0900 commit (initial): blah

0 commit comments

Comments
 (0)