mirror of
https://github.com/jesseduffield/lazygit.git
synced 2026-09-10 07:36:27 -04:00
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>
46 lines
1.2 KiB
Go
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)
|
|
}
|