1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-12 11:15:00 +02:00
lazygit/pkg/gui/controllers/switch_to_diff_files_controller.go

77 lines
1.9 KiB
Go
Raw Normal View History

2022-02-13 09:26:44 +02:00
package controllers
import (
"github.com/jesseduffield/lazygit/pkg/gui/types"
)
// This controller is for all contexts that contain commit files.
2022-03-26 08:03:30 +02:00
var _ types.IController = &SwitchToDiffFilesController{}
2022-02-13 09:26:44 +02:00
2022-03-26 08:03:30 +02:00
type CanSwitchToDiffFiles interface {
2022-02-13 09:26:44 +02:00
types.Context
CanRebase() bool
GetSelectedRefName() string
2022-03-26 05:44:30 +02:00
GetSelectedDescription() string
2022-02-13 09:26:44 +02:00
}
2022-03-26 08:03:30 +02:00
type SwitchToDiffFilesController struct {
2022-02-13 09:26:44 +02:00
baseController
*controllerCommon
2022-03-26 08:03:30 +02:00
context CanSwitchToDiffFiles
2022-02-13 09:26:44 +02:00
viewFiles func(SwitchToCommitFilesContextOpts) error
}
2022-03-26 08:03:30 +02:00
func NewSwitchToDiffFilesController(
controllerCommon *controllerCommon,
2022-02-13 09:26:44 +02:00
viewFiles func(SwitchToCommitFilesContextOpts) error,
2022-03-26 08:03:30 +02:00
context CanSwitchToDiffFiles,
) *SwitchToDiffFilesController {
return &SwitchToDiffFilesController{
2022-02-13 09:26:44 +02:00
baseController: baseController{},
2022-03-26 08:03:30 +02:00
controllerCommon: controllerCommon,
2022-02-13 09:26:44 +02:00
context: context,
2022-03-26 08:03:30 +02:00
viewFiles: viewFiles,
2022-02-13 09:26:44 +02:00
}
}
2022-03-26 08:03:30 +02:00
func (self *SwitchToDiffFilesController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding {
2022-02-13 09:26:44 +02:00
bindings := []*types.Binding{
{
Key: opts.GetKey(opts.Config.Universal.GoInto),
Handler: self.checkSelected(self.enter),
Description: self.c.Tr.LcViewItemFiles,
},
}
return bindings
}
2022-03-26 08:03:30 +02:00
func (self *SwitchToDiffFilesController) GetOnClick() func() error {
2022-02-27 02:42:22 +02:00
return self.checkSelected(self.enter)
}
2022-03-26 08:03:30 +02:00
func (self *SwitchToDiffFilesController) checkSelected(callback func(string) error) func() error {
2022-02-13 09:26:44 +02:00
return func() error {
refName := self.context.GetSelectedRefName()
if refName == "" {
return nil
}
return callback(refName)
}
}
2022-03-26 08:03:30 +02:00
func (self *SwitchToDiffFilesController) enter(refName string) error {
2022-02-13 09:26:44 +02:00
return self.viewFiles(SwitchToCommitFilesContextOpts{
2022-03-26 05:44:30 +02:00
RefName: refName,
RefDescription: self.context.GetSelectedDescription(),
CanRebase: self.context.CanRebase(),
Context: self.context,
2022-02-13 09:26:44 +02:00
})
}
2022-03-26 08:03:30 +02:00
func (self *SwitchToDiffFilesController) Context() types.Context {
2022-02-13 09:26:44 +02:00
return self.context
}