2019-01-16 19:47:15 +02:00
|
|
|
// This "script" generates a file called Keybindings_{{.LANG}}.md
|
|
|
|
// in current working directory.
|
|
|
|
//
|
|
|
|
// The content of this generated file is a keybindings cheatsheet.
|
|
|
|
//
|
|
|
|
// To generate cheatsheet in english run:
|
|
|
|
// LANG=en go run scripts/generate_cheatsheet.go
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-01-16 19:54:54 +02:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
2020-02-29 00:17:19 +02:00
|
|
|
"sort"
|
2019-01-16 19:54:54 +02:00
|
|
|
"strings"
|
2019-02-16 03:07:27 +02:00
|
|
|
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/app"
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/config"
|
2019-03-02 06:11:53 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/gui"
|
2019-01-16 19:47:15 +02:00
|
|
|
)
|
|
|
|
|
2019-03-02 09:45:18 +02:00
|
|
|
type bindingSection struct {
|
|
|
|
title string
|
|
|
|
bindings []*gui.Binding
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2019-03-16 09:40:48 +02:00
|
|
|
langs := []string{"pl", "nl", "en"}
|
2019-03-02 09:45:18 +02:00
|
|
|
mConfig, _ := config.NewAppConfig("", "", "", "", "", true)
|
|
|
|
|
2019-03-16 09:40:48 +02:00
|
|
|
for _, lang := range langs {
|
|
|
|
os.Setenv("LC_ALL", lang)
|
2020-07-18 11:41:13 +02:00
|
|
|
mApp, _ := app.NewApp(mConfig, "")
|
2019-03-22 11:13:48 +02:00
|
|
|
file, err := os.Create(getProjectRoot() + "/docs/keybindings/Keybindings_" + lang + ".md")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-03-02 09:45:18 +02:00
|
|
|
|
2019-03-16 09:40:48 +02:00
|
|
|
bindingSections := getBindingSections(mApp)
|
|
|
|
content := formatSections(mApp, bindingSections)
|
|
|
|
writeString(file, content)
|
|
|
|
}
|
2019-03-02 09:45:18 +02:00
|
|
|
}
|
|
|
|
|
2019-01-16 19:47:15 +02:00
|
|
|
func writeString(file *os.File, str string) {
|
2019-01-16 19:54:54 +02:00
|
|
|
_, err := file.WriteString(str)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2019-01-16 19:47:15 +02:00
|
|
|
}
|
|
|
|
|
2019-03-02 06:11:53 +02:00
|
|
|
func localisedTitle(mApp *app.App, str string) string {
|
|
|
|
viewTitle := strings.Title(str) + "Title"
|
|
|
|
return mApp.Tr.SLocalize(viewTitle)
|
2019-01-16 19:47:15 +02:00
|
|
|
}
|
|
|
|
|
2019-03-02 06:11:53 +02:00
|
|
|
func formatTitle(title string) string {
|
|
|
|
return fmt.Sprintf("\n## %s\n\n", title)
|
|
|
|
}
|
|
|
|
|
2019-03-02 09:45:18 +02:00
|
|
|
func formatBinding(binding *gui.Binding) string {
|
2019-05-03 07:03:25 +02:00
|
|
|
if binding.Alternative != "" {
|
2020-02-29 00:17:19 +02:00
|
|
|
return fmt.Sprintf(" <kbd>%s</kbd>: %s (%s)\n", gui.GetKeyDisplay(binding.Key), binding.Description, binding.Alternative)
|
2019-05-03 07:03:25 +02:00
|
|
|
}
|
2020-02-29 00:17:19 +02:00
|
|
|
return fmt.Sprintf(" <kbd>%s</kbd>: %s\n", gui.GetKeyDisplay(binding.Key), binding.Description)
|
2019-03-02 06:11:53 +02:00
|
|
|
}
|
|
|
|
|
2019-03-02 09:45:18 +02:00
|
|
|
func getBindingSections(mApp *app.App) []*bindingSection {
|
|
|
|
bindingSections := []*bindingSection{}
|
2019-01-16 19:54:54 +02:00
|
|
|
|
2020-02-29 00:17:19 +02:00
|
|
|
bindings := mApp.Gui.GetInitialKeybindings()
|
|
|
|
|
|
|
|
type contextAndViewType struct {
|
|
|
|
context string
|
|
|
|
viewName string
|
|
|
|
}
|
|
|
|
|
|
|
|
contextAndViewBindingMap := map[contextAndViewType][]*gui.Binding{}
|
|
|
|
|
|
|
|
for _, binding := range bindings {
|
|
|
|
contexts := []string{}
|
|
|
|
if len(binding.Contexts) == 0 {
|
|
|
|
contexts = append(contexts, "")
|
|
|
|
} else {
|
2020-03-09 02:34:10 +02:00
|
|
|
contexts = append(contexts, binding.Contexts...)
|
2019-01-16 19:54:54 +02:00
|
|
|
}
|
|
|
|
|
2020-02-29 00:17:19 +02:00
|
|
|
for _, context := range contexts {
|
|
|
|
key := contextAndViewType{context: context, viewName: binding.ViewName}
|
|
|
|
existing := contextAndViewBindingMap[key]
|
|
|
|
if existing == nil {
|
|
|
|
contextAndViewBindingMap[key] = []*gui.Binding{binding}
|
|
|
|
} else {
|
|
|
|
contextAndViewBindingMap[key] = append(contextAndViewBindingMap[key], binding)
|
|
|
|
}
|
2019-01-16 19:54:54 +02:00
|
|
|
}
|
2020-02-29 00:17:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type groupedBindingsType struct {
|
|
|
|
contextAndView contextAndViewType
|
|
|
|
bindings []*gui.Binding
|
|
|
|
}
|
|
|
|
|
|
|
|
groupedBindings := make([]groupedBindingsType, len(contextAndViewBindingMap))
|
2019-01-16 19:54:54 +02:00
|
|
|
|
2020-02-29 00:17:19 +02:00
|
|
|
for contextAndView, contextBindings := range contextAndViewBindingMap {
|
|
|
|
groupedBindings = append(groupedBindings, groupedBindingsType{contextAndView: contextAndView, bindings: contextBindings})
|
2019-01-16 19:54:54 +02:00
|
|
|
}
|
|
|
|
|
2020-02-29 00:17:19 +02:00
|
|
|
sort.Slice(groupedBindings, func(i, j int) bool {
|
|
|
|
first := groupedBindings[i].contextAndView
|
|
|
|
second := groupedBindings[j].contextAndView
|
|
|
|
if first.viewName == "" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if second.viewName == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return first.viewName < second.viewName || (first.viewName == second.viewName && first.context < second.context)
|
|
|
|
})
|
|
|
|
|
|
|
|
for _, group := range groupedBindings {
|
|
|
|
contextAndView := group.contextAndView
|
|
|
|
contextBindings := group.bindings
|
2020-09-26 02:23:10 +02:00
|
|
|
mApp.Log.Info("viewname: " + contextAndView.viewName + ", context: " + contextAndView.context)
|
2020-02-29 00:17:19 +02:00
|
|
|
viewName := contextAndView.viewName
|
|
|
|
if viewName == "" {
|
|
|
|
viewName = "global"
|
|
|
|
}
|
|
|
|
translatedView := localisedTitle(mApp, viewName)
|
|
|
|
var title string
|
|
|
|
if contextAndView.context == "" {
|
|
|
|
addendum := " " + mApp.Tr.SLocalize("Panel")
|
|
|
|
if viewName == "global" {
|
|
|
|
addendum = ""
|
|
|
|
}
|
|
|
|
title = fmt.Sprintf("%s%s", translatedView, addendum)
|
|
|
|
} else {
|
|
|
|
translatedContextName := localisedTitle(mApp, contextAndView.context)
|
|
|
|
title = fmt.Sprintf("%s %s (%s)", translatedView, mApp.Tr.SLocalize("Panel"), translatedContextName)
|
|
|
|
}
|
2019-11-10 07:33:31 +02:00
|
|
|
|
|
|
|
for _, binding := range contextBindings {
|
|
|
|
bindingSections = addBinding(title, bindingSections, binding)
|
2019-03-02 06:11:53 +02:00
|
|
|
}
|
|
|
|
}
|
2019-03-02 09:45:18 +02:00
|
|
|
|
|
|
|
return bindingSections
|
|
|
|
}
|
|
|
|
|
|
|
|
func addBinding(title string, bindingSections []*bindingSection, binding *gui.Binding) []*bindingSection {
|
2019-05-07 08:55:30 +02:00
|
|
|
if binding.Description == "" && binding.Alternative == "" {
|
2019-03-02 09:45:18 +02:00
|
|
|
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 {
|
2020-02-29 00:17:19 +02:00
|
|
|
content := fmt.Sprintf("# Lazygit %s\n", mApp.Tr.SLocalize("Keybindings"))
|
2019-03-02 09:45:18 +02:00
|
|
|
|
|
|
|
for _, section := range bindingSections {
|
|
|
|
content += formatTitle(section.title)
|
|
|
|
content += "<pre>\n"
|
|
|
|
for _, binding := range section.bindings {
|
|
|
|
content += formatBinding(binding)
|
|
|
|
}
|
|
|
|
content += "</pre>\n"
|
|
|
|
}
|
|
|
|
|
|
|
|
return content
|
2019-01-16 19:47:15 +02:00
|
|
|
}
|
2019-03-22 11:13:48 +02:00
|
|
|
|
|
|
|
func getProjectRoot() string {
|
|
|
|
dir, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return strings.Split(dir, "lazygit")[0] + "lazygit"
|
|
|
|
}
|