Skip to content

Commit 9fb667e

Browse files
committed
fix: unnecessary ReplaceAll
1 parent 31c3385 commit 9fb667e

File tree

1 file changed

+170
-171
lines changed

1 file changed

+170
-171
lines changed

pkg/cheatsheet/generate.go

Lines changed: 170 additions & 171 deletions
Original file line numberDiff line numberDiff line change
@@ -11,225 +11,224 @@
1111
package cheatsheet
1212

1313
import (
14-
"cmp"
15-
"fmt"
16-
"log"
17-
"os"
18-
"slices"
19-
"strings"
20-
21-
"github.com/jesseduffield/generics/maps"
22-
"github.com/jesseduffield/lazycore/pkg/utils"
23-
"github.com/jesseduffield/lazygit/pkg/app"
24-
"github.com/jesseduffield/lazygit/pkg/config"
25-
"github.com/jesseduffield/lazygit/pkg/gui/keybindings"
26-
"github.com/jesseduffield/lazygit/pkg/gui/types"
27-
"github.com/jesseduffield/lazygit/pkg/i18n"
28-
"github.com/samber/lo"
14+
"cmp"
15+
"fmt"
16+
"log"
17+
"os"
18+
"slices"
19+
"strings"
20+
21+
"github.com/jesseduffield/generics/maps"
22+
"github.com/jesseduffield/lazycore/pkg/utils"
23+
"github.com/jesseduffield/lazygit/pkg/app"
24+
"github.com/jesseduffield/lazygit/pkg/config"
25+
"github.com/jesseduffield/lazygit/pkg/gui/keybindings"
26+
"github.com/jesseduffield/lazygit/pkg/gui/types"
27+
"github.com/jesseduffield/lazygit/pkg/i18n"
28+
"github.com/samber/lo"
2929
)
3030

3131
type bindingSection struct {
32-
title string
33-
bindings []*types.Binding
32+
title string
33+
bindings []*types.Binding
3434
}
3535

3636
type header struct {
37-
// priority decides the order of the headers in the cheatsheet (lower means higher)
38-
priority int
39-
title string
37+
// priority decides the order of the headers in the cheatsheet (lower means higher)
38+
priority int
39+
title string
4040
}
4141

4242
type headerWithBindings struct {
43-
header header
44-
bindings []*types.Binding
43+
header header
44+
bindings []*types.Binding
4545
}
4646

4747
func CommandToRun() string {
48-
return "go generate ./..."
48+
return "go generate ./..."
4949
}
5050

5151
func GetKeybindingsDir() string {
52-
return utils.GetLazyRootDirectory() + "/docs/keybindings"
52+
return utils.GetLazyRootDirectory() + "/docs/keybindings"
5353
}
5454

5555
func generateAtDir(cheatsheetDir string) {
56-
translationSetsByLang, err := i18n.GetTranslationSets()
57-
if err != nil {
58-
log.Fatal(err)
59-
}
60-
mConfig := config.NewDummyAppConfig()
61-
62-
for lang := range translationSetsByLang {
63-
mConfig.GetUserConfig().Gui.Language = lang
64-
common, err := app.NewCommon(mConfig)
65-
if err != nil {
66-
log.Fatal(err)
67-
}
68-
tr, err := i18n.NewTranslationSetFromConfig(common.Log, lang)
69-
if err != nil {
70-
log.Fatal(err)
71-
}
72-
common.Tr = tr
73-
mApp, _ := app.NewApp(mConfig, nil, common)
74-
path := cheatsheetDir + "/Keybindings_" + lang + ".md"
75-
file, err := os.Create(path)
76-
if err != nil {
77-
panic(err)
78-
}
79-
80-
bindings := mApp.Gui.GetCheatsheetKeybindings()
81-
bindingSections := getBindingSections(bindings, mApp.Tr)
82-
content := formatSections(mApp.Tr, bindingSections)
83-
content = fmt.Sprintf("_This file is auto-generated. To update, make the changes in the "+
84-
"pkg/i18n directory and then run `%s` from the project root._\n\n%s", CommandToRun(), content)
85-
writeString(file, content)
86-
}
56+
translationSetsByLang, err := i18n.GetTranslationSets()
57+
if err != nil {
58+
log.Fatal(err)
59+
}
60+
mConfig := config.NewDummyAppConfig()
61+
62+
for lang := range translationSetsByLang {
63+
mConfig.GetUserConfig().Gui.Language = lang
64+
common, err := app.NewCommon(mConfig)
65+
if err != nil {
66+
log.Fatal(err)
67+
}
68+
tr, err := i18n.NewTranslationSetFromConfig(common.Log, lang)
69+
if err != nil {
70+
log.Fatal(err)
71+
}
72+
common.Tr = tr
73+
mApp, _ := app.NewApp(mConfig, nil, common)
74+
path := cheatsheetDir + "/Keybindings_" + lang + ".md"
75+
file, err := os.Create(path)
76+
if err != nil {
77+
panic(err)
78+
}
79+
80+
bindings := mApp.Gui.GetCheatsheetKeybindings()
81+
bindingSections := getBindingSections(bindings, mApp.Tr)
82+
content := formatSections(mApp.Tr, bindingSections)
83+
content = fmt.Sprintf("_This file is auto-generated. To update, make the changes in the "+
84+
"pkg/i18n directory and then run `%s` from the project root._\n\n%s", CommandToRun(), content)
85+
writeString(file, content)
86+
}
8787
}
8888

8989
func Generate() {
90-
generateAtDir(GetKeybindingsDir())
90+
generateAtDir(GetKeybindingsDir())
9191
}
9292

9393
func writeString(file *os.File, str string) {
94-
_, err := file.WriteString(str)
95-
if err != nil {
96-
log.Fatal(err)
97-
}
94+
_, err := file.WriteString(str)
95+
if err != nil {
96+
log.Fatal(err)
97+
}
9898
}
9999

100100
func localisedTitle(tr *i18n.TranslationSet, str string) string {
101-
contextTitleMap := map[string]string{
102-
"global": tr.GlobalTitle,
103-
"navigation": tr.NavigationTitle,
104-
"branches": tr.BranchesTitle,
105-
"localBranches": tr.LocalBranchesTitle,
106-
"files": tr.FilesTitle,
107-
"status": tr.StatusTitle,
108-
"submodules": tr.SubmodulesTitle,
109-
"subCommits": tr.SubCommitsTitle,
110-
"remoteBranches": tr.RemoteBranchesTitle,
111-
"remotes": tr.RemotesTitle,
112-
"reflogCommits": tr.ReflogCommitsTitle,
113-
"tags": tr.TagsTitle,
114-
"commitFiles": tr.CommitFilesTitle,
115-
"commitMessage": tr.CommitSummaryTitle,
116-
"commitDescription": tr.CommitDescriptionTitle,
117-
"commits": tr.CommitsTitle,
118-
"confirmation": tr.ConfirmationTitle,
119-
"information": tr.InformationTitle,
120-
"main": tr.NormalTitle,
121-
"patchBuilding": tr.PatchBuildingTitle,
122-
"mergeConflicts": tr.MergingTitle,
123-
"staging": tr.StagingTitle,
124-
"menu": tr.MenuTitle,
125-
"search": tr.SearchTitle,
126-
"secondary": tr.SecondaryTitle,
127-
"stash": tr.StashTitle,
128-
"suggestions": tr.SuggestionsCheatsheetTitle,
129-
"extras": tr.ExtrasTitle,
130-
"worktrees": tr.WorktreesTitle,
131-
}
132-
133-
title, ok := contextTitleMap[str]
134-
if !ok {
135-
panic(fmt.Sprintf("title not found for %s", str))
136-
}
137-
138-
return title
101+
contextTitleMap := map[string]string{
102+
"global": tr.GlobalTitle,
103+
"navigation": tr.NavigationTitle,
104+
"branches": tr.BranchesTitle,
105+
"localBranches": tr.LocalBranchesTitle,
106+
"files": tr.FilesTitle,
107+
"status": tr.StatusTitle,
108+
"submodules": tr.SubmodulesTitle,
109+
"subCommits": tr.SubCommitsTitle,
110+
"remoteBranches": tr.RemoteBranchesTitle,
111+
"remotes": tr.RemotesTitle,
112+
"reflogCommits": tr.ReflogCommitsTitle,
113+
"tags": tr.TagsTitle,
114+
"commitFiles": tr.CommitFilesTitle,
115+
"commitMessage": tr.CommitSummaryTitle,
116+
"commitDescription": tr.CommitDescriptionTitle,
117+
"commits": tr.CommitsTitle,
118+
"confirmation": tr.ConfirmationTitle,
119+
"information": tr.InformationTitle,
120+
"main": tr.NormalTitle,
121+
"patchBuilding": tr.PatchBuildingTitle,
122+
"mergeConflicts": tr.MergingTitle,
123+
"staging": tr.StagingTitle,
124+
"menu": tr.MenuTitle,
125+
"search": tr.SearchTitle,
126+
"secondary": tr.SecondaryTitle,
127+
"stash": tr.StashTitle,
128+
"suggestions": tr.SuggestionsCheatsheetTitle,
129+
"extras": tr.ExtrasTitle,
130+
"worktrees": tr.WorktreesTitle,
131+
}
132+
133+
title, ok := contextTitleMap[str]
134+
if !ok {
135+
panic(fmt.Sprintf("title not found for %s", str))
136+
}
137+
138+
return title
139139
}
140140

141141
func getBindingSections(bindings []*types.Binding, tr *i18n.TranslationSet) []*bindingSection {
142-
excludedViews := []string{"stagingSecondary", "patchBuildingSecondary"}
143-
bindingsToDisplay := lo.Filter(bindings, func(binding *types.Binding, _ int) bool {
144-
if lo.Contains(excludedViews, binding.ViewName) {
145-
return false
146-
}
147-
148-
return (binding.Description != "" || binding.Alternative != "") && binding.Key != nil
149-
})
150-
151-
bindingsByHeader := lo.GroupBy(bindingsToDisplay, func(binding *types.Binding) header {
152-
return getHeader(binding, tr)
153-
})
154-
155-
bindingGroups := maps.MapToSlice(
156-
bindingsByHeader,
157-
func(header header, hBindings []*types.Binding) headerWithBindings {
158-
uniqBindings := lo.UniqBy(hBindings, func(binding *types.Binding) string {
159-
return binding.Description + keybindings.LabelFromKey(binding.Key)
160-
})
161-
162-
return headerWithBindings{
163-
header: header,
164-
bindings: uniqBindings,
165-
}
166-
},
167-
)
168-
169-
slices.SortFunc(bindingGroups, func(a, b headerWithBindings) int {
170-
if a.header.priority != b.header.priority {
171-
return cmp.Compare(b.header.priority, a.header.priority)
172-
}
173-
return strings.Compare(a.header.title, b.header.title)
174-
})
175-
176-
return lo.Map(bindingGroups, func(hb headerWithBindings, _ int) *bindingSection {
177-
return &bindingSection{
178-
title: hb.header.title,
179-
bindings: hb.bindings,
180-
}
181-
})
142+
excludedViews := []string{"stagingSecondary", "patchBuildingSecondary"}
143+
bindingsToDisplay := lo.Filter(bindings, func(binding *types.Binding, _ int) bool {
144+
if lo.Contains(excludedViews, binding.ViewName) {
145+
return false
146+
}
147+
148+
return (binding.Description != "" || binding.Alternative != "") && binding.Key != nil
149+
})
150+
151+
bindingsByHeader := lo.GroupBy(bindingsToDisplay, func(binding *types.Binding) header {
152+
return getHeader(binding, tr)
153+
})
154+
155+
bindingGroups := maps.MapToSlice(
156+
bindingsByHeader,
157+
func(header header, hBindings []*types.Binding) headerWithBindings {
158+
uniqBindings := lo.UniqBy(hBindings, func(binding *types.Binding) string {
159+
return binding.Description + keybindings.LabelFromKey(binding.Key)
160+
})
161+
162+
return headerWithBindings{
163+
header: header,
164+
bindings: uniqBindings,
165+
}
166+
},
167+
)
168+
169+
slices.SortFunc(bindingGroups, func(a, b headerWithBindings) int {
170+
if a.header.priority != b.header.priority {
171+
return cmp.Compare(b.header.priority, a.header.priority)
172+
}
173+
return strings.Compare(a.header.title, b.header.title)
174+
})
175+
176+
return lo.Map(bindingGroups, func(hb headerWithBindings, _ int) *bindingSection {
177+
return &bindingSection{
178+
title: hb.header.title,
179+
bindings: hb.bindings,
180+
}
181+
})
182182
}
183183

184184
func getHeader(binding *types.Binding, tr *i18n.TranslationSet) header {
185-
if binding.Tag == "navigation" {
186-
return header{priority: 2, title: localisedTitle(tr, "navigation")}
187-
}
185+
if binding.Tag == "navigation" {
186+
return header{priority: 2, title: localisedTitle(tr, "navigation")}
187+
}
188188

189-
if binding.ViewName == "" {
190-
return header{priority: 3, title: localisedTitle(tr, "global")}
191-
}
189+
if binding.ViewName == "" {
190+
return header{priority: 3, title: localisedTitle(tr, "global")}
191+
}
192192

193-
return header{priority: 1, title: localisedTitle(tr, binding.ViewName)}
193+
return header{priority: 1, title: localisedTitle(tr, binding.ViewName)}
194194
}
195195

196196
func formatSections(tr *i18n.TranslationSet, bindingSections []*bindingSection) string {
197-
content := fmt.Sprintf("# Lazygit %s\n", tr.Keybindings)
197+
content := fmt.Sprintf("# Lazygit %s\n", tr.Keybindings)
198198

199-
content += fmt.Sprintf("\n%s\n", italicize(tr.KeybindingsLegend))
199+
content += fmt.Sprintf("\n%s\n", italicize(tr.KeybindingsLegend))
200200

201-
for _, section := range bindingSections {
202-
content += formatTitle(section.title)
203-
content += "| Key | Action | Info |\n"
204-
content += "|-----|--------|-------------|\n"
205-
for _, binding := range section.bindings {
206-
content += formatBinding(binding)
207-
}
208-
}
201+
for _, section := range bindingSections {
202+
content += formatTitle(section.title)
203+
content += "| Key | Action | Info |\n"
204+
content += "|-----|--------|-------------|\n"
205+
for _, binding := range section.bindings {
206+
content += formatBinding(binding)
207+
}
208+
}
209209

210-
return content
210+
return content
211211
}
212212

213213
func formatTitle(title string) string {
214-
return fmt.Sprintf("\n## %s\n\n", title)
214+
return fmt.Sprintf("\n## %s\n\n", title)
215215
}
216216

217217
func formatBinding(binding *types.Binding) string {
218-
action := keybindings.LabelFromKey(binding.Key)
219-
description := binding.Description
220-
if binding.Alternative != "" {
221-
action += fmt.Sprintf(" (%s)", binding.Alternative)
222-
}
223-
224-
// Replace newlines with <br> tags for proper markdown table formatting
225-
tooltip := strings.ReplaceAll(binding.Tooltip, "\n\n", "<br><br>")
226-
tooltip = strings.ReplaceAll(tooltip, "\n", "<br>")
227-
228-
// Use backticks for keyboard keys. Two backticks are needed with an inner space
229-
// to escape a key that is itself a backtick.
230-
return fmt.Sprintf("| `` %s `` | %s | %s |\n", action, description, tooltip)
218+
action := keybindings.LabelFromKey(binding.Key)
219+
description := binding.Description
220+
if binding.Alternative != "" {
221+
action += fmt.Sprintf(" (%s)", binding.Alternative)
222+
}
223+
224+
// Replace newlines with <br> tags for proper markdown table formatting
225+
tooltip := strings.ReplaceAll(binding.Tooltip, "\n", "<br>")
226+
227+
// Use backticks for keyboard keys. Two backticks are needed with an inner space
228+
// to escape a key that is itself a backtick.
229+
return fmt.Sprintf("| `` %s `` | %s | %s |\n", action, description, tooltip)
231230
}
232231

233232
func italicize(str string) string {
234-
return fmt.Sprintf("_%s_", str)
233+
return fmt.Sprintf("_%s_", str)
235234
}

0 commit comments

Comments
 (0)