From 7aa884ed8f19eacb03a5ee28baba989783746706 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Fri, 10 Aug 2018 23:36:54 +1000 Subject: [PATCH] step one on restoring multiline commits --- confirmation_panel.go | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/confirmation_panel.go b/confirmation_panel.go index 7ae55eaf9..c82b6b65b 100644 --- a/confirmation_panel.go +++ b/confirmation_panel.go @@ -56,6 +56,9 @@ func getConfirmationPanelDimensions(g *gocui.Gui, prompt string) (int, int, int, } func createPromptPanel(g *gocui.Gui, currentView *gocui.View, title string, initialValue *[]byte, handleConfirm func(*gocui.Gui, *gocui.View) error) error { + if initialValue == nil { + initialValue = &[]byte{} + } // only need to fit one line x0, y0, x1, y1 := getConfirmationPanelDimensions(g, "") if confirmationView, err := g.SetView("confirmation", x0, y0, x1, y1, 0); err != nil { @@ -79,14 +82,29 @@ func createPromptPanel(g *gocui.Gui, currentView *gocui.View, title string, init confirmationView.Editable = true confirmationView.Title = title - confirmationView.Write(*initialValue) - confirmationView.SetCursor(len(*initialValue), 0) + restorePreviousBuffer(confirmationView, initialValue) switchFocus(g, currentView, confirmationView) return setKeyBindings(g, handleConfirmAndClear, handleClose) } return nil } +func restorePreviousBuffer(confirmationView *gocui.View, initialValue *[]byte) { + confirmationView.Write(*initialValue) + x, y := getCursorPositionFromBuffer(initialValue) + devLog("New cursor position:", x, y) + confirmationView.SetCursor(0, 0) + confirmationView.MoveCursor(x, y, false) +} + +func getCursorPositionFromBuffer(initialValue *[]byte) (int, int) { + split := strings.Split(string(*initialValue), "\n") + lastLine := split[len(split)-1] + x := len(lastLine) + y := len(split) + return x, y +} + func createConfirmationPanel(g *gocui.Gui, currentView *gocui.View, title, prompt string, handleConfirm, handleClose func(*gocui.Gui, *gocui.View) error) error { g.Update(func(g *gocui.Gui) error { // delete the existing confirmation panel if it exists