Repository: jesseduffield/lazygit
File: confirmation_panel.go (Go)

Code before:
	}
	g.DeleteKeybindings("confirmation")
	return g.DeleteView("confirmation")
}

func getMessageHeight(message string, width int) int {
	lines := strings.Split(message, "\n")
	lineCount := 0
	for _, line := range lines {
		lineCount += len(line)/width + 1
	}
	return lineCount
}

func getConfirmationPanelDimensions(g *gocui.Gui, prompt string) (int, int, int, int) {
	width, height := g.Size()
	panelWidth := width / 2
	panelHeight := getMessageHeight(prompt, panelWidth)
	return width/2 - panelWidth/2,
		height/2 - panelHeight/2 - panelHeight%2 - 1,
		width/2 + panelWidth/2,
		height/2 + panelHeight/2
}

func createPromptPanel(g *gocui.Gui, currentView *gocui.View, title string, initialValue *[]byte, handleConfirm func(*gocui.Gui, *gocui.View) error) error {
	g.SetViewOnBottom("commitMessage")
	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 {
		if err != gocui.ErrUnknownView {
			return err
		}

		handleConfirm := func(gui *gocui.Gui, view *gocui.View) error {
			*initialValue = nil
			return handleConfirm(g, view)
		}

		handleClose := func(gui *gocui.Gui, view *gocui.View) error {
			// FIXME: trimming a newline that is no doubt caused by the enter keybinding
			// on the editor. We should just define a new editor that doesn't do that
			*initialValue = []byte(strings.TrimSpace(view.Buffer()))
			return nil
		}

		confirmationView.Editable = true
		confirmationView.Title = title
		restorePreviousBuffer(confirmationView, initialValue)

Review comment (suggestion):
I really don't like the fact that I'm doing this but I'm not sure what the best way around it is now.

It's not very clean to have to worry about the commit message being open whenever a new popup occurs. It would be better if it could listen for that event

Code after:
	}
	g.DeleteKeybindings("confirmation")
	return g.DeleteView("confirmation")
}

func getMessageHeight(message string, width int) int {
	lines := strings.Split(message, "\n")
	lineCount := 0
	for _, line := range lines {
		lineCount += len(line)/width + 1
	}
	return lineCount
}

func getConfirmationPanelDimensions(g *gocui.Gui, prompt string) (int, int, int, int) {
	width, height := g.Size()
	panelWidth := width / 2
	panelHeight := getMessageHeight(prompt, panelWidth)
	return width/2 - panelWidth/2,
		height/2 - panelHeight/2 - panelHeight%2 - 1,
		width/2 + panelWidth/2,
		height/2 + panelHeight/2
}

func createPromptPanel(g *gocui.Gui, currentView *gocui.View, title string, handleConfirm func(*gocui.Gui, *gocui.View) error) error {
	g.SetViewOnBottom("commitMessage")
	// 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 {
		if err != gocui.ErrUnknownView {
			return err
		}

		confirmationView.Editable = true
		confirmationView.Title = title
		switchFocus(g, currentView, confirmationView)
		return setKeyBindings(g, handleConfirm, nil)
	}
	return nil
}

func createConfirmationPanel(g *gocui.Gui, currentView *gocui.View, title, prompt string, handleConfirm, handleClose func(*gocui.Gui, *gocui.View) error) error {
	g.SetViewOnBottom("commitMessage")
	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)