refactor: use strings.Builder and strings.Repeat to simplify code (#5068)

### PR Description

strings.Builder has fewer memory allocations and better performance.
More info: [golang/go#75190](https://github.com/golang/go/issues/75190)
This commit is contained in:
Stefan Haller 2025-12-03 18:39:11 +01:00 committed by GitHub
commit 06426b2107
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 17 additions and 14 deletions

View file

@ -195,20 +195,21 @@ func getHeader(binding *types.Binding, tr *i18n.TranslationSet) header {
}
func formatSections(tr *i18n.TranslationSet, bindingSections []*bindingSection) string {
content := fmt.Sprintf("# Lazygit %s\n", tr.Keybindings)
var content strings.Builder
content.WriteString(fmt.Sprintf("# Lazygit %s\n", tr.Keybindings))
content += fmt.Sprintf("\n%s\n", italicize(tr.KeybindingsLegend))
content.WriteString(fmt.Sprintf("\n%s\n", italicize(tr.KeybindingsLegend)))
for _, section := range bindingSections {
content += formatTitle(section.title)
content += "| Key | Action | Info |\n"
content += "|-----|--------|-------------|\n"
content.WriteString(formatTitle(section.title))
content.WriteString("| Key | Action | Info |\n")
content.WriteString("|-----|--------|-------------|\n")
for _, binding := range section.bindings {
content += formatBinding(binding)
content.WriteString(formatBinding(binding))
}
}
return content
return content.String()
}
func formatTitle(title string) string {

View file

@ -66,22 +66,22 @@ func (p *PatchBuilder) Start(from, to string, reverse bool, canRebase bool) {
}
func (p *PatchBuilder) PatchToApply(reverse bool, turnAddedFilesIntoDiffAgainstEmptyFile bool) string {
patch := ""
var patch strings.Builder
for filename, info := range p.fileInfoMap {
if info.mode == UNSELECTED {
continue
}
patch += p.RenderPatchForFile(RenderPatchForFileOpts{
patch.WriteString(p.RenderPatchForFile(RenderPatchForFileOpts{
Filename: filename,
Plain: true,
Reverse: reverse,
TurnAddedFilesIntoDiffAgainstEmptyFile: turnAddedFilesIntoDiffAgainstEmptyFile,
})
}))
}
return patch
return patch.String()
}
func (p *PatchBuilder) addFileWhole(info *fileInfo) {

View file

@ -1,6 +1,8 @@
package presentation
import (
"strings"
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/theme"
"github.com/samber/lo"
@ -15,11 +17,11 @@ func GetSubmoduleListDisplayStrings(submodules []*models.SubmoduleConfig) [][]st
func getSubmoduleDisplayStrings(s *models.SubmoduleConfig) []string {
name := s.Name
if s.ParentModule != nil {
indentation := ""
count := 0
for p := s.ParentModule; p != nil; p = p.ParentModule {
indentation += " "
count++
}
indentation := strings.Repeat(" ", count)
name = indentation + "- " + s.Name
}