diff options
author | Sean <seand52@gmail.com> | 2023-01-21 11:38:14 +0000 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2023-04-30 13:19:53 +1000 |
commit | 9d68b287db57b49eddf4b2b81ca1acd2e16aa5aa (patch) | |
tree | d286af9a7cfa27348fb5034ee01a1e16a699057b /pkg/gui | |
parent | a5c72d056ddbd639db3597ffb19a60e0cbae1826 (diff) |
Split commit message panel into commit summary and commit description panel
When we use the one panel for the entire commit message, its tricky to have a keybinding both for adding a newline and submitting.
By having two panels: one for the summary line and one for the description, we allow for 'enter' to submit the message when done from the summary panel,
and 'enter' to add a newline when done from the description panel. Alt-enter, for those who can use that key combo, also works for submitting the message
from the description panel. For those who can't use that key combo, and don't want to remap the keybinding, they can hit tab to go back to the summary panel
and then 'enter' to submit the message.
We have some awkwardness in that both contexts (i.e. panels) need to appear and disappear in tandem and we don't have a great way of handling that concept,
so we just push both contexts one after the other, and likewise remove both contexts when we escape.
Diffstat (limited to 'pkg/gui')
-rw-r--r-- | pkg/gui/context.go | 31 | ||||
-rw-r--r-- | pkg/gui/context/commit_message_context.go | 72 | ||||
-rw-r--r-- | pkg/gui/context/context.go | 17 | ||||
-rw-r--r-- | pkg/gui/context/setup.go | 10 | ||||
-rw-r--r-- | pkg/gui/controllers.go | 55 | ||||
-rw-r--r-- | pkg/gui/controllers/commit_description_controller.go | 60 | ||||
-rw-r--r-- | pkg/gui/controllers/commit_message_controller.go | 83 | ||||
-rw-r--r-- | pkg/gui/controllers/files_controller.go | 9 | ||||
-rw-r--r-- | pkg/gui/controllers/helpers/commits_helper.go | 153 | ||||
-rw-r--r-- | pkg/gui/controllers/helpers/confirmation_helper.go | 76 | ||||
-rw-r--r-- | pkg/gui/controllers/helpers/helpers.go | 2 | ||||
-rw-r--r-- | pkg/gui/controllers/helpers/working_tree_helper.go | 84 | ||||
-rw-r--r-- | pkg/gui/controllers/local_commits_controller.go | 33 | ||||
-rw-r--r-- | pkg/gui/editors.go | 28 | ||||
-rw-r--r-- | pkg/gui/gui.go | 4 | ||||
-rw-r--r-- | pkg/gui/gui_common.go | 4 | ||||
-rw-r--r-- | pkg/gui/types/common.go | 4 | ||||
-rw-r--r-- | pkg/gui/types/views.go | 29 | ||||
-rw-r--r-- | pkg/gui/views.go | 12 |
19 files changed, 590 insertions, 176 deletions
diff --git a/pkg/gui/context.go b/pkg/gui/context.go index 28dc5e2b2..252815e18 100644 --- a/pkg/gui/context.go +++ b/pkg/gui/context.go @@ -7,6 +7,7 @@ import ( "github.com/jesseduffield/generics/slices" "github.com/jesseduffield/lazygit/pkg/gui/context" "github.com/jesseduffield/lazygit/pkg/gui/types" + "github.com/samber/lo" ) // This file is for the management of contexts. There is a context stack such that @@ -164,6 +165,36 @@ func (self *ContextMgr) Pop() error { return self.activateContext(newContext, types.OnFocusOpts{}) } +func (self *ContextMgr) RemoveContexts(contextsToRemove []types.Context) error { + self.Lock() + + if len(self.ContextStack) == 1 { + self.Unlock() + return nil + } + + rest := lo.Filter(self.ContextStack, func(context types.Context, _ int) bool { + for _, contextToRemove := range contextsToRemove { + if context.GetKey() == contextToRemove.GetKey() { + return false + } + } + return true + }) + self.ContextStack = rest + contextToActivate := rest[len(rest)-1] + self.Unlock() + + for _, context := range contextsToRemove { + if err := self.deactivateContext(context, types.OnFocusLostOpts{NewContextKey: contextToActivate.GetKey()}); err != nil { + return err + } + } + + // activate the item at the top of the stack + return self.activateContext(contextToActivate, types.OnFocusOpts{}) +} + func (self *ContextMgr) deactivateContext(c types.Context, opts types.OnFocusLostOpts) error { view, _ := self.gui.c.GocuiGui().View(c.GetViewName()) diff --git a/pkg/gui/context/commit_message_context.go b/pkg/gui/context/commit_message_context.go index 96c6c4f73..4241f859f 100644 --- a/pkg/gui/context/commit_message_context.go +++ b/pkg/gui/context/commit_message_context.go @@ -9,18 +9,45 @@ import ( ) type CommitMessageContext struct { - *SimpleContext c *ContextCommon + types.Context + viewModel *CommitMessageViewModel } var _ types.Context = (*CommitMessageContext)(nil) +// 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 +} + func NewCommitMessageContext( c *ContextCommon, ) *CommitMessageContext { + viewModel := &CommitMessageViewModel{} return &CommitMessageContext{ - c: c, - SimpleContext: NewSimpleContext( + c: c, + viewModel: viewModel, + Context: NewSimpleContext( NewBaseContext(NewBaseContextOpts{ Kind: types.PERSISTENT_POPUP, View: c.Views().CommitMessage, @@ -33,6 +60,45 @@ func NewCommitMessageContext( } } +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 +} + func (self *CommitMessageContext) RenderCommitLength() { if !self.c.UserConfig.Gui.CommitLength.Show { return diff --git a/pkg/gui/context/context.go b/pkg/gui/context/context.go index 7f1439466..ab188d761 100644 --- a/pkg/gui/context/context.go +++ b/pkg/gui/context/context.go @@ -33,13 +33,14 @@ const ( INFORMATION_CONTEXT_KEY types.ContextKey = "information" LIMIT_CONTEXT_KEY types.ContextKey = "limit" - MENU_CONTEXT_KEY types.ContextKey = "menu" - CONFIRMATION_CONTEXT_KEY types.ContextKey = "confirmation" - SEARCH_CONTEXT_KEY types.ContextKey = "search" - COMMIT_MESSAGE_CONTEXT_KEY types.ContextKey = "commitMessage" - SUBMODULES_CONTEXT_KEY types.ContextKey = "submodules" - SUGGESTIONS_CONTEXT_KEY types.ContextKey = "suggestions" - COMMAND_LOG_CONTEXT_KEY types.ContextKey = "cmdLog" + MENU_CONTEXT_KEY types.ContextKey = "menu" + CONFIRMATION_CONTEXT_KEY types.ContextKey = "confirmation" + SEARCH_CONTEXT_KEY types.ContextKey = "search" + COMMIT_MESSAGE_CONTEXT_KEY types.ContextKey = "commitMessage" + COMMIT_DESCRIPTION_CONTEXT_KEY types.ContextKey = "commitDescription" + SUBMODULES_CONTEXT_KEY types.ContextKey = "submodules" + SUGGESTIONS_CONTEXT_KEY types.ContextKey = "suggestions" + COMMAND_LOG_CONTEXT_KEY types.ContextKey = "cmdLog" ) var AllContextKeys = []types.ContextKey{ @@ -98,6 +99,7 @@ type ContextTree struct { MergeConflicts *MergeConflictsContext Confirmation *ConfirmationContext CommitMessage *CommitMessageContext + CommitDescription types.Context CommandLog types.Context // display contexts @@ -129,6 +131,7 @@ func (self *ContextTree) Flatten() []types.Context { self.Menu, self.Confirmation, self.CommitMessage, + self.CommitDescription, self.MergeConflicts, self.StagingSecondary, diff --git a/pkg/gui/context/setup.go b/pkg/gui/context/setup.go index f6a07ddd2..775803884 100644 --- a/pkg/gui/context/setup.go +++ b/pkg/gui/context/setup.go @@ -100,6 +100,16 @@ func NewContextTree(c *ContextCommon) *ContextTree { ), Confirmation: NewConfirmationContext(c), CommitMessage: NewCommitMessageContext(c), + CommitDescription: NewSimpleContext( + NewBaseContext(NewBaseContextOpts{ + Kind: types.PERSISTENT_POPUP, + View: c.Views().CommitDescription, + WindowName: "commitDescription", + Key: COMMIT_DESCRIPTION_CONTEXT_KEY, + Focusable: true, + HasUncontrolledBounds: true, + }), + ), Search: NewSimpleContext( NewBaseContext(NewBaseContextOpts{ Kind: types.PERSISTENT_POPUP, diff --git a/pkg/gui/controllers.go b/pkg/gui/controllers.go index 344de939e..78943e798 100644 --- a/pkg/gui/controllers.go +++ b/pkg/gui/controllers.go @@ -22,10 +22,23 @@ func (gui *Gui) resetHelpersAndControllers() { rebaseHelper := helpers.NewMergeAndRebaseHelper(helperCommon, refsHelper) suggestionsHelper := helpers.NewSuggestionsHelper(helperCommon) - setCommitMessage := gui.getSetTextareaTextFn(func() *gocui.View { return gui.Views.CommitMessage }) - getSavedCommitMessage := func() string { - return gui.State.savedCommitMessage + + setCommitSummary := gui.getCommitMessageSetTextareaTextFn(func() *gocui.View { return gui.Views.CommitMessage }) + setCommitDescription := gui.getCommitMessageSetTextareaTextFn(func() *gocui.View { return gui.Views.CommitDescription }) + getCommitSummary := func() string { + return strings.TrimSpace(gui.Views.CommitMessage.TextArea.GetContent()) } + + getCommitDescription := func() string { + return strings.TrimSpace(gui.Views.CommitDescription.TextArea.GetContent()) + } + commitsHelper := helpers.NewCommitsHelper(helperCommon, + getCommitSummary, + setCommitSummary, + getCommitDescription, + setCommitDescription, + ) + gpgHelper := helpers.NewGpgHelper(helperCommon) viewHelper := helpers.NewViewHelper(helperCommon, gui.State.Contexts) recordDirectoryHelper := helpers.NewRecordDirectoryHelper(helperCommon) @@ -60,7 +73,7 @@ func (gui *Gui) resetHelpersAndControllers() { Bisect: bisectHelper, Suggestions: suggestionsHelper, Files: helpers.NewFilesHelper(helperCommon), - WorkingTree: helpers.NewWorkingTreeHelper(helperCommon, refsHelper, setCommitMessage, getSavedCommitMessage), + WorkingTree: helpers.NewWorkingTreeHelper(helperCommon, refsHelper, commitsHelper, gpgHelper), Tags: helpers.NewTagsHelper(helperCommon), GPG: helpers.NewGpgHelper(helperCommon), MergeAndRebase: rebaseHelper, @@ -68,6 +81,7 @@ func (gui *Gui) resetHelpersAndControllers() { CherryPick: cherryPickHelper, Upstream: helpers.NewUpstreamHelper(helperCommon, suggestionsHelper.GetRemoteBranchesSuggestionsFunc), AmendHelper: helpers.NewAmendHelper(helperCommon, gpgHelper), + Commits: commitsHelper, Snake: helpers.NewSnakeHelper(helperCommon), Diff: diffHelper, Repos: helpers.NewRecentReposHelper(helperCommon, recordDirectoryHelper, gui.onNewRepo), @@ -102,27 +116,12 @@ func (gui *Gui) resetHelpersAndControllers() { bisectController := controllers.NewBisectController(common) - getCommitMessage := func() string { - return strings.TrimSpace(gui.Views.CommitMessage.TextArea.GetContent()) - } - - onCommitAttempt := func(message string) { - gui.State.savedCommitMessage = message - gui.Views.CommitMessage.ClearTextArea() - } - - onCommitSuccess := func() { - gui.State.savedCommitMessage = "" - _ = gui.c.Refresh(types.RefreshOptions{ - Scope: []types.RefreshableView{types.STAGING}, - }) - } - commitMessageController := controllers.NewCommitMessageController( common, - getCommitMessage, - onCommitAttempt, - onCommitSuccess, + ) + + commitDescriptionController := controllers.NewCommitDescriptionController( + common, ) remoteBranchesController := controllers.NewRemoteBranchesController(common) @@ -132,8 +131,6 @@ func (gui *Gui) resetHelpersAndControllers() { tagsController := controllers.NewTagsController(common) filesController := controllers.NewFilesController( common, - setCommitMessage, - getSavedCommitMessage, ) mergeConflictsController := controllers.NewMergeConflictsController(common) remotesController := controllers.NewRemotesController( @@ -302,6 +299,10 @@ func (gui *Gui) resetHelpersAndControllers() { commitMessageController, ) + controllers.AttachControllers(gui.State.Contexts.CommitDescription, + commitDescriptionController, + ) + controllers.AttachControllers(gui.State.Contexts.RemoteBranches, remoteBranchesController, ) @@ -341,13 +342,13 @@ func (gui *Gui) resetHelpersAndControllers() { } } -func (gui *Gui) getSetTextareaTextFn(getView func() *gocui.View) func(string) { +func (gui *Gui) getCommitMessageSetTextareaTextFn(getView func() *gocui.View) func(string) { return func(text string) { // using a getView function so that we don't need to worry about when the view is created view := getView() view.ClearTextArea() view.TextArea.TypeString(text) - _ = gui.helpers.Confirmation.ResizePopupPanel(view, view.TextArea.GetContent()) + gui.helpers.Confirmation.ResizeCommitMessagePanels() view.RenderTextArea() } } diff --git a/pkg/gui/controllers/commit_description_controller.go b/pkg/gui/controllers/commit_description_controller.go new file mode 100644 index 000000000..78d275184 --- /dev/null +++ b/pkg/gui/controllers/commit_description_controller.go @@ -0,0 +1,60 @@ +package controllers + +import ( + "github.com/jesseduffield/lazygit/pkg/gui/types" +) + +type CommitDescriptionController struct { + baseController + c *ControllerCommon +} + +var _ types.IController = &CommitMessageController{} + +func NewCommitDescriptionController( + common *ControllerCommon, +) *CommitDescriptionController { + return &CommitDescriptionController{ + baseController: baseController{}, + c: common, + } +} + +func (self *CommitDescriptionController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding { + bindings := []*types.Binding{ + { + Key: opts.GetKey(opts.Config.Universal.TogglePanel), + Handler: self.switchToCommitMessage, + }, + { + Key: opts.GetKey(opts.Config.Universal.Return), + Handler: self.close, + }, + { + Key: opts.GetKey(opts.Config.Universal.ConfirmInEditor), + Handler: self.confirm, + }, + } + + return bindings +} + +func (self *CommitDescriptionController) Context() types.Context { + return self.context() +} + +func (self *CommitDescriptionController) context() types.Context { + return self.c.Contexts().CommitMessage +} + +func (self *CommitDescriptionController) switchToCommitMessage() error { + return self.c.PushContext(self.c.Contexts().CommitMessage) +} + +func (self *CommitDescriptionController) close() error { + return self.c.Helpers().Commits.CloseCommitMessagePanel() +} + +func (self *CommitDescriptionController) confirm() error { + return self.c.Helpers().Commits.HandleCommitConfirm() +} diff --git a/pkg/gui/controllers/commit_message_controller.go b/pkg/gui/controllers/commit_message_controller.go index 481d65c4c..b0318e8a4 100644 --- a/pkg/gui/controllers/commit_message_controller.go +++ b/pkg/gui/controllers/commit_message_controller.go @@ -1,6 +1,7 @@ package controllers import ( + "github.com/jesseduffield/lazygit/pkg/commands/git_commands" "github.com/jesseduffield/lazygit/pkg/gui/context" "github.com/jesseduffield/lazygit/pkg/gui/types" ) @@ -8,27 +9,16 @@ import ( type CommitMessageController struct { baseController c *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{}, c: common, - - getCommitMessage: getCommitMessage, - onCommitAttempt: onCommitAttempt, - onCommitSuccess: onCommitSuccess, } } @@ -46,6 +36,18 @@ func (self *CommitMessageController) GetKeybindings(opts types.KeybindingsOpts) Handler: self.close, Description: self.c.Tr.LcClose, }, + { + Key: opts.GetKey(opts.Config.Universal.PrevItem), + Handler: self.handlePreviousCommit, + }, + { + Key: opts.GetKey(opts.Config.Universal.NextItem), + Handler: self.handleNextCommit, + }, + { + Key: opts.GetKey(opts.Config.Universal.TogglePanel), + Handler: self.switchToCommitDescription, + }, } return bindings @@ -62,30 +64,61 @@ 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 func (self *CommitMessageController) context() *context.CommitMessageContext { return self.c.Contexts().CommitMessage } -func (self *CommitMessageController) confirm() error { - message := self.getCommitMessage() - self.onCommitAttempt(message) +func (self *CommitMessageController) handlePreviousCommit() error { + return self.handleCommitIndexChange(1) +} - if message == "" { - return self.c.ErrorMsg(self.c.Tr.CommitWithoutMessageErr) +func (self *CommitMessageController) handleNextCommit() error { + if self.context().GetSelectedIndex() == context.NoCommitIndex { + return nil } + return self.handleCommitIndexChange(-1) +} - cmdObj := self.c.Git().Commit.CommitCmdObj(message) - self.c.LogAction(self.c.Tr.Actions.Commit) +func (self *CommitMessageController) switchToCommitDescription() error { + if err := self.c.PushContext(self.c.Contexts().CommitDescription); err != nil { + return err + } + return nil +} - _ = self.c.PopContext() - return self.c.Helpers().GPG.WithGpgHandling(cmdObj, self.c.Tr.CommittingStatus, func() error { - self.onCommitSuccess() +func (self *CommitMessageController) handleCommitIndexChange(value int) error { + currentIndex := self.context().GetSelectedIndex() + newIndex := currentIndex + value + if newIndex == context.NoCommitIndex { + self.context().SetSelectedIndex(newIndex) + self.c.Helpers().Commits.SetMessageAndDescriptionInView("") return nil - }) + } + + validCommit, err := self.setCommitMessageAtIndex(newIndex) + if validCommit { + self.context().SetSelectedIndex(newIndex) + } + return err +} + +// returns true if the given index is for a valid commit +func (self *CommitMessageController) setCommitMessageAtIndex(index int) (bool, error) { + commitMessage, err := self.c.Git().Commit.GetCommitMessageFromHistory(index) + if err != nil { + if err == git_commands.ErrInvalidCommitIndex { + return false, nil + } + return false, self.c.ErrorMsg(self.c.Tr.CommitWithoutMessageErr) + } + self.c.Helpers().Commits.UpdateCommitPanelView(commitMessage) + return true, nil +} + +func (self *CommitMessageController) confirm() error { + return self.c.Helpers().Commits.HandleCommitConfirm() } func (self *CommitMessageController) close() error { - return self.c.PopContext() + return self.c.Helpers().Commits.CloseCommitMessagePanel() } diff --git a/pkg/gui/controllers/files_controller.go b/pkg/gui/controllers/files_controller.go index 165878459..65fd16891 100644 --- a/pkg/gui/controllers/files_controller.go +++ b/pkg/gui/controllers/files_controller.go @@ -14,22 +14,15 @@ import ( type FilesController struct { baseController // nolint: unused c *ControllerCommon - - setCommitMessage func(message string) - getSavedCommitMessage func() string } var _ types.IController = &FilesController{} func NewFilesController( common *ControllerCommon, - setCommitMessage func(message string), - getSavedCommitMessage func() string, ) *FilesController { return &FilesController{ - c: common, - setCommitMessage: setCommitMessage, - getSavedCommitMessage: getSavedCommitMessage, + c: common, } } diff --git a/pkg/gui/controllers/helpers/commits_helper.go b/pkg/gui/controllers/helpers/commits_helper.go new file mode 100644 index 000000000..ce1b3a9e2 --- /dev/null +++ b/pkg/gui/controllers/helpers/commits_helper.go @@ -0,0 +1,153 @@ +package helpers + +import ( + "strings" + + "github.com/jesseduffield/lazygit/pkg/gui/types" +) + +type ICommitsHelper interface { + UpdateCommitPanelView(message string) +} + +type CommitsHelper struct { + c *HelperCommon + + getCommitSummary func() string + setCommitSummary func(string) + getCommitDescription func() string + setCommitDescription func(string) +} + +var _ ICommitsHelper = &CommitsHelper{} + +func NewCommitsHelper( + c *HelperCommon, + getCommitSummary func() string, + setCommitSummary func(string), + getCommitDescription func() string, + setCommitDescription func(string), +) *CommitsHelper { + return &CommitsHelper{ + c: c, + getCommitSummary: getCommitSummary, + setCommitSummary: setCommitSummary, + getCommitDescription: getCommitDescription, + setCommitDescription: setCommitDescription, + } +} + +func (self *CommitsHelper) SplitCommitMessageAndDescription(message string) (string, string) { + for _, separator := range []string{"\n\n", "\n\r\n\r", "\n", "\n\r"} { + msg, description, found := strings.Cut(message, separator) + if found { + return msg, description + } + } + return message, "" +} + +func (self *CommitsHelper) SetMessageAndDescriptionInView(message string) { + summary, description := self.SplitCommitMessageAndDescription(message) + + self.setCommitSummary(summary) + self.setCommitDescription(description) + self.c.Contexts().CommitMessage.RenderCommitLength() +} + +func (self *CommitsHelper) joinCommitMessageAndDescription() string { + if len(self.getCommitDescription()) == 0 { + return self.getCommitSummary() + } + return self.getCommitSummary() + "\n" + self.getCommitDescription() +} + +func (self *CommitsHelper) UpdateCommitPanelView(message string) { + // first try the passed in message, if not fallback to context -> view in that order + if message != "" { + self.SetMessageAndDescriptionInView(message) + return + } + message = self.c.Contexts().CommitMessage.GetPreservedMessage() + if message != "" { + self.SetMessageAndDescriptionInView(message) + } else { + self.SetMessageAndDescriptionInView(self.getCommitSummary()) + } +} + +type OpenCommitMessagePanelOpts struct { + CommitIndex int + Title string + PreserveMessage bool + OnConfirm func(string) error + InitialMessage string +} + +func (self *CommitsHelper) OpenCommitMessagePanel(opts *OpenCommitMessagePanelOpts) error { + self.c.Contexts().CommitMessage.SetPanelState( + opts.CommitIndex, + opts.Title, + opts.PreserveMessage, + opts.OnConfirm, + ) + + self.UpdateCommitPanelView(opts.InitialMessage) + + return self.pushCommitMessageContexts() +} + +func (self *CommitsHelper) OnCommitSuccess() { + // if we have a preserved message we want to clear it on success + if self.c.Contexts().CommitMessage.GetPreserveMessage() { + self.c.Contexts().CommitMessage.SetPreservedMessage("") + } + self.SetMessageAndDescriptionInView("") +} + +func (self *CommitsHelper) HandleCommitConfirm() error { + fullMessage := self.joinCommitMessageAndDescription() + + if fullMessage == "" { + return self.c.ErrorMsg(self.c.Tr.CommitWithoutMessageErr) + } + + err := self.c.Contexts().CommitMessage.OnConfirm(fullMessage) + if err != nil { + return err + } + + return nil +} + +func (self *CommitsHelper) CloseCommitMessagePanel() error { + if self.c.Contexts().CommitMessage.GetPreserveMessage() { + message := self.joinCommitMessageAndDescription() + + self.c.Contexts().CommitMessage.SetPreservedMessage(message) + } else { + self.SetMessageAndDescriptionInView("") + } + return self.EscapeCommitsPanel() +} + +func (self *CommitsHelper) EscapeCommitsPanel() error { + return self.c.RemoveContexts(self.commitMessageContexts()) +} + +func (self *CommitsHelper) pushCommitMessageContexts() error { + for _, context := range self.commitMessageContexts() { + if err := self.c.PushContext(context); err != nil { + return err + } + } + + return nil +} + +func (self *CommitsHelper) commitMessageContexts() []types.Context { + return []types.Context{ + self.c.Contexts().CommitDescription, + self.c.Contexts().CommitMessage, + } +} diff --git a/pkg/gui/controllers/helpers/confirmation_helper.go b/pkg/gui/controllers/helpers/confirmation_helper.go index e6e3deeef..7968933fc 100644 --- a/pkg/gui/controllers/helpers/confirmation_helper.go +++ b/pkg/gui/controllers/helpers/confirmation_helper.go @@ -75,17 +75,17 @@ func getMessageHeight(wrap bool, message string, width int) int { return lineCount } -func (self *ConfirmationHelper) getConfirmationPanelDimensions(wrap bool, prompt string) (int, int, int, int) { - panelWidth := self.getConfirmationPanelWidth() +func (self *ConfirmationHelper) getPopupPanelDimensions(wrap bool, prompt string) (int, int, int, int) { + panelWidth := self.getPopupPanelWidth() panelHeight := getMessageHeight(wrap, prompt, panelWidth) - return self.getConfirmationPanelDimensionsAux(panelWidth, panelHeight) + return self.getPopupPanelDimensionsAux(panelWidth, panelHeight) } -func (self *ConfirmationHelper) getConfirmationPanelDimensionsForContentHeight(panelWidth, contentHeight int) (int, int, int, int) { - return self.getConfirmationPanelDimensionsAux(panelWidth, contentHeight) +func (self *ConfirmationHelper) getPopupPanelDimensionsForContentHeight(panelWidth, contentHeight int) (int, int, int, int) { + return self.getPopupPanelDimensionsAux(panelWidth, contentHeight) } -func (self *ConfirmationHelper) getConfirmationPanelDimensionsAux(panelWidth int, panelHeight int) (int, int, int, int) { +func (self *ConfirmationHelper) getPopupPanelDimensionsAux(panelWidth int, panelHeight int) (int, int, int, int) { width, height := self.c.GocuiGui().Size() if panelHeight > height*3/4 { panelHeight = height * 3 / 4 @@ -96,7 +96,7 @@ func (self *ConfirmationHelper) getConfirmationPanelDimensionsAux(panelWidth int height/2 + panelHeight/2 } -func (self *ConfirmationHelper) getConfirmationPanelWidth() int { +func (self *ConfirmationHelper) getPopupPanelWidth() int { width, _ := self.c.GocuiGui().Size() // we want a minimum width up to a point, then we do it based on ratio. panelWidth := 4 * width / 7 @@ -254,7 +254,7 @@ func (self *ConfirmationHelper) ResizeConfirmationPanel() { if self.c.Views().Suggestions.Visible { suggestionsViewHeight = 11 < |