From 359636c1aa394bf8b619a7287f504e6afcf11470 Mon Sep 17 00:00:00 2001 From: Dawid Dziurla Date: Sat, 1 Sep 2018 16:50:22 +0200 Subject: [PATCH] add generate_cheatsheet script script is generating markdown document with small cheatsheet in selected language --- pkg/gui/help_panel.go | 26 ++++++++++++--------- pkg/gui/keybindings.go | 4 ++-- scripts/generate_cheatsheet.go | 41 ++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 13 deletions(-) create mode 100644 scripts/generate_cheatsheet.go diff --git a/pkg/gui/help_panel.go b/pkg/gui/help_panel.go index 3c102f2dc..ffb0628b7 100644 --- a/pkg/gui/help_panel.go +++ b/pkg/gui/help_panel.go @@ -47,23 +47,27 @@ func (gui *Gui) handleHelpClose(g *gocui.Gui, v *gocui.View) error { return gui.returnFocus(g, v) } +func (gui *Gui) GetKey(binding Binding) string { + r, ok := binding.Key.(rune) + key := "" + + if ok { + key = string(r) + } else if binding.KeyReadable != "" { + key = binding.KeyReadable + } + + return key +} + func (gui *Gui) handleHelp(g *gocui.Gui, v *gocui.View) error { // clear keys slice, so we don't have ghost elements keys = keys[:0] content := "" - bindings := gui.getKeybindings() + bindings := gui.GetKeybindings() for _, binding := range bindings { - r, ok := binding.Key.(rune) - key := "" - - if ok { - key = string(r) - } else if binding.KeyReadable != "" { - key = binding.KeyReadable - } - - if key != "" && binding.ViewName == v.Name() && binding.Description != "" { + if key := gui.GetKey(binding); key != "" && binding.ViewName == v.Name() && binding.Description != "" { content += fmt.Sprintf(" %s - %s\n", key, binding.Description) keys = append(keys, binding) } diff --git a/pkg/gui/keybindings.go b/pkg/gui/keybindings.go index 34c3e0f68..034f907bf 100644 --- a/pkg/gui/keybindings.go +++ b/pkg/gui/keybindings.go @@ -14,7 +14,7 @@ type Binding struct { Description string } -func (gui *Gui) getKeybindings() []Binding { +func (gui *Gui) GetKeybindings() []Binding { bindings := []Binding{ { ViewName: "", @@ -379,7 +379,7 @@ func (gui *Gui) getKeybindings() []Binding { } func (gui *Gui) keybindings(g *gocui.Gui) error { - bindings := gui.getKeybindings() + bindings := gui.GetKeybindings() for _, binding := range bindings { if err := g.SetKeybinding(binding.ViewName, binding.Key, binding.Modifier, binding.Handler); err != nil { diff --git a/scripts/generate_cheatsheet.go b/scripts/generate_cheatsheet.go new file mode 100644 index 000000000..87ebe43c5 --- /dev/null +++ b/scripts/generate_cheatsheet.go @@ -0,0 +1,41 @@ +// run: +// LANG=en go run generate_cheatsheet.go +// to generate Keybindings_en.md file in current directory +// change LANG to generate cheatsheet in different language (if supported) + +package main + +import ( + "os" + "fmt" + "strings" + + "github.com/jesseduffield/lazygit/pkg/app" + "github.com/jesseduffield/lazygit/pkg/config" +) + +func main() { + appConfig, _ := config.NewAppConfig("", "", "", "", "", new(bool)) + a, _ := app.NewApp(appConfig) + lang := a.Tr.GetLanguage() + name := "Keybindings_" + lang + ".md" + bindings := a.Gui.GetKeybindings() + file, _ := os.Create(name) + current := "" + content := "" + + file.WriteString("# Lazygit " + a.Tr.SLocalize("help")) + + for _, binding := range bindings { + if key := a.Gui.GetKey(binding); key != "" && binding.Description != "" { + if binding.ViewName != current { + current = binding.ViewName + title := a.Tr.SLocalize(strings.Title(current) + "Title") + content = fmt.Sprintf("\n\n## %s\n
\n", title)
+				file.WriteString(content)
+			}
+			content = fmt.Sprintf("\t%s:\t%s\n", key, binding.Description)
+			file.WriteString(content)
+		}
+	}
+}