diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2022-01-30 10:40:48 +1100 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2022-03-17 19:13:40 +1100 |
commit | f97de692e32be50a54a93b75d3686da0495275cb (patch) | |
tree | 45a65ffb90d6c3897cd554f9551082e7466467fc /pkg | |
parent | 2692637fbeac353d805399dd97714c235f88f303 (diff) |
move helpers into their own struct
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/gui/branches_panel.go | 8 | ||||
-rw-r--r-- | pkg/gui/commit_files_panel.go | 4 | ||||
-rw-r--r-- | pkg/gui/controllers/bisect_controller.go | 44 | ||||
-rw-r--r-- | pkg/gui/controllers/bisect_helper.go | 40 | ||||
-rw-r--r-- | pkg/gui/diffing.go | 2 | ||||
-rw-r--r-- | pkg/gui/filtering_menu_panel.go | 2 | ||||
-rw-r--r-- | pkg/gui/gui.go | 66 | ||||
-rw-r--r-- | pkg/gui/line_by_line_panel.go | 2 | ||||
-rw-r--r-- | pkg/gui/modes.go | 2 | ||||
-rw-r--r-- | pkg/gui/patch_options_panel.go | 2 | ||||
-rw-r--r-- | pkg/gui/pull_request_menu_panel.go | 2 | ||||
-rw-r--r-- | pkg/gui/reflog_panel.go | 4 | ||||
-rw-r--r-- | pkg/gui/remote_branches_panel.go | 2 | ||||
-rw-r--r-- | pkg/gui/status_panel.go | 4 | ||||
-rw-r--r-- | pkg/gui/sub_commits_panel.go | 4 | ||||
-rw-r--r-- | pkg/gui/submodules_panel.go | 2 |
16 files changed, 111 insertions, 79 deletions
diff --git a/pkg/gui/branches_panel.go b/pkg/gui/branches_panel.go index 2a5d0f308..4941f32ed 100644 --- a/pkg/gui/branches_panel.go +++ b/pkg/gui/branches_panel.go @@ -58,7 +58,7 @@ func (gui *Gui) handleBranchPress() error { } branch := gui.getSelectedBranch() gui.c.LogAction(gui.c.Tr.Actions.CheckoutBranch) - return gui.refsHelper.CheckoutRef(branch.Name, types.CheckoutRefOptions{}) + return gui.helpers.refs.CheckoutRef(branch.Name, types.CheckoutRefOptions{}) } func (gui *Gui) handleCreatePullRequestPress() error { @@ -131,10 +131,10 @@ func (gui *Gui) handleForceCheckout() error { func (gui *Gui) handleCheckoutByName() error { return gui.c.Prompt(types.PromptOpts{ Title: gui.c.Tr.BranchName + ":", - FindSuggestionsFunc: gui.suggestionsHelper.GetRefsSuggestionsFunc(), + FindSuggestionsFunc: gui.helpers.suggestions.GetRefsSuggestionsFunc(), HandleConfirm: func(response string) error { gui.c.LogAction("Checkout branch") - return gui.refsHelper.CheckoutRef(response, types.CheckoutRefOptions{ + return gui.helpers.refs.CheckoutRef(response, types.CheckoutRefOptions{ OnRefNotFound: func(ref string) error { return gui.c.Ask(types.AskOpts{ Title: gui.c.Tr.BranchNotFoundTitle, @@ -328,7 +328,7 @@ func (gui *Gui) handleCreateResetToBranchMenu() error { return nil } - return gui.refsHelper.CreateGitResetMenu(branch.Name) + return gui.helpers.refs.CreateGitResetMenu(branch.Name) } func (gui *Gui) handleRenameBranch() error { diff --git a/pkg/gui/commit_files_panel.go b/pkg/gui/commit_files_panel.go index 55090010c..973273089 100644 --- a/pkg/gui/commit_files_panel.go +++ b/pkg/gui/commit_files_panel.go @@ -126,7 +126,7 @@ func (gui *Gui) handleOpenOldCommitFile() error { return nil } - return gui.filesHelper.OpenFile(node.GetPath()) + return gui.helpers.files.OpenFile(node.GetPath()) } func (gui *Gui) handleEditCommitFile() error { @@ -139,7 +139,7 @@ func (gui *Gui) handleEditCommitFile() error { return gui.c.ErrorMsg(gui.c.Tr.ErrCannotEditDirectory) } - return gui.filesHelper.EditFile(node.GetPath()) + return gui.helpers.files.EditFile(node.GetPath()) } func (gui *Gui) handleToggleFileForPatch() error { diff --git a/pkg/gui/controllers/bisect_controller.go b/pkg/gui/controllers/bisect_controller.go index 8bfcba5a9..6befce84c 100644 --- a/pkg/gui/controllers/bisect_controller.go +++ b/pkg/gui/controllers/bisect_controller.go @@ -12,9 +12,10 @@ import ( ) type BisectController struct { - c *types.ControllerCommon - getContext func() types.IListContext - git *commands.GitCommand + c *types.ControllerCommon + getContext func() types.IListContext + git *commands.GitCommand + bisectHelper *BisectHelper getSelectedLocalCommit func() *models.Commit getCommits func() []*models.Commit @@ -26,14 +27,16 @@ func NewBisectController( c *types.ControllerCommon, getContext func() types.IListContext, git *commands.GitCommand, + bisectHelper *BisectHelper, getSelectedLocalCommit func() *models.Commit, getCommits func() []*models.Commit, ) *BisectController { return &BisectController{ - c: c, - getContext: getContext, - git: git, + c: c, + getContext: getContext, + git: git, + bisectHelper: bisectHelper, getSelectedLocalCommit: getSelectedLocalCommit, getCommits: getCommits, @@ -116,7 +119,7 @@ func (self *BisectController) openMidBisectMenu(info *git_commands.BisectInfo, c { DisplayString: self.c.Tr.Bisect.ResetOption, OnPress: func() error { - return self.Reset() + return self.bisectHelper.Reset() }, }, } @@ -143,7 +146,7 @@ func (self *BisectController) openStartBisectMenu(info *git_commands.BisectInfo, return self.c.Error(err) } - return self.postBisectCommandRefresh() + return self.bisectHelper.PostBisectCommandRefresh() }, }, { @@ -158,28 +161,13 @@ func (self *BisectController) openStartBisectMenu(info *git_commands.BisectInfo, return self.c.Error(err) } - return self.postBisectCommandRefresh() + return self.bisectHelper.PostBisectCommandRefresh() }, }, }, }) } -func (self *BisectController) Reset() error { - return self.c.Ask(types.AskOpts{ - Title: self.c.Tr.Bisect.ResetTitle, - Prompt: self.c.Tr.Bisect.ResetPrompt, - HandleConfirm: func() error { - self.c.LogAction(self.c.Tr.Actions.ResetBisect) - if err := self.git.Bisect.Reset(); err != nil { - return self.c.Error(err) - } - - return self.postBisectCommandRefresh() - }, - }) -} - func (self *BisectController) showBisectCompleteMessage(candidateShas []string) error { prompt := self.c.Tr.Bisect.CompletePrompt if len(candidateShas) > 1 { @@ -200,7 +188,7 @@ func (self *BisectController) showBisectCompleteMessage(candidateShas []string) return self.c.Error(err) } - return self.postBisectCommandRefresh() + return self.bisectHelper.PostBisectCommandRefresh() }, }) } @@ -222,10 +210,6 @@ func (self *BisectController) afterMark(selectCurrent bool, waitToReselect bool) return nil } -func (self *BisectController) postBisectCommandRefresh() error { - return self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC, Scope: []types.RefreshableView{}}) -} - func (self *BisectController) afterBisectMarkRefresh(selectCurrent bool, waitToReselect bool) error { selectFn := func() { if selectCurrent { @@ -238,7 +222,7 @@ func (self *BisectController) afterBisectMarkRefresh(selectCurrent bool, waitToR } else { selectFn() - return self.postBisectCommandRefresh() + return self.bisectHelper.PostBisectCommandRefresh() } } diff --git a/pkg/gui/controllers/bisect_helper.go b/pkg/gui/controllers/bisect_helper.go new file mode 100644 index 000000000..357407fb6 --- /dev/null +++ b/pkg/gui/controllers/bisect_helper.go @@ -0,0 +1,40 @@ +package controllers + +import ( + "github.com/jesseduffield/lazygit/pkg/commands" + "github.com/jesseduffield/lazygit/pkg/gui/types" +) + +type BisectHelper struct { + c *types.ControllerCommon + git *commands.GitCommand +} + +func NewBisectHelper( + c *types.ControllerCommon, + git *commands.GitCommand, +) *BisectHelper { + return &BisectHelper{ + c: c, + git: git, + } +} + +func (self *BisectHelper) Reset() error { + return self.c.Ask(types.AskOpts{ + Title: self.c.Tr.Bisect.ResetTitle, + Prompt: self.c.Tr.Bisect.ResetPrompt, + HandleConfirm: func() error { + self.c.LogAction(self.c.Tr.Actions.ResetBisect) + if err := self.git.Bisect.Reset(); err != nil { + return self.c.Error(err) + } + + return self.PostBisectCommandRefresh() + }, + }) +} + +func (self *BisectHelper) PostBisectCommandRefresh() error { + return self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC, Scope: []types.RefreshableView{}}) +} diff --git a/pkg/gui/diffing.go b/pkg/gui/diffing.go index 3977e6b45..98833dfee 100644 --- a/pkg/gui/diffing.go +++ b/pkg/gui/diffing.go @@ -128,7 +128,7 @@ func (gui *Gui) handleCreateDiffingMenuPanel() error { OnPress: func() error { return gui.c.Prompt(types.PromptOpts{ Title: gui.c.Tr.LcEnteRefName, - FindSuggestionsFunc: gui.suggestionsHelper.GetRefsSuggestionsFunc(), + FindSuggestionsFunc: gui.helpers.suggestions.GetRefsSuggestionsFunc(), HandleConfirm: func(response string) error { gui.State.Modes.Diffing.Ref = strings.TrimSpace(response) return gui.c.Refresh(types.RefreshOptions{Mode: types.ASYNC}) diff --git a/pkg/gui/filtering_menu_panel.go b/pkg/gui/filtering_menu_panel.go index 71201859f..9e8ab9be5 100644 --- a/pkg/gui/filtering_menu_panel.go +++ b/pkg/gui/filtering_menu_panel.go @@ -37,7 +37,7 @@ func (gui *Gui) handleCreateFilteringMenuPanel() error { DisplayString: gui.c.Tr.LcFilterPathOption, OnPress: func() error { return gui.c.Prompt(types.PromptOpts{ - FindSuggestionsFunc: gui.suggestionsHelper.GetFilePathSuggestionsFunc(), + FindSuggestionsFunc: gui.helpers.suggestions.GetFilePathSuggestionsFunc(), Title: gui.c.Tr.EnterFileName, HandleConfirm: func(response string) error { return gui.setFiltering(strings.TrimSpace(response)) diff --git a/pkg/gui/gui.go b/pkg/gui/gui.go index 1d9ee2d76..09d508e44 100644 --- a/pkg/gui/gui.go +++ b/pkg/gui/gui.go @@ -68,6 +68,15 @@ func NewContextManager(initialContext types.Context) ContextManager { } } +type Helpers struct { + refs *RefsHelper + bisect *controllers.BisectHelper + suggestions *SuggestionsHelper + files *FilesHelper + workingTree *WorkingTreeHelper + tags *controllers.TagsHelper +} + type Repo string // Gui wraps the gocui Gui object which handles rendering and events @@ -143,12 +152,6 @@ type Gui struct { PrevLayout PrevLayout - c *types.ControllerCommon - refsHelper *RefsHelper - suggestionsHelper *SuggestionsHelper - filesHelper *FilesHelper - workingTreeHelper *WorkingTreeHelper - // this is the initial dir we are in upon opening lazygit. We hold onto this // in case we want to restore it before quitting for users who have set up // the feature for changing directory upon quit. @@ -156,6 +159,9 @@ type Gui struct { // is because some users want to keep track of the current lazygit directory in an outside // process InitialDir string + + c *types.ControllerCommon + helpers *Helpers } // we keep track of some stuff from one render to the next to see if certain @@ -571,23 +577,24 @@ func (gui *Gui) setControllers() { getState := func() *GuiRepoState { return gui.State } getContexts := func() context.ContextTree { return gui.State.Contexts } // TODO: have a getGit function too - refsHelper := NewRefsHelper( - controllerCommon, - gui.git, - getState, - ) - gui.refsHelper = refsHelper - gui.suggestionsHelper = NewSuggestionsHelper(controllerCommon, getState, gui.refreshSuggestions) - gui.filesHelper = NewFilesHelper(controllerCommon, gui.git, osCommand) - gui.workingTreeHelper = NewWorkingTreeHelper(func() *filetree.FileTreeViewModel { return gui.State.FileTreeViewModel }) - - tagsHelper := controllers.NewTagsHelper(controllerCommon, gui.git) + gui.helpers = &Helpers{ + refs: NewRefsHelper( + controllerCommon, + gui.git, + getState, + ), + bisect: controllers.NewBisectHelper(controllerCommon, gui.git), + suggestions: NewSuggestionsHelper(controllerCommon, getState, gui.refreshSuggestions), + files: NewFilesHelper(controllerCommon, gui.git, osCommand), + workingTree: NewWorkingTreeHelper(func() *filetree.FileTreeViewModel { return gui.State.FileTreeViewModel }), + tags: controllers.NewTagsHelper(controllerCommon, gui.git), + } syncController := controllers.NewSyncController( controllerCommon, gui.git, gui.getCheckedOutBranch, - gui.suggestionsHelper, + gui.helpers.suggestions, gui.getSuggestedRemote, gui.checkMergeOrRebase, ) @@ -616,19 +623,19 @@ func (gui *Gui) setControllers() { func() []*models.Commit { return gui.State.Commits }, gui.getSelectedPath, gui.switchToMerge, - gui.suggestionsHelper, - gui.refsHelper, - gui.filesHelper, - gui.workingTreeHelper, + gui.helpers.suggestions, + gui.helpers.refs, + gui.helpers.files, + gui.helpers.workingTree, ), Tags: controllers.NewTagsController( controllerCommon, func() *context.TagsContext { return gui.State.Contexts.Tags }, gui.git, getContexts, - tagsHelper, - refsHelper, - gui.suggestionsHelper, + gui.helpers.tags, + gui.helpers.refs, + gui.helpers.suggestions, gui.switchToSubCommitsContext, ), LocalCommits: controllers.NewLocalCommitsController( @@ -636,8 +643,8 @@ func (gui *Gui) setControllers() { func() types.IListContext { return gui.State.Contexts.BranchCommits }, osCommand, gui.git, - tagsHelper, - refsHelper, + gui.helpers.tags, + gui.helpers.refs, gui.getSelectedLocalCommit, func() []*models.Commit { return gui.State.Commits }, func() int { return gui.State.Panels.Commits.SelectedLineIdx }, @@ -668,14 +675,15 @@ func (gui *Gui) setControllers() { controllerCommon, func() types.IListContext { return gui.State.Contexts.BranchCommits }, gui.git, + gui.helpers.bisect, gui.getSelectedLocalCommit, func() []*models.Commit { return gui.State.Commits }, ), Undo: controllers.NewUndoController( controllerCommon, gui.git, - refsHelper, - gui.workingTreeHelper, + gui.helpers.refs, + gui.helpers.workingTree, func() []*models.Commit { return gui.State.FilteredReflogCommits }, ), Sync: syncController, diff --git a/pkg/gui/line_by_line_panel.go b/pkg/gui/line_by_line_panel.go index 8d283576f..b2d5f2382 100644 --- a/pkg/gui/line_by_line_panel.go +++ b/pkg/gui/line_by_line_panel.go @@ -285,5 +285,5 @@ func (gui *Gui) handleLineByLineEdit() error { } lineNumber := gui.State.Panels.LineByLine.CurrentLineNumber() - return gui.filesHelper.EditFileAtLine(file.Name, lineNumber) + return gui.helpers.files.EditFileAtLine(file.Name, lineNumber) } diff --git a/pkg/gui/modes.go b/pkg/gui/modes.go index 3f90f312d..0f2af6192 100644 --- a/pkg/gui/modes.go +++ b/pkg/gui/modes.go @@ -82,7 +82,7 @@ func (gui *Gui) modeStatuses() []modeStatus { description: func() string { return gui.withResetButton("bisecting", style.FgGreen) }, - reset: gui.Controllers.Bisect.Reset, + reset: gui.helpers.bisect.Reset, }, } } diff --git a/pkg/gui/patch_options_panel.go b/pkg/gui/patch_options_panel.go index e28e7b26d..1ae0693f1 100644 --- a/pkg/gui/patch_options_panel.go +++ b/pkg/gui/patch_options_panel.go @@ -141,7 +141,7 @@ func (gui *Gui) handleMovePatchIntoWorkingTree() error { }) } - if gui.workingTreeHelper.IsWorkingTreeDirty() { + if gui.helpers.workingTree.IsWorkingTreeDirty() { return gui.c.Ask(types.AskOpts{ Title: gui.c.Tr.MustStashTitle, Prompt: gui.c.Tr.MustStashWarning, diff --git a/pkg/gui/pull_request_menu_panel.go b/pkg/gui/pull_request_menu_panel.go index 2dce784b9..b3cc33f3f 100644 --- a/pkg/gui/pull_request_menu_panel.go +++ b/pkg/gui/pull_request_menu_panel.go @@ -28,7 +28,7 @@ func (gui *Gui) createPullRequestMenu(selectedBranch *models.Branch, checkedOutB OnPress: func() error { return gui.c.Prompt(types.PromptOpts{ Title: branch.Name + " →", - FindSuggestionsFunc: gui.suggestionsHelper.GetBranchNameSuggestionsFunc(), + FindSuggestionsFunc: gui.helpers.suggestions.GetBranchNameSuggestionsFunc(), HandleConfirm: func(targetBranchName string) error { return gui.createPullRequest(branch.Name, targetBranchName) }}, diff --git a/pkg/gui/reflog_panel.go b/pkg/gui/reflog_panel.go index 4857840d2..39292c7ba 100644 --- a/pkg/gui/reflog_panel.go +++ b/pkg/gui/reflog_panel.go @@ -48,7 +48,7 @@ func (gui *Gui) CheckoutReflogCommit() error { Prompt: gui.c.Tr.SureCheckoutThisCommit, HandleConfirm: func() error { gui.c.LogAction(gui.c.Tr.Actions.CheckoutReflogCommit) - return gui.refsHelper.CheckoutRef(commit.Sha, types.CheckoutRefOptions{}) + return gui.helpers.refs.CheckoutRef(commit.Sha, types.CheckoutRefOptions{}) }, }) if err != nil { @@ -63,7 +63,7 @@ func (gui *Gui) CheckoutReflogCommit() error { func (gui *Gui) handleCreateReflogResetMenu() error { commit := gui.getSelectedReflogCommit() - return gui.refsHelper.CreateGitResetMenu(commit.Sha) + return gui.helpers.refs.CreateGitResetMenu(commit.Sha) } func (gui *Gui) handleViewReflogCommitFiles() error { diff --git a/pkg/gui/remote_branches_panel.go b/pkg/gui/remote_branches_panel.go index 882dea42e..5f60a4211 100644 --- a/pkg/gui/remote_branches_panel.go +++ b/pkg/gui/remote_branches_panel.go @@ -107,5 +107,5 @@ func (gui *Gui) handleCreateResetToRemoteBranchMenu() error { return nil } - return gui.refsHelper.CreateGitResetMenu(selectedBranch.FullName()) + return gui.helpers.refs.CreateGitResetMenu(selectedBranch.FullName()) } diff --git a/pkg/gui/status_panel.go b/pkg/gui/status_panel.go index f79a5e9cd..1b53fc39c 100644 --- a/pkg/gui/status_panel.go +++ b/pkg/gui/status_panel.go @@ -130,11 +130,11 @@ func (gui *Gui) askForConfigFile(action func(file string) error) error { } func (gui *Gui) handleOpenConfig() error { - return gui.askForConfigFile(gui.filesHelper.OpenFile) + return gui.askForConfigFile(gui.helpers.files.OpenFile) } func (gui *Gui) handleEditConfig() error { - return gui.askForConfigFile(gui.filesHelper.EditFile) + return gui.askForConfigFile(gui.helpers.files.EditFile) } func lazygitTitle() string { diff --git a/pkg/gui/sub_commits_panel.go b/pkg/gui/sub_commits_panel.go index 0c7b7a0b5..9f3eea642 100644 --- a/pkg/gui/sub_commits_panel.go +++ b/pkg/gui/sub_commits_panel.go @@ -49,7 +49,7 @@ func (gui *Gui) handleCheckoutSubCommit() error { Prompt: gui.c.Tr.SureCheckoutThisCommit, HandleConfirm: func() error { gui.c.LogAction(gui.c.Tr.Actions.CheckoutCommit) - return gui.refsHelper.CheckoutRef(commit.Sha, types.CheckoutRefOptions{}) + return gui.helpers.refs.CheckoutRef(commit.Sha, types.CheckoutRefOptions{}) }, }) if err != nil { @@ -64,7 +64,7 @@ func (gui *Gui) handleCheckoutSubCommit() error { func (gui *Gui) handleCreateSubCommitResetMenu() error { commit := gui.getSelectedSubCommit() - return gui.refsHelper.CreateGitResetMenu(commit.Sha) + return gui.helpers.refs.CreateGitResetMenu(commit.Sha) } func (gui *Gui) handleViewSubCommitFiles() error { diff --git a/pkg/gui/submodules_panel.go b/pkg/gui/submodules_panel.go index 2817ef7a5..297887207 100644 --- a/pkg/gui/submodules_panel.go +++ b/pkg/gui/submodules_panel.go @@ -30,7 +30,7 @@ func (gui *Gui) submodulesRenderToMain() error { style.FgCyan.Sprint(submodule.Url), ) - file := gui.workingTreeHelper.FileForSubmodule(submodule) + file := gui.helpers.workingTree.FileForSubmodule(submodule) if file == nil { task = NewRenderStringTask(prefix) } else { |