1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-04-02 22:25:47 +02:00

move helpers into their own struct

This commit is contained in:
Jesse Duffield 2022-01-30 10:40:48 +11:00
parent 2692637fbe
commit f97de692e3
16 changed files with 111 additions and 79 deletions

View File

@ -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 {

View File

@ -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 {

View File

@ -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()
}
}

View File

@ -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{}})
}

View File

@ -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})

View File

@ -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))

View File

@ -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,

View File

@ -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)
}

View File

@ -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,
},
}
}

View File

@ -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,

View File

@ -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)
}},

View File

@ -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 {

View File

@ -107,5 +107,5 @@ func (gui *Gui) handleCreateResetToRemoteBranchMenu() error {
return nil
}
return gui.refsHelper.CreateGitResetMenu(selectedBranch.FullName())
return gui.helpers.refs.CreateGitResetMenu(selectedBranch.FullName())
}

View File

@ -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 {

View File

@ -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 {

View File

@ -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 {