summaryrefslogtreecommitdiffstats
path: root/pkg/gui/controllers/switch_to_diff_files_controller.go
diff options
context:
space:
mode:
authorJesse Duffield <jessedduffield@gmail.com>2022-03-26 17:03:30 +1100
committerJesse Duffield <jessedduffield@gmail.com>2022-03-26 17:22:42 +1100
commit077b6eb8a34f28d9d11c43b65d4b2a54835b0f31 (patch)
tree68ae6acf80cdfc0e60bc3df943a22f0d15cdbbe2 /pkg/gui/controllers/switch_to_diff_files_controller.go
parent45dab51214d7deca689279a6a162d80ee27befc8 (diff)
refactor to make code clearer
Diffstat (limited to 'pkg/gui/controllers/switch_to_diff_files_controller.go')
-rw-r--r--pkg/gui/controllers/switch_to_diff_files_controller.go74
1 files changed, 74 insertions, 0 deletions
diff --git a/pkg/gui/controllers/switch_to_diff_files_controller.go b/pkg/gui/controllers/switch_to_diff_files_controller.go
new file mode 100644
index 000000000..9a3111cae
--- /dev/null
+++ b/pkg/gui/controllers/switch_to_diff_files_controller.go
@@ -0,0 +1,74 @@
+package controllers
+
+import (
+ "github.com/jesseduffield/lazygit/pkg/gui/types"
+)
+
+// This controller is for all contexts that contain commit files.
+
+var _ types.IController = &SwitchToDiffFilesController{}
+
+type CanSwitchToDiffFiles interface {
+ types.Context
+ CanRebase() bool
+ GetSelectedRefName() string
+}
+
+type SwitchToDiffFilesController struct {
+ baseController
+ *controllerCommon
+ context CanSwitchToDiffFiles
+ viewFiles func(SwitchToCommitFilesContextOpts) error
+}
+
+func NewSwitchToDiffFilesController(
+ controllerCommon *controllerCommon,
+ viewFiles func(SwitchToCommitFilesContextOpts) error,
+ context CanSwitchToDiffFiles,
+) *SwitchToDiffFilesController {
+ return &SwitchToDiffFilesController{
+ baseController: baseController{},
+ controllerCommon: controllerCommon,
+ context: context,
+ viewFiles: viewFiles,
+ }
+}
+
+func (self *SwitchToDiffFilesController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding {
+ bindings := []*types.Binding{
+ {
+ Key: opts.GetKey(opts.Config.Universal.GoInto),
+ Handler: self.checkSelected(self.enter),
+ Description: self.c.Tr.LcViewItemFiles,
+ },
+ }
+
+ return bindings
+}
+
+func (self *SwitchToDiffFilesController) GetOnClick() func() error {
+ return self.checkSelected(self.enter)
+}
+
+func (self *SwitchToDiffFilesController) checkSelected(callback func(string) error) func() error {
+ return func() error {
+ refName := self.context.GetSelectedRefName()
+ if refName == "" {
+ return nil
+ }
+
+ return callback(refName)
+ }
+}
+
+func (self *SwitchToDiffFilesController) enter(refName string) error {
+ return self.viewFiles(SwitchToCommitFilesContextOpts{
+ RefName: refName,
+ CanRebase: self.context.CanRebase(),
+ Context: self.context,
+ })
+}
+
+func (self *SwitchToDiffFilesController) Context() types.Context {
+ return self.context
+}