diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2022-03-26 17:03:30 +1100 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2022-03-26 17:22:42 +1100 |
commit | 077b6eb8a34f28d9d11c43b65d4b2a54835b0f31 (patch) | |
tree | 68ae6acf80cdfc0e60bc3df943a22f0d15cdbbe2 /pkg/gui/controllers | |
parent | 45dab51214d7deca689279a6a162d80ee27befc8 (diff) |
refactor to make code clearer
Diffstat (limited to 'pkg/gui/controllers')
-rw-r--r-- | pkg/gui/controllers/basic_commits_controller.go | 16 | ||||
-rw-r--r-- | pkg/gui/controllers/switch_to_diff_files_controller.go (renamed from pkg/gui/controllers/commitish_controller.go) | 44 | ||||
-rw-r--r-- | pkg/gui/controllers/switch_to_sub_commits_controller.go (renamed from pkg/gui/controllers/sub_commits_switch_controller.go) | 37 |
3 files changed, 31 insertions, 66 deletions
diff --git a/pkg/gui/controllers/basic_commits_controller.go b/pkg/gui/controllers/basic_commits_controller.go index c59686126..bcd180612 100644 --- a/pkg/gui/controllers/basic_commits_controller.go +++ b/pkg/gui/controllers/basic_commits_controller.go @@ -8,10 +8,6 @@ import ( // This controller is for all contexts that contain a list of commits. -type BasicCommitsControllerFactory struct { - controllerCommon *controllerCommon -} - var _ types.IController = &BasicCommitsController{} type ContainsCommits interface { @@ -27,18 +23,10 @@ type BasicCommitsController struct { context ContainsCommits } -func NewBasicCommitsControllerFactory( - common *controllerCommon, -) *BasicCommitsControllerFactory { - return &BasicCommitsControllerFactory{ - controllerCommon: common, - } -} - -func (self *BasicCommitsControllerFactory) Create(context ContainsCommits) *BasicCommitsController { +func NewBasicCommitsController(controllerCommon *controllerCommon, context ContainsCommits) *BasicCommitsController { return &BasicCommitsController{ baseController: baseController{}, - controllerCommon: self.controllerCommon, + controllerCommon: controllerCommon, context: context, } } diff --git a/pkg/gui/controllers/commitish_controller.go b/pkg/gui/controllers/switch_to_diff_files_controller.go index 04e271253..9a3111cae 100644 --- a/pkg/gui/controllers/commitish_controller.go +++ b/pkg/gui/controllers/switch_to_diff_files_controller.go @@ -6,47 +6,35 @@ import ( // This controller is for all contexts that contain commit files. -type CommitishControllerFactory struct { - controllerCommon *controllerCommon - viewFiles func(SwitchToCommitFilesContextOpts) error -} - -var _ types.IController = &CommitishController{} +var _ types.IController = &SwitchToDiffFilesController{} -type Commitish interface { +type CanSwitchToDiffFiles interface { types.Context CanRebase() bool GetSelectedRefName() string } -type CommitishController struct { +type SwitchToDiffFilesController struct { baseController *controllerCommon - context Commitish - + context CanSwitchToDiffFiles viewFiles func(SwitchToCommitFilesContextOpts) error } -func NewCommitishControllerFactory( - common *controllerCommon, +func NewSwitchToDiffFilesController( + controllerCommon *controllerCommon, viewFiles func(SwitchToCommitFilesContextOpts) error, -) *CommitishControllerFactory { - return &CommitishControllerFactory{ - controllerCommon: common, - viewFiles: viewFiles, - } -} - -func (self *CommitishControllerFactory) Create(context Commitish) *CommitishController { - return &CommitishController{ + context CanSwitchToDiffFiles, +) *SwitchToDiffFilesController { + return &SwitchToDiffFilesController{ baseController: baseController{}, - controllerCommon: self.controllerCommon, + controllerCommon: controllerCommon, context: context, - viewFiles: self.viewFiles, + viewFiles: viewFiles, } } -func (self *CommitishController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding { +func (self *SwitchToDiffFilesController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding { bindings := []*types.Binding{ { Key: opts.GetKey(opts.Config.Universal.GoInto), @@ -58,11 +46,11 @@ func (self *CommitishController) GetKeybindings(opts types.KeybindingsOpts) []*t return bindings } -func (self *CommitishController) GetOnClick() func() error { +func (self *SwitchToDiffFilesController) GetOnClick() func() error { 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 { refName := self.context.GetSelectedRefName() 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{ RefName: refName, 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 } diff --git a/pkg/gui/controllers/sub_commits_switch_controller.go b/pkg/gui/controllers/switch_to_sub_commits_controller.go index 4c8f086a5..f7e9f6702 100644 --- a/pkg/gui/controllers/sub_commits_switch_controller.go +++ b/pkg/gui/controllers/switch_to_sub_commits_controller.go @@ -6,19 +6,14 @@ import ( "github.com/jesseduffield/lazygit/pkg/gui/types" ) -type SubCommitsSwitchControllerFactory struct { - controllerCommon *controllerCommon - setSubCommits func([]*models.Commit) -} - -var _ types.IController = &SubCommitsSwitchController{} +var _ types.IController = &SwitchToSubCommitsController{} type ContextWithRefName interface { types.Context GetSelectedRefName() string } -type SubCommitsSwitchController struct { +type SwitchToSubCommitsController struct { baseController *controllerCommon context ContextWithRefName @@ -26,26 +21,20 @@ type SubCommitsSwitchController struct { setSubCommits func([]*models.Commit) } -func NewSubCommitsSwitchControllerFactory( - common *controllerCommon, +func NewSwitchToSubCommitsController( + controllerCommon *controllerCommon, setSubCommits func([]*models.Commit), -) *SubCommitsSwitchControllerFactory { - return &SubCommitsSwitchControllerFactory{ - controllerCommon: common, - setSubCommits: setSubCommits, - } -} - -func (self *SubCommitsSwitchControllerFactory) Create(context ContextWithRefName) *SubCommitsSwitchController { - return &SubCommitsSwitchController{ + context ContextWithRefName, +) *SwitchToSubCommitsController { + return &SwitchToSubCommitsController{ baseController: baseController{}, - controllerCommon: self.controllerCommon, + controllerCommon: controllerCommon, context: context, - setSubCommits: self.setSubCommits, + setSubCommits: setSubCommits, } } -func (self *SubCommitsSwitchController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding { +func (self *SwitchToSubCommitsController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding { bindings := []*types.Binding{ { Handler: self.viewCommits, @@ -57,11 +46,11 @@ func (self *SubCommitsSwitchController) GetKeybindings(opts types.KeybindingsOpt return bindings } -func (self *SubCommitsSwitchController) GetOnClick() func() error { +func (self *SwitchToSubCommitsController) GetOnClick() func() error { return self.viewCommits } -func (self *SubCommitsSwitchController) viewCommits() error { +func (self *SwitchToSubCommitsController) viewCommits() error { refName := self.context.GetSelectedRefName() if refName == "" { return nil @@ -87,6 +76,6 @@ func (self *SubCommitsSwitchController) viewCommits() error { return self.c.PushContext(self.contexts.SubCommits) } -func (self *SubCommitsSwitchController) Context() types.Context { +func (self *SwitchToSubCommitsController) Context() types.Context { return self.context } |