1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-14 11:23:09 +02:00
lazygit/pkg/gui/controllers/commit_message_controller.go

92 lines
2.4 KiB
Go
Raw Normal View History

2022-02-22 12:16:00 +02:00
package controllers
import (
2023-03-21 11:57:52 +02:00
"github.com/jesseduffield/lazygit/pkg/gui/context"
2022-02-22 12:16:00 +02:00
"github.com/jesseduffield/lazygit/pkg/gui/types"
)
type CommitMessageController struct {
baseController
*controllerCommon
getCommitMessage func() string
onCommitAttempt func(message string)
onCommitSuccess func()
}
var _ types.IController = &CommitMessageController{}
func NewCommitMessageController(
common *controllerCommon,
getCommitMessage func() string,
onCommitAttempt func(message string),
onCommitSuccess func(),
) *CommitMessageController {
return &CommitMessageController{
baseController: baseController{},
controllerCommon: common,
getCommitMessage: getCommitMessage,
onCommitAttempt: onCommitAttempt,
onCommitSuccess: onCommitSuccess,
}
}
2023-03-21 11:57:52 +02:00
// TODO: merge that commit panel PR because we're not currently showing how to add a newline as it's
// handled by the editor func rather than by the controller here.
2022-02-22 12:16:00 +02:00
func (self *CommitMessageController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding {
bindings := []*types.Binding{
{
2023-03-21 11:57:52 +02:00
Key: opts.GetKey(opts.Config.Universal.SubmitEditorText),
Handler: self.confirm,
Description: self.c.Tr.LcConfirm,
2022-02-22 12:16:00 +02:00
},
{
2023-03-21 11:57:52 +02:00
Key: opts.GetKey(opts.Config.Universal.Return),
Handler: self.close,
Description: self.c.Tr.LcClose,
2022-02-22 12:16:00 +02:00
},
}
return bindings
}
2023-03-21 11:57:52 +02:00
func (self *CommitMessageController) GetOnFocusLost() func(types.OnFocusLostOpts) error {
return func(types.OnFocusLostOpts) error {
self.context().RenderCommitLength()
return nil
}
}
2022-02-22 12:16:00 +02:00
func (self *CommitMessageController) Context() types.Context {
return self.context()
}
// this method is pointless in this context but I'm keeping it consistent
// with other contexts so that when generics arrive it's easier to refactor
2023-03-21 11:57:52 +02:00
func (self *CommitMessageController) context() *context.CommitMessageContext {
2022-02-22 12:16:00 +02:00
return self.contexts.CommitMessage
}
2022-02-22 12:21:35 +02:00
func (self *CommitMessageController) confirm() error {
2022-02-22 12:16:00 +02:00
message := self.getCommitMessage()
self.onCommitAttempt(message)
if message == "" {
return self.c.ErrorMsg(self.c.Tr.CommitWithoutMessageErr)
}
cmdObj := self.git.Commit.CommitCmdObj(message)
self.c.LogAction(self.c.Tr.Actions.Commit)
_ = self.c.PopContext()
return self.helpers.GPG.WithGpgHandling(cmdObj, self.c.Tr.CommittingStatus, func() error {
self.onCommitSuccess()
return nil
})
}
2022-02-22 12:21:35 +02:00
func (self *CommitMessageController) close() error {
2022-02-22 12:16:00 +02:00
return self.c.PopContext()
}