mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-05-31 23:19:40 +02:00
Merge pull request #2193 from Ryooooooga/feature/template-funcs
This commit is contained in:
commit
8a022ddf0e
@ -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'
|
||||
|
@ -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 {
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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"`))
|
||||
},
|
||||
})
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -3,6 +3,10 @@
|
||||
filemode = true
|
||||
bare = false
|
||||
logallrefupdates = true
|
||||
ignorecase = true
|
||||
precomposeunicode = true
|
||||
[user]
|
||||
email = CI@example.com
|
||||
name = CI
|
||||
[commit]
|
||||
gpgSign = false
|
||||
|
@ -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]
|
||||
# *~
|
@ -1 +1 @@
|
||||
0000000000000000000000000000000000000000 6cd61dc75eb17cf3e01d4d5f8f2b38a73ed9be90 CI <CI@example.com> 1660591942 +0000 commit (initial): blah
|
||||
0000000000000000000000000000000000000000 4687e94a43ed02b2ba08f3e6160ee22b92e64413 CI <CI@example.com> 1664625021 +0900 commit (initial): blah
|
||||
|
@ -1 +1 @@
|
||||
0000000000000000000000000000000000000000 6cd61dc75eb17cf3e01d4d5f8f2b38a73ed9be90 CI <CI@example.com> 1660591942 +0000 commit (initial): blah
|
||||
0000000000000000000000000000000000000000 4687e94a43ed02b2ba08f3e6160ee22b92e64413 CI <CI@example.com> 1664625021 +0900 commit (initial): blah
|
||||
|
@ -0,0 +1,3 @@
|
||||
x�Í1
|
||||
Ã0@ÑÎ>…öB‘UYØPB SŽa9
|
||||
)ĸzüä]?~iµ¾;x~Þúa¬‘ÂR„©¨˜¦Œ‚X׸H¢W5ã„ìò·oí€i†×4�öËõ³Û£´:€a¡€äᎠÑ]õštû“;ÝóæN¬£+²
|
Binary file not shown.
@ -1 +1 @@
|
||||
6cd61dc75eb17cf3e01d4d5f8f2b38a73ed9be90
|
||||
4687e94a43ed02b2ba08f3e6160ee22b92e64413
|
||||
|
@ -0,0 +1 @@
|
||||
"BAR"
|
@ -1 +0,0 @@
|
||||
BAR
|
Loading…
x
Reference in New Issue
Block a user