summaryrefslogtreecommitdiffstats
path: root/pkg/gui/controllers/switch_to_diff_files_controller.go
blob: 9a3111caea6dd5efb4060bbe24281369c3a1ebb4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
}