From 7a170bbccfb5464706694320041567fdf7cd5eff Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Sat, 2 Mar 2019 18:45:18 +1100 Subject: extend cheatsheet generator to contain context based keybindings --- scripts/generate_cheatsheet.go | 99 ++++++++++++++++++++++++++++-------------- 1 file changed, 67 insertions(+), 32 deletions(-) (limited to 'scripts') diff --git a/scripts/generate_cheatsheet.go b/scripts/generate_cheatsheet.go index 92d5880a5..f8beaa457 100644 --- a/scripts/generate_cheatsheet.go +++ b/scripts/generate_cheatsheet.go @@ -19,6 +19,24 @@ import ( "github.com/jesseduffield/lazygit/pkg/gui" ) +type bindingSection struct { + title string + bindings []*gui.Binding +} + +func main() { + mConfig, _ := config.NewAppConfig("", "", "", "", "", true) + mApp, _ := app.NewApp(mConfig) + lang := mApp.Tr.GetLanguage() + file, _ := os.Create("Keybindings_" + lang + ".md") + + bindingSections := getBindingSections(mApp) + + content := formatSections(mApp, bindingSections) + + writeString(file, content) +} + func writeString(file *os.File, str string) { _, err := file.WriteString(str) if err != nil { @@ -35,24 +53,12 @@ func formatTitle(title string) string { return fmt.Sprintf("\n## %s\n\n", title) } -func writeBinding(file *os.File, binding *gui.Binding) { - info := fmt.Sprintf(" %s: %s\n", binding.GetKey(), binding.Description) - writeString(file, info) +func formatBinding(binding *gui.Binding) string { + return fmt.Sprintf(" %s: %s\n", binding.GetKey(), binding.Description) } -// I should really just build an array of tuples, one thing with a string and the other with a list of bindings, and then build them like that. - -func main() { - mConfig, _ := config.NewAppConfig("", "", "", "", "", true) - mApp, _ := app.NewApp(mConfig) - lang := mApp.Tr.GetLanguage() - file, _ := os.Create("Keybindings_" + lang + ".md") - current := "" - - writeString(file, fmt.Sprintf("# Lazygit %s\n", mApp.Tr.SLocalize("menu"))) - writeString(file, formatTitle(localisedTitle(mApp, "global"))) - - writeString(file, "
\n")
+func getBindingSections(mApp *app.App) []*bindingSection {
+	bindingSections := []*bindingSection{}
 
 	// TODO: add context-based keybindings
 	for _, binding := range mApp.Gui.GetInitialKeybindings() {
@@ -60,32 +66,61 @@ func main() {
 			continue
 		}
 
-		if binding.ViewName != current {
-			current = binding.ViewName
-			writeString(file, "
\n") - writeString(file, formatTitle(localisedTitle(mApp, current))) - writeString(file, "
\n")
+		viewName := binding.ViewName
+		if viewName == "" {
+			viewName = "global"
 		}
+		title := localisedTitle(mApp, viewName)
 
-		writeBinding(file, binding)
+		bindingSections = addBinding(title, bindingSections, binding)
 	}
 
-	writeString(file, "
\n") - for view, contexts := range mApp.Gui.GetContextMap() { for contextName, contextBindings := range contexts { translatedView := localisedTitle(mApp, view) translatedContextName := localisedTitle(mApp, contextName) - writeString(file, fmt.Sprintf("\n## %s (%s)\n\n", translatedView, translatedContextName)) - writeString(file, "
\n")
-			for _, binding := range contextBindings {
-				if binding.Description == "" {
-					continue
-				}
+			title := fmt.Sprintf("%s (%s)", translatedView, translatedContextName)
 
-				writeBinding(file, binding)
+			for _, binding := range contextBindings {
+				bindingSections = addBinding(title, bindingSections, binding)
 			}
-			writeString(file, "
\n") } } + + return bindingSections +} + +func addBinding(title string, bindingSections []*bindingSection, binding *gui.Binding) []*bindingSection { + if binding.Description == "" { + return bindingSections + } + + for _, section := range bindingSections { + if title == section.title { + section.bindings = append(section.bindings, binding) + return bindingSections + } + } + + section := &bindingSection{ + title: title, + bindings: []*gui.Binding{binding}, + } + + return append(bindingSections, section) +} + +func formatSections(mApp *app.App, bindingSections []*bindingSection) string { + content := fmt.Sprintf("# Lazygit %s\n", mApp.Tr.SLocalize("menu")) + + for _, section := range bindingSections { + content += formatTitle(section.title) + content += "
\n"
+		for _, binding := range section.bindings {
+			content += formatBinding(binding)
+		}
+		content += "
\n" + } + + return content } -- cgit v1.2.3