2018-08-14 11:05:26 +02:00
|
|
|
package gui
|
|
|
|
|
2018-08-14 14:12:07 +02:00
|
|
|
import (
|
2019-02-24 00:42:24 +02:00
|
|
|
"os/exec"
|
2018-09-05 12:42:11 +02:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2018-08-14 14:12:07 +02:00
|
|
|
"github.com/jesseduffield/gocui"
|
|
|
|
)
|
2018-08-14 11:05:26 +02:00
|
|
|
|
2019-02-24 00:42:24 +02:00
|
|
|
// runSyncOrAsyncCommand takes the output of a command that may have returned
|
|
|
|
// either no error, an error, or a subprocess to execute, and if a subprocess
|
|
|
|
// needs to be set on the gui object, it does so, and then returns the error
|
2019-03-23 03:16:55 +02:00
|
|
|
// the bool returned tells us whether the calling code should continue
|
|
|
|
func (gui *Gui) runSyncOrAsyncCommand(sub *exec.Cmd, err error) (bool, error) {
|
2018-08-14 11:05:26 +02:00
|
|
|
if err != nil {
|
2018-08-14 15:47:14 +02:00
|
|
|
if err != gui.Errors.ErrSubProcess {
|
2019-03-23 03:16:55 +02:00
|
|
|
return false, gui.createErrorPanel(gui.g, err.Error())
|
2018-08-14 11:05:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if sub != nil {
|
|
|
|
gui.SubProcess = sub
|
2019-03-23 03:16:55 +02:00
|
|
|
return false, gui.Errors.ErrSubProcess
|
2018-08-14 11:05:26 +02:00
|
|
|
}
|
2019-03-23 03:16:55 +02:00
|
|
|
return true, nil
|
2019-02-24 00:42:24 +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"))
|
|
|
|
}
|
2019-04-13 05:34:12 +02:00
|
|
|
flags := ""
|
|
|
|
skipHookPrefix := gui.Config.GetUserConfig().GetString("git.skipHookPrefix")
|
|
|
|
if skipHookPrefix != "" && strings.HasPrefix(message, skipHookPrefix) {
|
|
|
|
flags = "--no-verify"
|
|
|
|
}
|
|
|
|
ok, err := gui.runSyncOrAsyncCommand(gui.GitCommand.Commit(message, flags))
|
2019-03-23 03:16:55 +02:00
|
|
|
if err != nil {
|
2019-02-24 00:42:24 +02:00
|
|
|
return err
|
|
|
|
}
|
2019-03-23 03:16:55 +02:00
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
2019-02-24 00:42:24 +02:00
|
|
|
|
2018-08-14 11:05:26 +02:00
|
|
|
v.Clear()
|
2018-12-07 09:52:31 +02:00
|
|
|
_ = v.SetCursor(0, 0)
|
|
|
|
_ = v.SetOrigin(0, 0)
|
|
|
|
_, _ = g.SetViewOnBottom("commitMessage")
|
2018-12-08 07:54:54 +02:00
|
|
|
_ = gui.switchFocus(g, v, gui.getFilesView())
|
2018-12-06 13:18:17 +02:00
|
|
|
return gui.refreshSidePanels(g)
|
2018-08-14 11:05:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (gui *Gui) handleCommitClose(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
g.SetViewOnBottom("commitMessage")
|
2018-12-08 07:54:54 +02:00
|
|
|
return gui.switchFocus(g, v, gui.getFilesView())
|
2018-08-14 11:05:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (gui *Gui) handleCommitFocused(g *gocui.Gui, v *gocui.View) error {
|
2018-09-19 12:36:40 +02:00
|
|
|
if _, err := g.SetViewOnTop("commitMessage"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-08-15 09:15:31 +02:00
|
|
|
message := gui.Tr.TemplateLocalize(
|
|
|
|
"CloseConfirm",
|
2018-08-16 11:31:03 +02:00
|
|
|
Teml{
|
2018-08-15 09:15:31 +02:00
|
|
|
"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) getBufferLength(view *gocui.View) string {
|
|
|
|
return " " + strconv.Itoa(strings.Count(view.Buffer(), "")-1) + " "
|
|
|
|
}
|
2018-09-05 15:02:13 +02:00
|
|
|
|
2018-11-30 02:47:14 +02:00
|
|
|
// RenderCommitLength is a function.
|
2018-09-05 15:02:13 +02:00
|
|
|
func (gui *Gui) RenderCommitLength() {
|
|
|
|
if !gui.Config.GetUserConfig().GetBool("gui.commitLength.show") {
|
|
|
|
return
|
|
|
|
}
|
2018-12-08 07:54:54 +02:00
|
|
|
v := gui.getCommitMessageView()
|
2018-09-05 15:02:13 +02:00
|
|
|
v.Subtitle = gui.getBufferLength(v)
|
|
|
|
}
|