mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-05-11 04:15:48 +02:00
refactor to make code clearer
This commit is contained in:
parent
45dab51214
commit
077b6eb8a3
4 changed files with 42 additions and 83 deletions
74
pkg/gui/controllers/switch_to_diff_files_controller.go
Normal file
74
pkg/gui/controllers/switch_to_diff_files_controller.go
Normal file
|
@ -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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue