2023-03-21 11:57:52 +02:00
|
|
|
package context
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/jesseduffield/gocui"
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/gui/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
type CommitMessageContext struct {
|
2023-03-23 03:35:07 +02:00
|
|
|
c *ContextCommon
|
2023-01-21 13:38:14 +02:00
|
|
|
types.Context
|
|
|
|
viewModel *CommitMessageViewModel
|
2023-03-21 11:57:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ types.Context = (*CommitMessageContext)(nil)
|
|
|
|
|
2023-01-21 13:38:14 +02:00
|
|
|
// when selectedIndex (see below) is set to this value, it means that we're not
|
|
|
|
// currently viewing a commit message of an existing commit: instead we're making our own
|
|
|
|
// new commit message
|
|
|
|
const NoCommitIndex = -1
|
|
|
|
|
|
|
|
type CommitMessageViewModel struct {
|
|
|
|
// index of the commit message, where -1 is 'no commit', 0 is the HEAD commit, 1
|
|
|
|
// is the prior commit, and so on
|
|
|
|
selectedindex int
|
|
|
|
// if true, then upon escaping from the commit message panel, we will preserve
|
|
|
|
// the message so that it's still shown next time we open the panel
|
|
|
|
preserveMessage bool
|
|
|
|
// the full preserved message (combined summary and description)
|
|
|
|
preservedMessage string
|
|
|
|
// invoked when pressing enter in the commit message panel
|
|
|
|
onConfirm func(string) error
|
|
|
|
|
|
|
|
// The message typed in before cycling through history
|
|
|
|
// We store this separately to 'preservedMessage' because 'preservedMessage'
|
|
|
|
// is specifically for committing staged files and we don't want this affected
|
|
|
|
// by cycling through history in the context of rewording an old commit.
|
|
|
|
historyMessage string
|
|
|
|
}
|
|
|
|
|
2023-03-21 11:57:52 +02:00
|
|
|
func NewCommitMessageContext(
|
2023-03-23 03:35:07 +02:00
|
|
|
c *ContextCommon,
|
2023-03-21 11:57:52 +02:00
|
|
|
) *CommitMessageContext {
|
2023-01-21 13:38:14 +02:00
|
|
|
viewModel := &CommitMessageViewModel{}
|
2023-03-21 11:57:52 +02:00
|
|
|
return &CommitMessageContext{
|
2023-01-21 13:38:14 +02:00
|
|
|
c: c,
|
|
|
|
viewModel: viewModel,
|
|
|
|
Context: NewSimpleContext(
|
2023-03-21 11:57:52 +02:00
|
|
|
NewBaseContext(NewBaseContextOpts{
|
|
|
|
Kind: types.PERSISTENT_POPUP,
|
|
|
|
View: c.Views().CommitMessage,
|
|
|
|
WindowName: "commitMessage",
|
|
|
|
Key: COMMIT_MESSAGE_CONTEXT_KEY,
|
|
|
|
Focusable: true,
|
|
|
|
HasUncontrolledBounds: true,
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-21 13:38:14 +02:00
|
|
|
func (self *CommitMessageContext) SetSelectedIndex(value int) {
|
|
|
|
self.viewModel.selectedindex = value
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *CommitMessageContext) GetSelectedIndex() int {
|
|
|
|
return self.viewModel.selectedindex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *CommitMessageContext) GetPreserveMessage() bool {
|
|
|
|
return self.viewModel.preserveMessage
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *CommitMessageContext) GetPreservedMessage() string {
|
|
|
|
return self.viewModel.preservedMessage
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *CommitMessageContext) SetPreservedMessage(message string) {
|
|
|
|
self.viewModel.preservedMessage = message
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *CommitMessageContext) GetHistoryMessage() string {
|
|
|
|
return self.viewModel.historyMessage
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *CommitMessageContext) SetHistoryMessage(message string) {
|
|
|
|
self.viewModel.historyMessage = message
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *CommitMessageContext) OnConfirm(message string) error {
|
|
|
|
return self.viewModel.onConfirm(message)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *CommitMessageContext) SetPanelState(index int, title string, preserveMessage bool, onConfirm func(string) error) {
|
|
|
|
self.viewModel.selectedindex = index
|
|
|
|
self.viewModel.preserveMessage = preserveMessage
|
|
|
|
self.viewModel.onConfirm = onConfirm
|
|
|
|
self.GetView().Title = title
|
|
|
|
}
|
|
|
|
|
2023-03-21 11:57:52 +02:00
|
|
|
func (self *CommitMessageContext) RenderCommitLength() {
|
|
|
|
if !self.c.UserConfig.Gui.CommitLength.Show {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
self.c.Views().CommitMessage.Subtitle = getBufferLength(self.c.Views().CommitMessage)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getBufferLength(view *gocui.View) string {
|
|
|
|
return " " + strconv.Itoa(strings.Count(view.TextArea.GetContent(), "")-1) + " "
|
|
|
|
}
|