mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-04-19 12:12:42 +02:00
refactor to make code clearer
This commit is contained in:
parent
45dab51214
commit
077b6eb8a3
@ -129,41 +129,35 @@ func (gui *Gui) resetControllers() {
|
|||||||
stashController := controllers.NewStashController(common)
|
stashController := controllers.NewStashController(common)
|
||||||
commitFilesController := controllers.NewCommitFilesController(common)
|
commitFilesController := controllers.NewCommitFilesController(common)
|
||||||
|
|
||||||
switchToSubCommitsControllerFactory := controllers.NewSubCommitsSwitchControllerFactory(
|
setSubCommits := func(commits []*models.Commit) { gui.State.Model.SubCommits = commits }
|
||||||
common,
|
|
||||||
func(commits []*models.Commit) { gui.State.Model.SubCommits = commits },
|
|
||||||
)
|
|
||||||
|
|
||||||
for _, context := range []controllers.ContextWithRefName{
|
for _, context := range []controllers.ContextWithRefName{
|
||||||
gui.State.Contexts.Branches,
|
gui.State.Contexts.Branches,
|
||||||
gui.State.Contexts.RemoteBranches,
|
gui.State.Contexts.RemoteBranches,
|
||||||
gui.State.Contexts.Tags,
|
gui.State.Contexts.Tags,
|
||||||
} {
|
} {
|
||||||
controllers.AttachControllers(context, switchToSubCommitsControllerFactory.Create(context))
|
controllers.AttachControllers(context, controllers.NewSwitchToSubCommitsController(
|
||||||
|
common, setSubCommits, context,
|
||||||
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
commitishControllerFactory := controllers.NewCommitishControllerFactory(
|
for _, context := range []controllers.CanSwitchToDiffFiles{
|
||||||
common,
|
|
||||||
gui.SwitchToCommitFilesContext,
|
|
||||||
)
|
|
||||||
|
|
||||||
for _, context := range []controllers.Commitish{
|
|
||||||
gui.State.Contexts.LocalCommits,
|
gui.State.Contexts.LocalCommits,
|
||||||
gui.State.Contexts.ReflogCommits,
|
gui.State.Contexts.ReflogCommits,
|
||||||
gui.State.Contexts.SubCommits,
|
gui.State.Contexts.SubCommits,
|
||||||
gui.State.Contexts.Stash,
|
gui.State.Contexts.Stash,
|
||||||
} {
|
} {
|
||||||
controllers.AttachControllers(context, commitishControllerFactory.Create(context))
|
controllers.AttachControllers(context, controllers.NewSwitchToDiffFilesController(
|
||||||
|
common, gui.SwitchToCommitFilesContext, context,
|
||||||
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
basicCommitsControllerFactory := controllers.NewBasicCommitsControllerFactory(common)
|
|
||||||
|
|
||||||
for _, context := range []controllers.ContainsCommits{
|
for _, context := range []controllers.ContainsCommits{
|
||||||
gui.State.Contexts.LocalCommits,
|
gui.State.Contexts.LocalCommits,
|
||||||
gui.State.Contexts.ReflogCommits,
|
gui.State.Contexts.ReflogCommits,
|
||||||
gui.State.Contexts.SubCommits,
|
gui.State.Contexts.SubCommits,
|
||||||
} {
|
} {
|
||||||
controllers.AttachControllers(context, basicCommitsControllerFactory.Create(context))
|
controllers.AttachControllers(context, controllers.NewBasicCommitsController(common, context))
|
||||||
}
|
}
|
||||||
|
|
||||||
controllers.AttachControllers(gui.State.Contexts.Branches, branchesController, gitFlowController)
|
controllers.AttachControllers(gui.State.Contexts.Branches, branchesController, gitFlowController)
|
||||||
|
@ -8,10 +8,6 @@ import (
|
|||||||
|
|
||||||
// This controller is for all contexts that contain a list of commits.
|
// This controller is for all contexts that contain a list of commits.
|
||||||
|
|
||||||
type BasicCommitsControllerFactory struct {
|
|
||||||
controllerCommon *controllerCommon
|
|
||||||
}
|
|
||||||
|
|
||||||
var _ types.IController = &BasicCommitsController{}
|
var _ types.IController = &BasicCommitsController{}
|
||||||
|
|
||||||
type ContainsCommits interface {
|
type ContainsCommits interface {
|
||||||
@ -27,18 +23,10 @@ type BasicCommitsController struct {
|
|||||||
context ContainsCommits
|
context ContainsCommits
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewBasicCommitsControllerFactory(
|
func NewBasicCommitsController(controllerCommon *controllerCommon, context ContainsCommits) *BasicCommitsController {
|
||||||
common *controllerCommon,
|
|
||||||
) *BasicCommitsControllerFactory {
|
|
||||||
return &BasicCommitsControllerFactory{
|
|
||||||
controllerCommon: common,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (self *BasicCommitsControllerFactory) Create(context ContainsCommits) *BasicCommitsController {
|
|
||||||
return &BasicCommitsController{
|
return &BasicCommitsController{
|
||||||
baseController: baseController{},
|
baseController: baseController{},
|
||||||
controllerCommon: self.controllerCommon,
|
controllerCommon: controllerCommon,
|
||||||
context: context,
|
context: context,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,47 +6,35 @@ import (
|
|||||||
|
|
||||||
// This controller is for all contexts that contain commit files.
|
// This controller is for all contexts that contain commit files.
|
||||||
|
|
||||||
type CommitishControllerFactory struct {
|
var _ types.IController = &SwitchToDiffFilesController{}
|
||||||
controllerCommon *controllerCommon
|
|
||||||
viewFiles func(SwitchToCommitFilesContextOpts) error
|
|
||||||
}
|
|
||||||
|
|
||||||
var _ types.IController = &CommitishController{}
|
type CanSwitchToDiffFiles interface {
|
||||||
|
|
||||||
type Commitish interface {
|
|
||||||
types.Context
|
types.Context
|
||||||
CanRebase() bool
|
CanRebase() bool
|
||||||
GetSelectedRefName() string
|
GetSelectedRefName() string
|
||||||
}
|
}
|
||||||
|
|
||||||
type CommitishController struct {
|
type SwitchToDiffFilesController struct {
|
||||||
baseController
|
baseController
|
||||||
*controllerCommon
|
*controllerCommon
|
||||||
context Commitish
|
context CanSwitchToDiffFiles
|
||||||
|
|
||||||
viewFiles func(SwitchToCommitFilesContextOpts) error
|
viewFiles func(SwitchToCommitFilesContextOpts) error
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewCommitishControllerFactory(
|
func NewSwitchToDiffFilesController(
|
||||||
common *controllerCommon,
|
controllerCommon *controllerCommon,
|
||||||
viewFiles func(SwitchToCommitFilesContextOpts) error,
|
viewFiles func(SwitchToCommitFilesContextOpts) error,
|
||||||
) *CommitishControllerFactory {
|
context CanSwitchToDiffFiles,
|
||||||
return &CommitishControllerFactory{
|
) *SwitchToDiffFilesController {
|
||||||
controllerCommon: common,
|
return &SwitchToDiffFilesController{
|
||||||
|
baseController: baseController{},
|
||||||
|
controllerCommon: controllerCommon,
|
||||||
|
context: context,
|
||||||
viewFiles: viewFiles,
|
viewFiles: viewFiles,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *CommitishControllerFactory) Create(context Commitish) *CommitishController {
|
func (self *SwitchToDiffFilesController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding {
|
||||||
return &CommitishController{
|
|
||||||
baseController: baseController{},
|
|
||||||
controllerCommon: self.controllerCommon,
|
|
||||||
context: context,
|
|
||||||
viewFiles: self.viewFiles,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (self *CommitishController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding {
|
|
||||||
bindings := []*types.Binding{
|
bindings := []*types.Binding{
|
||||||
{
|
{
|
||||||
Key: opts.GetKey(opts.Config.Universal.GoInto),
|
Key: opts.GetKey(opts.Config.Universal.GoInto),
|
||||||
@ -58,11 +46,11 @@ func (self *CommitishController) GetKeybindings(opts types.KeybindingsOpts) []*t
|
|||||||
return bindings
|
return bindings
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *CommitishController) GetOnClick() func() error {
|
func (self *SwitchToDiffFilesController) GetOnClick() func() error {
|
||||||
return self.checkSelected(self.enter)
|
return self.checkSelected(self.enter)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *CommitishController) checkSelected(callback func(string) error) func() error {
|
func (self *SwitchToDiffFilesController) checkSelected(callback func(string) error) func() error {
|
||||||
return func() error {
|
return func() error {
|
||||||
refName := self.context.GetSelectedRefName()
|
refName := self.context.GetSelectedRefName()
|
||||||
if refName == "" {
|
if refName == "" {
|
||||||
@ -73,7 +61,7 @@ func (self *CommitishController) checkSelected(callback func(string) error) func
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *CommitishController) enter(refName string) error {
|
func (self *SwitchToDiffFilesController) enter(refName string) error {
|
||||||
return self.viewFiles(SwitchToCommitFilesContextOpts{
|
return self.viewFiles(SwitchToCommitFilesContextOpts{
|
||||||
RefName: refName,
|
RefName: refName,
|
||||||
CanRebase: self.context.CanRebase(),
|
CanRebase: self.context.CanRebase(),
|
||||||
@ -81,6 +69,6 @@ func (self *CommitishController) enter(refName string) error {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *CommitishController) Context() types.Context {
|
func (self *SwitchToDiffFilesController) Context() types.Context {
|
||||||
return self.context
|
return self.context
|
||||||
}
|
}
|
@ -6,19 +6,14 @@ import (
|
|||||||
"github.com/jesseduffield/lazygit/pkg/gui/types"
|
"github.com/jesseduffield/lazygit/pkg/gui/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
type SubCommitsSwitchControllerFactory struct {
|
var _ types.IController = &SwitchToSubCommitsController{}
|
||||||
controllerCommon *controllerCommon
|
|
||||||
setSubCommits func([]*models.Commit)
|
|
||||||
}
|
|
||||||
|
|
||||||
var _ types.IController = &SubCommitsSwitchController{}
|
|
||||||
|
|
||||||
type ContextWithRefName interface {
|
type ContextWithRefName interface {
|
||||||
types.Context
|
types.Context
|
||||||
GetSelectedRefName() string
|
GetSelectedRefName() string
|
||||||
}
|
}
|
||||||
|
|
||||||
type SubCommitsSwitchController struct {
|
type SwitchToSubCommitsController struct {
|
||||||
baseController
|
baseController
|
||||||
*controllerCommon
|
*controllerCommon
|
||||||
context ContextWithRefName
|
context ContextWithRefName
|
||||||
@ -26,26 +21,20 @@ type SubCommitsSwitchController struct {
|
|||||||
setSubCommits func([]*models.Commit)
|
setSubCommits func([]*models.Commit)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSubCommitsSwitchControllerFactory(
|
func NewSwitchToSubCommitsController(
|
||||||
common *controllerCommon,
|
controllerCommon *controllerCommon,
|
||||||
setSubCommits func([]*models.Commit),
|
setSubCommits func([]*models.Commit),
|
||||||
) *SubCommitsSwitchControllerFactory {
|
context ContextWithRefName,
|
||||||
return &SubCommitsSwitchControllerFactory{
|
) *SwitchToSubCommitsController {
|
||||||
controllerCommon: common,
|
return &SwitchToSubCommitsController{
|
||||||
|
baseController: baseController{},
|
||||||
|
controllerCommon: controllerCommon,
|
||||||
|
context: context,
|
||||||
setSubCommits: setSubCommits,
|
setSubCommits: setSubCommits,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *SubCommitsSwitchControllerFactory) Create(context ContextWithRefName) *SubCommitsSwitchController {
|
func (self *SwitchToSubCommitsController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding {
|
||||||
return &SubCommitsSwitchController{
|
|
||||||
baseController: baseController{},
|
|
||||||
controllerCommon: self.controllerCommon,
|
|
||||||
context: context,
|
|
||||||
setSubCommits: self.setSubCommits,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (self *SubCommitsSwitchController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding {
|
|
||||||
bindings := []*types.Binding{
|
bindings := []*types.Binding{
|
||||||
{
|
{
|
||||||
Handler: self.viewCommits,
|
Handler: self.viewCommits,
|
||||||
@ -57,11 +46,11 @@ func (self *SubCommitsSwitchController) GetKeybindings(opts types.KeybindingsOpt
|
|||||||
return bindings
|
return bindings
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *SubCommitsSwitchController) GetOnClick() func() error {
|
func (self *SwitchToSubCommitsController) GetOnClick() func() error {
|
||||||
return self.viewCommits
|
return self.viewCommits
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *SubCommitsSwitchController) viewCommits() error {
|
func (self *SwitchToSubCommitsController) viewCommits() error {
|
||||||
refName := self.context.GetSelectedRefName()
|
refName := self.context.GetSelectedRefName()
|
||||||
if refName == "" {
|
if refName == "" {
|
||||||
return nil
|
return nil
|
||||||
@ -87,6 +76,6 @@ func (self *SubCommitsSwitchController) viewCommits() error {
|
|||||||
return self.c.PushContext(self.contexts.SubCommits)
|
return self.c.PushContext(self.contexts.SubCommits)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *SubCommitsSwitchController) Context() types.Context {
|
func (self *SwitchToSubCommitsController) Context() types.Context {
|
||||||
return self.context
|
return self.context
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user