Skip to content

Commit 815a4f4

Browse files
committed
Add formatDate function to Go template syntax
This makes it possible to use date and time in initial values like this: ```yaml initialValue: "{{ formatDate .Now "2006" }}" ``` I want to use this to configure my BranchPrefix like this: ```yaml git: branchPrefix: "ruudk/{{ formatDate .Now "2006/1" }}" ```
1 parent facc73a commit 815a4f4

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

pkg/gui/services/custom_commands/handler_creator.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"strings"
77
"text/template"
8+
"time"
89

910
"github.com/jesseduffield/gocui"
1011
"github.com/jesseduffield/lazygit/pkg/config"
@@ -236,17 +237,22 @@ type CustomCommandObjects struct {
236237
*SessionState
237238
PromptResponses []string
238239
Form map[string]string
240+
Now time.Time
239241
}
240242

241243
func (self *HandlerCreator) getResolveTemplateFn(form map[string]string, promptResponses []string, sessionState *SessionState) func(string) (string, error) {
242244
objects := CustomCommandObjects{
243245
SessionState: sessionState,
244246
PromptResponses: promptResponses,
245247
Form: form,
248+
Now: time.Now(),
246249
}
247250

248251
funcs := template.FuncMap{
249252
"quote": self.c.OS().Quote,
253+
"formatDate": func(t time.Time, layout string) string {
254+
return t.Format(layout)
255+
},
250256
}
251257

252258
return func(templateStr string) (string, error) { return utils.ResolveTemplate(templateStr, objects, funcs) }

0 commit comments

Comments
 (0)