Skip to content

Commit

Permalink
feat: allow OSCommand.Quote to be invoked within a custom command
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryooooooga committed Sep 30, 2022
1 parent 092363a commit 19df238
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 21 deletions.
2 changes: 1 addition & 1 deletion docs/Custom_Command_Keybindings.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
7 changes: 6 additions & 1 deletion pkg/gui/services/custom_commands/handler_creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package custom_commands

import (
"strings"
"text/template"

"github.com/jesseduffield/generics/slices"
"github.com/jesseduffield/lazygit/pkg/commands"
Expand Down Expand Up @@ -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 {
Expand Down
17 changes: 0 additions & 17 deletions pkg/gui/services/custom_commands/resolver.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package custom_commands

import (
"bytes"
"text/template"

"github.com/jesseduffield/lazygit/pkg/common"
"github.com/jesseduffield/lazygit/pkg/config"
)
Expand Down Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions pkg/utils/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 19df238

Please sign in to comment.