From a09bb5d4d83c9738969b337d82e14e4bbe34dd53 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Sun, 27 Sep 2020 09:37:22 +1000 Subject: [PATCH] better validation messages --- pkg/gui/cherry_picking.go | 2 +- pkg/gui/context.go | 41 ++++++++++++++++++++++++++++++++++---- pkg/gui/custom_commands.go | 14 ++++++++----- pkg/gui/keybindings.go | 2 +- pkg/gui/remotes_panel.go | 2 +- 5 files changed, 49 insertions(+), 12 deletions(-) diff --git a/pkg/gui/cherry_picking.go b/pkg/gui/cherry_picking.go index 4b853d637..99da59f98 100644 --- a/pkg/gui/cherry_picking.go +++ b/pkg/gui/cherry_picking.go @@ -176,7 +176,7 @@ func (gui *Gui) rerenderContextViewIfPresent(contextKey string) error { return nil } - context := gui.contextForContextKey(contextKey) + context := gui.mustContextForContextKey(contextKey) viewName := context.GetViewName() diff --git a/pkg/gui/context.go b/pkg/gui/context.go index c84e604b8..c48626a44 100644 --- a/pkg/gui/context.go +++ b/pkg/gui/context.go @@ -36,6 +36,29 @@ const ( COMMIT_MESSAGE_CONTEXT_KEY = "commitMessage" ) +var allContextKeys = []string{ + STATUS_CONTEXT_KEY, + FILES_CONTEXT_KEY, + LOCAL_BRANCHES_CONTEXT_KEY, + REMOTES_CONTEXT_KEY, + REMOTE_BRANCHES_CONTEXT_KEY, + TAGS_CONTEXT_KEY, + BRANCH_COMMITS_CONTEXT_KEY, + REFLOG_COMMITS_CONTEXT_KEY, + SUB_COMMITS_CONTEXT_KEY, + COMMIT_FILES_CONTEXT_KEY, + STASH_CONTEXT_KEY, + MAIN_NORMAL_CONTEXT_KEY, + MAIN_MERGING_CONTEXT_KEY, + MAIN_PATCH_BUILDING_CONTEXT_KEY, + MAIN_STAGING_CONTEXT_KEY, + MENU_CONTEXT_KEY, + CREDENTIALS_CONTEXT_KEY, + CONFIRMATION_CONTEXT_KEY, + SEARCH_CONTEXT_KEY, + COMMIT_MESSAGE_CONTEXT_KEY, +} + type Context interface { HandleFocus() error HandleFocusLost() error @@ -674,14 +697,24 @@ type tabContext struct { contexts []Context } -func (gui *Gui) contextForContextKey(contextKey string) Context { +func (gui *Gui) mustContextForContextKey(contextKey string) Context { + context, ok := gui.contextForContextKey(contextKey) + + if !ok { + panic(fmt.Sprintf("context now found for key %s", contextKey)) + } + + return context +} + +func (gui *Gui) contextForContextKey(contextKey string) (Context, bool) { for _, context := range gui.allContexts() { if context.GetKey() == contextKey { - return context + return context, true } } - panic(fmt.Sprintf("context now found for key %s", contextKey)) + return nil, false } func (gui *Gui) rerenderView(viewName string) error { @@ -691,7 +724,7 @@ func (gui *Gui) rerenderView(viewName string) error { } contextKey := v.Context - context := gui.contextForContextKey(contextKey) + context := gui.mustContextForContextKey(contextKey) return context.HandleRender() } diff --git a/pkg/gui/custom_commands.go b/pkg/gui/custom_commands.go index bf1e97b02..655e1771f 100644 --- a/pkg/gui/custom_commands.go +++ b/pkg/gui/custom_commands.go @@ -3,6 +3,7 @@ package gui import ( "bytes" "log" + "strings" "text/template" "github.com/fatih/color" @@ -211,12 +212,15 @@ func (gui *Gui) GetCustomCommandKeybindings() []*Binding { for _, customCommand := range customCommands { var viewName string - if customCommand.Context == "global" || customCommand.Context == "" { + switch customCommand.Context { + case "global": viewName = "" - } else { - context := gui.contextForContextKey(customCommand.Context) - if context == nil { - log.Fatalf("Error when setting custom command keybindings: unknown context: %s", customCommand.Context) + case "": + log.Fatalf("Error parsing custom command keybindings: context not provided (use context: 'global' for the global context). Key: %s, Command: %s", customCommand.Key, customCommand.Command) + default: + context, ok := gui.contextForContextKey(customCommand.Context) + if !ok { + log.Fatalf("Error when setting custom command keybindings: unknown context: %s. Key: %s, Command: %s.\nPermitted contexts: %s", customCommand.Context, customCommand.Key, customCommand.Command, strings.Join(allContextKeys, ", ")) } // here we assume that a given context will always belong to the same view. // Currently this is a safe bet but it's by no means guaranteed in the long term diff --git a/pkg/gui/keybindings.go b/pkg/gui/keybindings.go index d7b62051d..9ef0019b2 100644 --- a/pkg/gui/keybindings.go +++ b/pkg/gui/keybindings.go @@ -189,7 +189,7 @@ func (gui *Gui) getKey(name string) interface{} { if runeCount > 1 { binding := keymap[strings.ToLower(key)] if binding == nil { - log.Fatalf("Unrecognized key %s for keybinding %s", strings.ToLower(key), name) + log.Fatalf("Unrecognized key %s for keybinding %s. For permitted values see https://github.com/jesseduffield/lazygit/blob/master/docs/keybindings/Custom_Keybindings.md", strings.ToLower(key), name) } else { return binding } diff --git a/pkg/gui/remotes_panel.go b/pkg/gui/remotes_panel.go index e32eb77c6..102ba9e33 100644 --- a/pkg/gui/remotes_panel.go +++ b/pkg/gui/remotes_panel.go @@ -58,7 +58,7 @@ func (gui *Gui) refreshRemotes() error { } } - return gui.postRefreshUpdate(gui.contextForContextKey(gui.getBranchesView().Context)) + return gui.postRefreshUpdate(gui.mustContextForContextKey(gui.getBranchesView().Context)) } func (gui *Gui) handleRemoteEnter() error {