From 164ac72c5a66b251f6b4d1366b246476f4148572 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Tue, 5 Jun 2018 18:47:57 +1000 Subject: [PATCH] generalise commit panel into confirmation panel --- commit_panel.go | 59 --------------------------------- confirmation_panel.go | 76 ++++++++++++++++++++++--------------------- 2 files changed, 39 insertions(+), 96 deletions(-) delete mode 100644 commit_panel.go diff --git a/commit_panel.go b/commit_panel.go deleted file mode 100644 index a3d5d9b68..000000000 --- a/commit_panel.go +++ /dev/null @@ -1,59 +0,0 @@ -package main - -import ( - "fmt" - - "github.com/jroimartin/gocui" -) - -func handleCommitPress(g *gocui.Gui, currentView *gocui.View) error { - if len(stagedFiles(state.GitFiles)) == 0 { - return createSimpleConfirmationPanel(g, currentView, "Nothing to Commit", "There are no staged files to commit (esc)") - } - maxX, maxY := g.Size() - if v, err := g.SetView("commit", maxX/2-30, maxY/2-1, maxX/2+30, maxY/2+1); err != nil { - if err != gocui.ErrUnknownView { - return err - } - v.Title = "Commit Message" - v.Editable = true - if _, err := g.SetCurrentView("commit"); err != nil { - return err - } - switchFocus(g, currentView, v) - } - return nil -} - -func handleCommitSubmit(g *gocui.Gui, v *gocui.View) error { - if len(v.BufferLines()) == 0 { - return closeCommitPrompt(g, v) - } - message := fmt.Sprint(v.BufferLines()[0]) - // for whatever reason, a successful commit returns an error, so we're not - // going to check for an error here - if err := gitCommit(message); err != nil { - panic(err) - } - refreshFiles(g) - refreshCommits(g) - return closeCommitPrompt(g, v) -} - -func closeCommitPrompt(g *gocui.Gui, v *gocui.View) error { - filesView, _ := g.View("files") - // not passing in the view as oldView to switchFocus because we don't want a - // reference pointing to a deleted view - switchFocus(g, nil, filesView) - if err := g.DeleteView("commit"); err != nil { - return err - } - if _, err := g.SetCurrentView(state.PreviousView); err != nil { - return err - } - return nil -} - -func handleCommitPromptFocus(g *gocui.Gui, v *gocui.View) error { - return renderString(g, "options", "esc: close, enter: commit") -} diff --git a/confirmation_panel.go b/confirmation_panel.go index f18c2a2e5..e86972eea 100644 --- a/confirmation_panel.go +++ b/confirmation_panel.go @@ -61,63 +61,65 @@ func getConfirmationPanelDimensions(g *gocui.Gui, prompt string) (int, int, int, height/2 + panelHeight/2 } -func createPromptPanel(g *gocui.Gui, v *gocui.View, title string, handleSubmit func(*gocui.Gui, *gocui.View) error) error { +func createPromptPanel(g *gocui.Gui, currentView *gocui.View, title string, handleYes func(*gocui.Gui, *gocui.View) error) error { // only need to fit one line x0, y0, x1, y1 := getConfirmationPanelDimensions(g, "") if confirmationView, err := g.SetView("confirmation", x0, y0, x1, y1); err != nil { if err != gocui.ErrUnknownView { return err } + confirmationView.Editable = true g.Cursor = true + confirmationView.Title = title - switchFocus(g, v, confirmationView) - if err := g.SetKeybinding("confirmation", gocui.KeyEnter, gocui.ModNone, wrappedConfirmationFunction(handleSubmit)); err != nil { - return err - } - if err := g.SetKeybinding("confirmation", gocui.KeyEsc, gocui.ModNone, wrappedConfirmationFunction(nil)); err != nil { - return err - } + switchFocus(g, currentView, confirmationView) + return setKeyBindings(g, handleYes, nil) } return nil } -func createConfirmationPanel(g *gocui.Gui, v *gocui.View, title, prompt string, handleYes, handleNo func(*gocui.Gui, *gocui.View) error) error { - // delete the existing confirmation panel if it exists - if view, _ := g.View("confirmation"); view != nil { - if err := closeConfirmationPrompt(g); err != nil { - panic(err) +func createConfirmationPanel(g *gocui.Gui, currentView *gocui.View, title, prompt string, handleYes, handleNo func(*gocui.Gui, *gocui.View) error) error { + g.Update(func(g *gocui.Gui) error { + // delete the existing confirmation panel if it exists + if view, _ := g.View("confirmation"); view != nil { + if err := closeConfirmationPrompt(g); err != nil { + panic(err) + } } + x0, y0, x1, y1 := getConfirmationPanelDimensions(g, prompt) + if confirmationView, err := g.SetView("confirmation", x0, y0, x1, y1); err != nil { + if err != gocui.ErrUnknownView { + return err + } + confirmationView.Title = title + renderString(g, "confirmation", prompt) + switchFocus(g, currentView, confirmationView) + return setKeyBindings(g, handleYes, handleNo) + } + return nil + }) + return nil +} + +func setKeyBindings(g *gocui.Gui, handleYes, handleNo func(*gocui.Gui, *gocui.View) error) error { + renderString(g, "options", "esc/n: close, enter/y: confirm") + if err := g.SetKeybinding("confirmation", gocui.KeyEnter, gocui.ModNone, wrappedConfirmationFunction(handleYes)); err != nil { + return err } - x0, y0, x1, y1 := getConfirmationPanelDimensions(g, prompt) - if confirmationView, err := g.SetView("confirmation", x0, y0, x1, y1); err != nil { - if err != gocui.ErrUnknownView { - return err - } - confirmationView.Title = title - renderString(g, "confirmation", prompt) - switchFocus(g, v, confirmationView) - if err := g.SetKeybinding("confirmation", 'n', gocui.ModNone, wrappedConfirmationFunction(handleNo)); err != nil { - return err - } - if err := g.SetKeybinding("confirmation", gocui.KeyEsc, gocui.ModNone, wrappedConfirmationFunction(handleNo)); err != nil { - return err - } - if err := g.SetKeybinding("confirmation", 'y', gocui.ModNone, wrappedConfirmationFunction(handleYes)); err != nil { - return err - } - if err := g.SetKeybinding("confirmation", gocui.KeyEnter, gocui.ModNone, wrappedConfirmationFunction(handleYes)); err != nil { - return err - } + if err := g.SetKeybinding("confirmation", gocui.KeyEsc, gocui.ModNone, wrappedConfirmationFunction(handleNo)); err != nil { + return err } return nil } -func createSimpleConfirmationPanel(g *gocui.Gui, v *gocui.View, title, prompt string) error { - return createConfirmationPanel(g, v, title, prompt, nil, nil) +func createSimpleConfirmationPanel(g *gocui.Gui, currentView *gocui.View, title, prompt string) error { + return createConfirmationPanel(g, currentView, title, prompt, nil, nil) } func createErrorPanel(g *gocui.Gui, message string) error { - v := g.CurrentView() - return createConfirmationPanel(g, v, "Error", message, nil, nil) + currentView := g.CurrentView() + colorFunction := color.New(color.FgRed).SprintFunc() + coloredMessage := colorFunction(strings.TrimSpace(message)) + return createConfirmationPanel(g, currentView, "Error", coloredMessage, nil, nil) }