From b3bce8a1bacc602b25ad367c943c01b9cc6d2c10 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Sun, 17 Nov 2019 17:58:24 +1100 Subject: refactor confirmation prompt code --- pkg/gui/confirmation_panel.go | 68 +++++++++++++++++++------------------------ pkg/i18n/dutch.go | 3 -- pkg/i18n/english.go | 3 -- pkg/i18n/polish.go | 3 -- 4 files changed, 30 insertions(+), 47 deletions(-) diff --git a/pkg/gui/confirmation_panel.go b/pkg/gui/confirmation_panel.go index 0638f6fba..f204672c8 100644 --- a/pkg/gui/confirmation_panel.go +++ b/pkg/gui/confirmation_panel.go @@ -18,17 +18,13 @@ import ( func (gui *Gui) wrappedConfirmationFunction(function func(*gocui.Gui, *gocui.View) error, returnFocusOnClose bool) func(*gocui.Gui, *gocui.View) error { return func(g *gocui.Gui, v *gocui.View) error { - if err := gui.closeConfirmationPrompt(g, returnFocusOnClose); err != nil { - return err - } - if function != nil { if err := function(g, v); err != nil { return err } } - return nil + return gui.closeConfirmationPrompt(g, returnFocusOnClose) } } @@ -37,6 +33,7 @@ func (gui *Gui) closeConfirmationPrompt(g *gocui.Gui, returnFocusOnClose bool) e if err != nil { return nil // if it's already been closed we can just return } + view.Editable = false if returnFocusOnClose { if err := gui.returnFocus(g, view); err != nil { panic(err) @@ -70,20 +67,6 @@ func (gui *Gui) getConfirmationPanelDimensions(g *gocui.Gui, wrap bool, prompt s height/2 + panelHeight/2 } -func (gui *Gui) createPromptPanel(g *gocui.Gui, currentView *gocui.View, title string, initialContent string, handleConfirm func(*gocui.Gui, *gocui.View) error) error { - gui.onNewPopupPanel() - confirmationView, err := gui.prepareConfirmationPanel(currentView, title, initialContent, false) - if err != nil { - return err - } - confirmationView.Editable = true - if err := gui.renderString(g, "confirmation", initialContent); err != nil { - return err - } - // in the future we might want to give createPromptPanel the returnFocusOnClose arg too, but for now we're always setting it to true - return gui.setKeyBindings(g, handleConfirm, nil, true) -} - func (gui *Gui) prepareConfirmationPanel(currentView *gocui.View, title, prompt string, hasLoader bool) (*gocui.View, error) { x0, y0, x1, y1 := gui.getConfirmationPanelDimensions(gui.g, true, prompt) confirmationView, err := gui.g.SetView("confirmation", x0, y0, x1, y1, 0) @@ -111,35 +94,31 @@ func (gui *Gui) onNewPopupPanel() { } } -func (gui *Gui) createLoaderPanel(g *gocui.Gui, currentView *gocui.View, prompt string) error { - return gui.createPopupPanel(g, currentView, "", prompt, true, true, nil, nil) -} - -// it is very important that within this function we never include the original prompt in any error messages, because it may contain e.g. a user password -func (gui *Gui) createConfirmationPanel(g *gocui.Gui, currentView *gocui.View, returnFocusOnClose bool, title, prompt string, handleConfirm, handleClose func(*gocui.Gui, *gocui.View) error) error { - return gui.createPopupPanel(g, currentView, title, prompt, false, returnFocusOnClose, handleConfirm, handleClose) -} - -func (gui *Gui) createPopupPanel(g *gocui.Gui, currentView *gocui.View, title, prompt string, hasLoader bool, returnFocusOnClose bool, handleConfirm, handleClose func(*gocui.Gui, *gocui.View) error) error { +func (gui *Gui) createPopupPanel(g *gocui.Gui, currentView *gocui.View, title, prompt string, hasLoader bool, returnFocusOnClose bool, editable bool, handleConfirm, handleClose func(*gocui.Gui, *gocui.View) error) error { gui.onNewPopupPanel() g.Update(func(g *gocui.Gui) error { // delete the existing confirmation panel if it exists if view, _ := g.View("confirmation"); view != nil { if err := gui.closeConfirmationPrompt(g, true); err != nil { - errMessage := gui.Tr.TemplateLocalize( - "CantCloseConfirmationPrompt", - Teml{ - "error": err.Error(), - }, - ) - gui.Log.Error(errMessage) + gui.Log.Error(err) } } confirmationView, err := gui.prepareConfirmationPanel(currentView, title, prompt, hasLoader) if err != nil { return err } - confirmationView.Editable = false + confirmationView.Editable = editable + if editable { + go func() { + // TODO: remove this wait (right now if you remove it the EditGotoToEndOfLine method doesn't seem to work) + time.Sleep(time.Millisecond) + gui.g.Update(func(g *gocui.Gui) error { + confirmationView.EditGotoToEndOfLine() + return nil + }) + }() + } + if err := gui.renderString(g, "confirmation", prompt); err != nil { return err } @@ -148,6 +127,19 @@ func (gui *Gui) createPopupPanel(g *gocui.Gui, currentView *gocui.View, title, p return nil } +func (gui *Gui) createLoaderPanel(g *gocui.Gui, currentView *gocui.View, prompt string) error { + return gui.createPopupPanel(g, currentView, "", prompt, true, true, false, nil, nil) +} + +// it is very important that within this function we never include the original prompt in any error messages, because it may contain e.g. a user password +func (gui *Gui) createConfirmationPanel(g *gocui.Gui, currentView *gocui.View, returnFocusOnClose bool, title, prompt string, handleConfirm, handleClose func(*gocui.Gui, *gocui.View) error) error { + return gui.createPopupPanel(g, currentView, title, prompt, false, returnFocusOnClose, false, handleConfirm, handleClose) +} + +func (gui *Gui) createPromptPanel(g *gocui.Gui, currentView *gocui.View, title string, initialContent string, handleConfirm func(*gocui.Gui, *gocui.View) error) error { + return gui.createPopupPanel(gui.g, currentView, title, initialContent, false, true, true, handleConfirm, nil) +} + func (gui *Gui) setKeyBindings(g *gocui.Gui, handleConfirm, handleClose func(*gocui.Gui, *gocui.View) error, returnFocusOnClose bool) error { actions := gui.Tr.TemplateLocalize( "CloseConfirm", @@ -166,7 +158,7 @@ func (gui *Gui) setKeyBindings(g *gocui.Gui, handleConfirm, handleClose func(*go } func (gui *Gui) createMessagePanel(g *gocui.Gui, currentView *gocui.View, title, prompt string) error { - return gui.createPopupPanel(g, currentView, title, prompt, false, true, nil, nil) + return gui.createPopupPanel(g, currentView, title, prompt, false, true, false, nil, nil) } // createSpecificErrorPanel allows you to create an error popup, specifying the diff --git a/pkg/i18n/dutch.go b/pkg/i18n/dutch.go index ca76f0a5a..323d7fafa 100644 --- a/pkg/i18n/dutch.go +++ b/pkg/i18n/dutch.go @@ -337,9 +337,6 @@ func addDutch(i18nObject *i18n.Bundle) error { }, &i18n.Message{ ID: "newFocusedViewIs", Other: "nieuw gefocussed weergave is {{.newFocusedView}}", - }, &i18n.Message{ - ID: "CantCloseConfirmationPrompt", - Other: "Kon de bevestiging prompt niet sluiten: {{.error}}", }, &i18n.Message{ ID: "MergeAborted", Other: "Merge afgebroken", diff --git a/pkg/i18n/english.go b/pkg/i18n/english.go index d39b4b279..5a5622f6c 100644 --- a/pkg/i18n/english.go +++ b/pkg/i18n/english.go @@ -399,9 +399,6 @@ func addEnglish(i18nObject *i18n.Bundle) error { }, &i18n.Message{ ID: "newFocusedViewIs", Other: "new focused view is {{.newFocusedView}}", - }, &i18n.Message{ - ID: "CantCloseConfirmationPrompt", - Other: "Could not close confirmation prompt: {{.error}}", }, &i18n.Message{ ID: "NoChangedFiles", Other: "No changed files", diff --git a/pkg/i18n/polish.go b/pkg/i18n/polish.go index bd9d4bc10..8afe27036 100644 --- a/pkg/i18n/polish.go +++ b/pkg/i18n/polish.go @@ -329,9 +329,6 @@ func addPolish(i18nObject *i18n.Bundle) error { }, &i18n.Message{ ID: "newFocusedViewIs", Other: "nowy skupiony widok to {{.newFocusedView}}", - }, &i18n.Message{ - ID: "CantCloseConfirmationPrompt", - Other: "Nie można zamknąć monitu potwierdzenia: {{.error}}", }, &i18n.Message{ ID: "MergeAborted", Other: "Scalanie anulowane", -- cgit v1.2.3