jesseduffield.lazygit/pkg/utils/template.go
Rabin Yasharzadehe 8730d58ba6 feat: add configurable terminal window title
Add a `gui.terminalTitle` config option (default: `lazygit::{{repoName}}`)
that sets the terminal window title via ANSI escape sequences through
gocui.Screen.SetTitle(). The title updates on startup and when switching
repos. Control characters are sanitized to prevent escape sequence
injection. Set to empty string to disable.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-09-06 10:00:19 +03:00

46 lines
1.2 KiB
Go

package utils
import (
"bytes"
"strings"
"text/template"
)
func ResolveTemplate(templateStr string, object any, funcs template.FuncMap) (string, error) {
tmpl, err := template.New("template").Funcs(funcs).Option("missingkey=error").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
}
// ResolvePlaceholderString populates a template with values
func ResolvePlaceholderString(str string, arguments map[string]string) string {
oldnews := make([]string, 0, len(arguments)*4)
for key, value := range arguments {
oldnews = append(oldnews,
"{{"+key+"}}", value,
"{{."+key+"}}", value,
)
}
return strings.NewReplacer(oldnews...).Replace(str)
}
// SanitizeTerminalTitle removes control characters from a string intended
// for use as a terminal title. Control characters (ASCII 0-31 and 127) could
// break terminal behavior or be used for escape sequence injection.
func SanitizeTerminalTitle(title string) string {
return strings.Map(func(r rune) rune {
if r < 32 || r == 127 {
return -1 // Remove control characters
}
return r
}, title)
}