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

176 lines
4.2 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"
"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)
}
2018-11-23 14:58:30 +02:00
type credentials chan string
2018-10-20 17:37:55 +02:00
// 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-11-23 14:58:30 +02:00
gui.credentials = make(chan string)
2018-11-10 10:09:18 +02:00
pushPassUnameView, _ := g.View("pushPassUname")
2018-10-20 17:37:55 +02:00
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-11-23 14:58:30 +02:00
userInput := <-gui.credentials
return userInput
2018-10-20 17:37:55 +02:00
}
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-11-23 14:58:30 +02:00
gui.credentials <- message
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-12-07 15:56:29 +02:00
gui.credentials <- "-"
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) + " "
}
2018-11-30 02:47:14 +02:00
// RenderCommitLength is a function.
func (gui *Gui) RenderCommitLength() {
if !gui.Config.GetUserConfig().GetBool("gui.commitLength.show") {
return
}
v := gui.getCommitMessageView(gui.g)
v.Subtitle = gui.getBufferLength(v)
}