From 28a9594ef7f7dad70efa9470ed12e5d102f6c5df Mon Sep 17 00:00:00 2001 From: Dawid Dziurla Date: Wed, 29 Aug 2018 13:43:59 +0200 Subject: [PATCH] update help panel - delete scrolling ability - lines are now selectable - implemented handler execution when space is pressed - add example descriptions for status panel keybindings --- pkg/gui/files_panel.go | 2 +- pkg/gui/help_panel.go | 76 ++++++++++++++++++++++++----------------- pkg/gui/keybindings.go | 11 +++--- pkg/gui/view_helpers.go | 7 ++-- pkg/i18n/english.go | 3 ++ 5 files changed, 56 insertions(+), 43 deletions(-) diff --git a/pkg/gui/files_panel.go b/pkg/gui/files_panel.go index 90f2b7e84..cf98e8a44 100644 --- a/pkg/gui/files_panel.go +++ b/pkg/gui/files_panel.go @@ -185,7 +185,7 @@ func (gui *Gui) renderfilesOptions(g *gocui.Gui, file *commands.File) error { "e": gui.Tr.SLocalize("edit"), "a": gui.Tr.SLocalize("toggleStagedAll"), "PgUp/PgDn": gui.Tr.SLocalize("scroll"), - "?": gui.Tr.SLocalize("help"), + "?": gui.Tr.SLocalize("help"), //TODO make it visible for all panels, not only files } if gui.State.HasMergeConflicts { optionsMap["a"] = gui.Tr.SLocalize("abortMerge") diff --git a/pkg/gui/help_panel.go b/pkg/gui/help_panel.go index 7fb6a69b5..6ef8def7f 100644 --- a/pkg/gui/help_panel.go +++ b/pkg/gui/help_panel.go @@ -2,62 +2,74 @@ package gui import ( "fmt" - "github.com/jesseduffield/gocui" "strings" + + "github.com/jesseduffield/gocui" ) +var keys []Binding + +func (gui *Gui) handleHelpPress(g *gocui.Gui, v *gocui.View) error { + lineNumber := gui.getItemPosition(v) + err := gui.handleHelpClose(g, v) + if err != nil { + return err + } + return keys[lineNumber].Handler(g, v) +} + +func (gui *Gui) handleHelpSelect(g *gocui.Gui, v *gocui.View) error { + // doing nothing for now + // but it is needed for switch in newLineFocused + return nil +} + func (gui *Gui) renderHelpOptions(g *gocui.Gui) error { optionsMap := map[string]string{ - "esc/q": gui.Tr.SLocalize("close"), - "PgUp/PgDn": gui.Tr.SLocalize("scroll"), + "esc/q": gui.Tr.SLocalize("close"), + "↑ ↓": gui.Tr.SLocalize("navigate"), + "space": gui.Tr.SLocalize("execute"), } return gui.renderOptionsMap(g, optionsMap) } -func (gui *Gui) scrollUpHelp(g *gocui.Gui, v *gocui.View) error { - mainView, _ := g.View("help") - ox, oy := mainView.Origin() - if oy >= 1 { - return mainView.SetOrigin(ox, oy-gui.Config.GetUserConfig().GetInt("gui.scrollHeight")) - } - return nil -} - -func (gui *Gui) scrollDownHelp(g *gocui.Gui, v *gocui.View) error { - mainView, _ := g.View("help") - ox, oy := mainView.Origin() - if oy < len(mainView.BufferLines()) { - return mainView.SetOrigin(ox, oy+gui.Config.GetUserConfig().GetInt("gui.scrollHeight")) - } - return nil -} - func (gui *Gui) handleHelpClose(g *gocui.Gui, v *gocui.View) error { - g.SetViewOnBottom(v.Name()) - return gui.switchFocus(g, v, gui.getFilesView(g)) + // better to delete because for example after closing update confirmation panel, + // the focus isn't set back to any of panels and one is unable to even quit + //_, err := g.SetViewOnBottom(v.Name()) + err := g.DeleteView(v.Name()) + if err != nil { + return err + } + return gui.returnFocus(g, v) } 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 := "" - curr := "" bindings := gui.getKeybindings() maxX, maxY := g.Size() - helpView, _ := g.SetView("help", 0, 0, maxX-1, maxY-2, 0) + x := maxX * 3 / 4 + y := 5 + helpView, _ := g.SetView("help", maxX-x, y, x, maxY-y, 0) helpView.Title = strings.Title(gui.Tr.SLocalize("help")) gui.renderHelpOptions(g) for _, binding := range bindings { - if binding.Description != "" { - if curr != binding.ViewName { - curr = binding.ViewName - content += fmt.Sprintf("\n%s:\n", strings.Title(curr)) - } - content += fmt.Sprintf(" %s - %s\n", binding.KeyReadable, binding.Description) + if binding.ViewName == v.Name() && binding.Description != "" && binding.KeyReadable != "" { + content += fmt.Sprintf(" %s - %s\n", binding.KeyReadable, binding.Description) + keys = append(keys, binding) } } - helpView.Write([]byte(content)) + // for testing + /*content += "first\n" + content += "second\n" + content += "third\n" + */ + gui.renderString(g, "help", content) g.Update(func(g *gocui.Gui) error { g.SetViewOnTop("help") diff --git a/pkg/gui/keybindings.go b/pkg/gui/keybindings.go index bdc5daf5d..e8ddb68d5 100644 --- a/pkg/gui/keybindings.go +++ b/pkg/gui/keybindings.go @@ -27,9 +27,9 @@ func (gui *Gui) getKeybindings() []Binding { {ViewName: "", Key: 'p', Modifier: gocui.ModNone, Handler: gui.pullFiles}, {ViewName: "", Key: 'R', Modifier: gocui.ModNone, Handler: gui.handleRefresh}, {ViewName: "", Key: '?', Modifier: gocui.ModNone, Handler: gui.handleHelp}, - {ViewName: "status", Key: 'e', Modifier: gocui.ModNone, Handler: gui.handleEditConfig}, - {ViewName: "status", Key: 'o', Modifier: gocui.ModNone, Handler: gui.handleOpenConfig}, - {ViewName: "status", Key: 'u', Modifier: gocui.ModNone, Handler: gui.handleCheckForUpdate}, + {ViewName: "status", Key: 'e', Modifier: gocui.ModNone, Handler: gui.handleEditConfig, KeyReadable: "e", Description: "edit config"}, + {ViewName: "status", Key: 'o', Modifier: gocui.ModNone, Handler: gui.handleOpenConfig, KeyReadable: "o", Description: "open config"}, + {ViewName: "status", Key: 'u', Modifier: gocui.ModNone, Handler: gui.handleCheckForUpdate, KeyReadable: "u", Description: "check for update"}, {ViewName: "files", Key: 'c', Modifier: gocui.ModNone, Handler: gui.handleCommitPress}, {ViewName: "files", Key: 'C', Modifier: gocui.ModNone, Handler: gui.handleCommitEditorPress}, {ViewName: "files", Key: gocui.KeySpace, Modifier: gocui.ModNone, Handler: gui.handleFilePress}, @@ -76,13 +76,12 @@ func (gui *Gui) getKeybindings() []Binding { {ViewName: "commitMessage", Key: gocui.KeyTab, Modifier: gocui.ModNone, Handler: gui.handleNewlineCommitMessage}, {ViewName: "help", Key: gocui.KeyEsc, Modifier: gocui.ModNone, Handler: gui.handleHelpClose}, {ViewName: "help", Key: 'q', Modifier: gocui.ModNone, Handler: gui.handleHelpClose}, - {ViewName: "help", Key: gocui.KeyPgup, Modifier: gocui.ModNone, Handler: gui.scrollUpHelp}, - {ViewName: "help", Key: gocui.KeyPgdn, Modifier: gocui.ModNone, Handler: gui.scrollDownHelp}, + {ViewName: "help", Key: gocui.KeySpace, Modifier: gocui.ModNone, Handler: gui.handleHelpPress}, } // Would make these keybindings global but that interferes with editing // input in the confirmation panel - for _, viewName := range []string{"status", "files", "branches", "commits", "stash"} { + for _, viewName := range []string{"status", "files", "branches", "commits", "stash", "help"} { bindings = append(bindings, []Binding{ {ViewName: viewName, Key: gocui.KeyTab, Modifier: gocui.ModNone, Handler: gui.nextView}, {ViewName: viewName, Key: gocui.KeyArrowLeft, Modifier: gocui.ModNone, Handler: gui.previousView}, diff --git a/pkg/gui/view_helpers.go b/pkg/gui/view_helpers.go index 9cebbf3c7..4cab2de08 100644 --- a/pkg/gui/view_helpers.go +++ b/pkg/gui/view_helpers.go @@ -82,6 +82,8 @@ func (gui *Gui) newLineFocused(g *gocui.Gui, v *gocui.View) error { mainView.SetOrigin(0, 0) switch v.Name() { + case "help": + return gui.handleHelpSelect(g, v) case "status": return gui.handleStatusSelect(g, v) case "files": @@ -146,10 +148,7 @@ func (gui *Gui) switchFocus(g *gocui.Gui, oldView, newView *gocui.View) error { } g.Cursor = newView.Editable - if newView.Name() != "help" { - return gui.newLineFocused(g, newView) - } - return nil + return gui.newLineFocused(g, newView) } func (gui *Gui) getItemPosition(v *gocui.View) int { diff --git a/pkg/i18n/english.go b/pkg/i18n/english.go index 1527b7faf..c29138e42 100644 --- a/pkg/i18n/english.go +++ b/pkg/i18n/english.go @@ -51,6 +51,9 @@ func addEnglish(i18nObject *i18n.Bundle) error { }, &i18n.Message{ ID: "help", Other: "help", + }, &i18n.Message{ + ID: "execute", + Other: "execute", }, &i18n.Message{ ID: "stashFiles", Other: "stash files",