|
11 | 11 | package cheatsheet |
12 | 12 |
|
13 | 13 | 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" |
29 | 29 | ) |
30 | 30 |
|
31 | 31 | type bindingSection struct { |
32 | | - title string |
33 | | - bindings []*types.Binding |
| 32 | + title string |
| 33 | + bindings []*types.Binding |
34 | 34 | } |
35 | 35 |
|
36 | 36 | 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 |
40 | 40 | } |
41 | 41 |
|
42 | 42 | type headerWithBindings struct { |
43 | | - header header |
44 | | - bindings []*types.Binding |
| 43 | + header header |
| 44 | + bindings []*types.Binding |
45 | 45 | } |
46 | 46 |
|
47 | 47 | func CommandToRun() string { |
48 | | - return "go generate ./..." |
| 48 | + return "go generate ./..." |
49 | 49 | } |
50 | 50 |
|
51 | 51 | func GetKeybindingsDir() string { |
52 | | - return utils.GetLazyRootDirectory() + "/docs/keybindings" |
| 52 | + return utils.GetLazyRootDirectory() + "/docs/keybindings" |
53 | 53 | } |
54 | 54 |
|
55 | 55 | 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 | + } |
87 | 87 | } |
88 | 88 |
|
89 | 89 | func Generate() { |
90 | | - generateAtDir(GetKeybindingsDir()) |
| 90 | + generateAtDir(GetKeybindingsDir()) |
91 | 91 | } |
92 | 92 |
|
93 | 93 | 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 | + } |
98 | 98 | } |
99 | 99 |
|
100 | 100 | 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 |
139 | 139 | } |
140 | 140 |
|
141 | 141 | 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 | + }) |
182 | 182 | } |
183 | 183 |
|
184 | 184 | 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 | + } |
188 | 188 |
|
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 | + } |
192 | 192 |
|
193 | | - return header{priority: 1, title: localisedTitle(tr, binding.ViewName)} |
| 193 | + return header{priority: 1, title: localisedTitle(tr, binding.ViewName)} |
194 | 194 | } |
195 | 195 |
|
196 | 196 | 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) |
198 | 198 |
|
199 | | - content += fmt.Sprintf("\n%s\n", italicize(tr.KeybindingsLegend)) |
| 199 | + content += fmt.Sprintf("\n%s\n", italicize(tr.KeybindingsLegend)) |
200 | 200 |
|
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 | + } |
209 | 209 |
|
210 | | - return content |
| 210 | + return content |
211 | 211 | } |
212 | 212 |
|
213 | 213 | func formatTitle(title string) string { |
214 | | - return fmt.Sprintf("\n## %s\n\n", title) |
| 214 | + return fmt.Sprintf("\n## %s\n\n", title) |
215 | 215 | } |
216 | 216 |
|
217 | 217 | 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) |
231 | 230 | } |
232 | 231 |
|
233 | 232 | func italicize(str string) string { |
234 | | - return fmt.Sprintf("_%s_", str) |
| 233 | + return fmt.Sprintf("_%s_", str) |
235 | 234 | } |
0 commit comments