summaryrefslogtreecommitdiffstats
path: root/scripts/generate_cheatsheet.go
blob: 86308901454159367de0f37b97043cce9209e4bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// 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 (
	"fmt"
	"os"
	"strings"

	"github.com/jesseduffield/lazygit/pkg/app"
	"github.com/jesseduffield/lazygit/pkg/config"
	"github.com/jesseduffield/lazygit/pkg/utils"
)

func main() {
	appConfig, _ := config.NewAppConfig("", "", "", "", "", new(bool))
	a, _ := app.NewApp(appConfig)
	lang := a.Tr.GetLanguage()
	name := "Keybindings_" + lang + ".md"
	bindings := a.Gui.GetKeybindings()
	padWidth := a.Gui.GetMaxKeyLength(bindings)
	file, _ := os.Create(name)
	current := "v"
	content := ""
	title := ""

	file.WriteString("# Lazygit " + a.Tr.SLocalize("menu"))

	for _, binding := range bindings {
		if key := a.Gui.GetKey(binding); key != "" && (binding.Description != "" || key == "?") {
			if binding.ViewName != current {
				current = binding.ViewName
				if current == "" {
					title = a.Tr.SLocalize("GlobalTitle")
				} else {
					title = a.Tr.SLocalize(strings.Title(current) + "Title")
				}
				content = fmt.Sprintf("</pre>\n\n## %s\n<pre>\n", title)
				file.WriteString(content)
			}
			// workaround to include menu keybinding in cheatsheet
			// could not add this Description field directly to keybindings.go,
			// because then menu key would be displayed in menu itself and that is undesirable
			if key == "?" {
				binding.Description = a.Tr.SLocalize("menu")
			}
			content = fmt.Sprintf("\t<kbd>%s</kbd>%s  %s\n", key, strings.TrimPrefix(utils.WithPadding(key, padWidth), key), binding.Description)
			file.WriteString(content)
		}
	}
}