summaryrefslogtreecommitdiffstats
path: root/scripts
diff options
context:
space:
mode:
authorJesse Duffield <jessedduffield@gmail.com>2020-02-29 09:17:19 +1100
committerJesse Duffield <jessedduffield@gmail.com>2020-02-29 17:46:00 +1100
commita7508a5dfda7a06f5a9d08d4f9f3f957ac589f88 (patch)
treeaa7c452eff1f3197785b9162a90efe1102006ad0 /scripts
parent1a3d765c4cd685e2f503fa048037d70d1227776a (diff)
fix cheatsheet script to support different contexts
Diffstat (limited to 'scripts')
-rw-r--r--scripts/generate_cheatsheet.go87
1 files changed, 71 insertions, 16 deletions
diff --git a/scripts/generate_cheatsheet.go b/scripts/generate_cheatsheet.go
index 56059d3bc..c5afdf5e9 100644
--- a/scripts/generate_cheatsheet.go
+++ b/scripts/generate_cheatsheet.go
@@ -12,6 +12,7 @@ import (
"fmt"
"log"
"os"
+ "sort"
"strings"
"github.com/jesseduffield/lazygit/pkg/app"
@@ -60,33 +61,87 @@ func formatTitle(title string) string {
func formatBinding(binding *gui.Binding) string {
if binding.Alternative != "" {
- return fmt.Sprintf(" <kbd>%s</kbd>: %s (%s)\n", binding.GetKey(), binding.Description, binding.Alternative)
+ return fmt.Sprintf(" <kbd>%s</kbd>: %s (%s)\n", gui.GetKeyDisplay(binding.Key), binding.Description, binding.Alternative)
}
- return fmt.Sprintf(" <kbd>%s</kbd>: %s\n", binding.GetKey(), binding.Description)
+ return fmt.Sprintf(" <kbd>%s</kbd>: %s\n", gui.GetKeyDisplay(binding.Key), binding.Description)
}
func getBindingSections(mApp *app.App) []*bindingSection {
bindingSections := []*bindingSection{}
- // TODO: add context-based keybindings
- for _, binding := range mApp.Gui.GetInitialKeybindings() {
- if binding.Description == "" {
- continue
+ 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 {
+ for _, context := range binding.Contexts {
+ contexts = append(contexts, context)
+ }
}
- viewName := binding.ViewName
- if viewName == "" {
- viewName = "global"
+ 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)
+ }
}
- title := localisedTitle(mApp, viewName)
+ }
+
+ type groupedBindingsType struct {
+ contextAndView contextAndViewType
+ bindings []*gui.Binding
+ }
+
+ groupedBindings := make([]groupedBindingsType, len(contextAndViewBindingMap))
- bindingSections = addBinding(title, bindingSections, binding)
+ for contextAndView, contextBindings := range contextAndViewBindingMap {
+ groupedBindings = append(groupedBindings, groupedBindingsType{contextAndView: contextAndView, bindings: contextBindings})
}
- for contextName, contextBindings := range mApp.Gui.GetContextMap() {
- translatedView := localisedTitle(mApp, contextBindings[0].ViewName)
- translatedContextName := localisedTitle(mApp, contextName)
- title := fmt.Sprintf("%s (%s)", translatedView, translatedContextName)
+ 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
+ mApp.Log.Warn("viewname: " + contextAndView.viewName + ", context: " + contextAndView.context)
+ 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)
+ }
for _, binding := range contextBindings {
bindingSections = addBinding(title, bindingSections, binding)
@@ -117,7 +172,7 @@ func addBinding(title string, bindingSections []*bindingSection, binding *gui.Bi
}
func formatSections(mApp *app.App, bindingSections []*bindingSection) string {
- content := fmt.Sprintf("# Lazygit %s\n", mApp.Tr.SLocalize("menu"))
+ content := fmt.Sprintf("# Lazygit %s\n", mApp.Tr.SLocalize("Keybindings"))
for _, section := range bindingSections {
content += formatTitle(section.title)