1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-11-30 09:16:47 +02:00
lazygit/pkg/gui/commit_message_panel.go

188 lines
4.4 KiB
Go
Raw Normal View History

2018-08-14 11:05:26 +02:00
package gui
import (
2018-09-05 12:42:11 +02:00
"strconv"
"strings"
2018-10-20 17:37:55 +02:00
"sync"
2018-09-05 12:42:11 +02:00
"github.com/jesseduffield/gocui"
)
2018-08-14 11:05:26 +02:00
func (gui *Gui) handleCommitConfirm(g *gocui.Gui, v *gocui.View) error {
message := gui.trimmedContent(v)
if message == "" {
return gui.createErrorPanel(g, gui.Tr.SLocalize("CommitWithoutMessageErr"))
2018-08-14 11:05:26 +02:00
}
sub, err := gui.GitCommand.Commit(message, false)
2018-08-14 11:05:26 +02:00
if err != nil {
// TODO need to find a way to send through this error
if err != gui.Errors.ErrSubProcess {
2018-08-14 11:05:26 +02:00
return gui.createErrorPanel(g, err.Error())
}
}
if sub != nil {
gui.SubProcess = sub
return gui.Errors.ErrSubProcess
2018-08-14 11:05:26 +02:00
}
gui.refreshFiles(g)
v.Clear()
v.SetCursor(0, 0)
g.SetViewOnBottom("commitMessage")
gui.switchFocus(g, v, gui.getFilesView(g))
return gui.refreshCommits(g)
}
func (gui *Gui) handleCommitClose(g *gocui.Gui, v *gocui.View) error {
g.SetViewOnBottom("commitMessage")
return gui.switchFocus(g, v, gui.getFilesView(g))
}
func (gui *Gui) handleCommitFocused(g *gocui.Gui, v *gocui.View) error {
if _, err := g.SetViewOnTop("commitMessage"); err != nil {
return err
}
2018-10-20 17:37:55 +02:00
message := gui.Tr.TemplateLocalize(
"CloseConfirm",
Teml{
"keyBindClose": "esc",
"keyBindConfirm": "enter",
},
)
return gui.renderString(g, "options", message)
}
var unamePassMessage = ""
var waitForGroup sync.WaitGroup
var waitForGroupActie = false
// waitForPassUname wait for a username or password input from the pushPassUname popup
func (gui *Gui) waitForPassUname(g *gocui.Gui, currentView *gocui.View, passOrUname string) string {
2018-10-20 18:58:37 +02:00
waitForGroupActie = true
waitForGroup.Add(1)
2018-10-20 19:44:56 +02:00
2018-10-20 17:37:55 +02:00
pushPassUnameView := gui.getPushPassUnameView(g)
if passOrUname == "username" {
pushPassUnameView.Title = gui.Tr.SLocalize("PushUsername")
2018-10-20 19:44:56 +02:00
pushPassUnameView.Mask = 0
2018-10-20 17:37:55 +02:00
} else {
pushPassUnameView.Title = gui.Tr.SLocalize("PushPassword")
pushPassUnameView.Mask = '*'
}
g.Update(func(g *gocui.Gui) error {
2018-10-20 18:58:37 +02:00
_, err := g.SetViewOnTop("pushPassUname")
if err != nil {
return err
}
err = gui.switchFocus(g, currentView, pushPassUnameView)
if err != nil {
return err
}
2018-10-20 17:37:55 +02:00
gui.RenderCommitLength()
return nil
})
2018-10-20 19:44:56 +02:00
// wait for username/passwords input
2018-10-20 17:37:55 +02:00
waitForGroup.Wait()
return unamePassMessage
}
func (gui *Gui) handlePushConfirm(g *gocui.Gui, v *gocui.View) error {
message := gui.trimmedContent(v)
2018-10-20 19:44:56 +02:00
if message == "" {
// make sure to input something
// if not dune the push progress will run forever
message = "-"
}
2018-10-20 17:37:55 +02:00
unamePassMessage = message
if waitForGroupActie {
2018-10-20 19:44:56 +02:00
waitForGroup.Done()
waitForGroupActie = false
2018-10-20 17:37:55 +02:00
}
2018-10-20 18:58:37 +02:00
err := gui.refreshFiles(g)
if err != nil {
return err
}
2018-10-20 17:37:55 +02:00
v.Clear()
2018-10-20 18:58:37 +02:00
err = v.SetCursor(0, 0)
if err != nil {
return err
}
_, err = g.SetViewOnBottom("pushPassUname")
if err != nil {
return err
}
err = gui.switchFocus(g, v, gui.getFilesView(g))
if err != nil {
return err
}
2018-10-20 17:37:55 +02:00
return gui.refreshCommits(g)
}
func (gui *Gui) handlePushClose(g *gocui.Gui, v *gocui.View) error {
2018-10-20 18:58:37 +02:00
_, err := g.SetViewOnBottom("pushPassUname")
if err != nil {
return err
}
2018-10-20 17:37:55 +02:00
unamePassMessage = ""
if waitForGroupActie {
2018-10-20 19:44:56 +02:00
waitForGroup.Done()
waitForGroupActie = false
2018-10-20 17:37:55 +02:00
}
return gui.switchFocus(g, v, gui.getFilesView(g))
}
func (gui *Gui) handlePushFocused(g *gocui.Gui, v *gocui.View) error {
if _, err := g.SetViewOnTop("pushPassUname"); err != nil {
return err
}
message := gui.Tr.TemplateLocalize(
"CloseConfirm",
Teml{
"keyBindClose": "esc",
"keyBindConfirm": "enter",
},
)
return gui.renderString(g, "options", message)
2018-08-14 11:05:26 +02:00
}
2018-09-05 12:42:11 +02:00
func (gui *Gui) simpleEditor(v *gocui.View, key gocui.Key, ch rune, mod gocui.Modifier) {
switch {
case key == gocui.KeyBackspace || key == gocui.KeyBackspace2:
v.EditDelete(true)
case key == gocui.KeyDelete:
v.EditDelete(false)
case key == gocui.KeyArrowDown:
v.MoveCursor(0, 1, false)
case key == gocui.KeyArrowUp:
v.MoveCursor(0, -1, false)
case key == gocui.KeyArrowLeft:
v.MoveCursor(-1, 0, false)
case key == gocui.KeyArrowRight:
v.MoveCursor(1, 0, false)
case key == gocui.KeyTab:
v.EditNewLine()
case key == gocui.KeySpace:
v.EditWrite(' ')
case key == gocui.KeyInsert:
v.Overwrite = !v.Overwrite
default:
v.EditWrite(ch)
}
gui.RenderCommitLength()
2018-09-05 12:42:11 +02:00
}
func (gui *Gui) getBufferLength(view *gocui.View) string {
return " " + strconv.Itoa(strings.Count(view.Buffer(), "")-1) + " "
}
func (gui *Gui) RenderCommitLength() {
if !gui.Config.GetUserConfig().GetBool("gui.commitLength.show") {
return
}
v := gui.getCommitMessageView(gui.g)
v.Subtitle = gui.getBufferLength(v)
}