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

75 lines
1.8 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
2022-03-26 15:18:08 +02:00
GetSelectedRef() types.Ref
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 15:18:08 +02:00
func (self *SwitchToDiffFilesController) checkSelected(callback func(types.Ref) error) func() error {
2022-02-13 09:26:44 +02:00
return func() error {
2022-03-26 15:18:08 +02:00
ref := self.context.GetSelectedRef()
if ref == nil {
2022-02-13 09:26:44 +02:00
return nil
}
2022-03-26 15:18:08 +02:00
return callback(ref)
2022-02-13 09:26:44 +02:00
}
}
2022-03-26 15:18:08 +02:00
func (self *SwitchToDiffFilesController) enter(ref types.Ref) error {
2022-02-13 09:26:44 +02:00
return self.viewFiles(SwitchToCommitFilesContextOpts{
2022-03-26 15:18:08 +02:00
Ref: ref,
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
}