feat: allow OSCommand.Quote to be invoked within a custom command

This commit is contained in:
Ryooooooga 2022-09-30 21:10:56 +09:00
parent 092363a986
commit 19df238b77
No known key found for this signature in database
GPG key ID: 07CF200DFCC20C25
4 changed files with 9 additions and 21 deletions

View file

@ -8,7 +8,7 @@ customCommands:
command: 'hub browse -- "commit/{{.SelectedLocalCommit.Sha}}"' command: 'hub browse -- "commit/{{.SelectedLocalCommit.Sha}}"'
context: 'commits' context: 'commits'
- key: 'a' - 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' context: 'files'
description: 'toggle file staged' description: 'toggle file staged'
- key: 'C' - key: 'C'

View file

@ -2,6 +2,7 @@ package custom_commands
import ( import (
"strings" "strings"
"text/template"
"github.com/jesseduffield/generics/slices" "github.com/jesseduffield/generics/slices"
"github.com/jesseduffield/lazygit/pkg/commands" "github.com/jesseduffield/lazygit/pkg/commands"
@ -166,7 +167,11 @@ func (self *HandlerCreator) getResolveTemplateFn(form map[string]string, promptR
Form: form, 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 { func (self *HandlerCreator) finalHandler(customCommand config.CustomCommand, sessionState *SessionState, promptResponses []string, form map[string]string) error {

View file

@ -1,9 +1,6 @@
package custom_commands package custom_commands
import ( import (
"bytes"
"text/template"
"github.com/jesseduffield/lazygit/pkg/common" "github.com/jesseduffield/lazygit/pkg/common"
"github.com/jesseduffield/lazygit/pkg/config" "github.com/jesseduffield/lazygit/pkg/config"
) )
@ -110,17 +107,3 @@ type CustomCommandObject struct {
PromptResponses []string PromptResponses []string
Form map[string]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
}

View file

@ -6,8 +6,8 @@ import (
"text/template" "text/template"
) )
func ResolveTemplate(templateStr string, object interface{}) (string, error) { func ResolveTemplate(templateStr string, object interface{}, funcs template.FuncMap) (string, error) {
tmpl, err := template.New("template").Option("missingkey=error").Parse(templateStr) tmpl, err := template.New("template").Funcs(funcs).Option("missingkey=error").Parse(templateStr)
if err != nil { if err != nil {
return "", err return "", err
} }