1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-11-28 09:08:41 +02:00

generalise commit panel into confirmation panel

This commit is contained in:
Jesse Duffield 2018-06-05 18:47:57 +10:00
parent d89cab146a
commit 164ac72c5a
2 changed files with 39 additions and 96 deletions

View File

@ -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")
}

View File

@ -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)
}