1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-01-26 05:37:18 +02:00
lazygit/pkg/gui/controllers/switch_to_diff_files_controller.go

96 lines
2.4 KiB
Go
Raw Normal View History

2022-02-13 18:26:44 +11:00
package controllers
import (
"github.com/jesseduffield/lazygit/pkg/gui/context"
2022-02-13 18:26:44 +11:00
"github.com/jesseduffield/lazygit/pkg/gui/types"
)
// This controller is for all contexts that contain commit files.
2022-03-26 17:03:30 +11:00
var _ types.IController = &SwitchToDiffFilesController{}
2022-02-13 18:26:44 +11:00
2022-03-26 17:03:30 +11:00
type CanSwitchToDiffFiles interface {
2022-02-13 18:26:44 +11:00
types.Context
CanRebase() bool
2022-03-26 22:18:08 +09:00
GetSelectedRef() types.Ref
2022-02-13 18:26:44 +11:00
}
2022-03-26 17:03:30 +11:00
type SwitchToDiffFilesController struct {
2022-02-13 18:26:44 +11:00
baseController
2023-03-23 18:47:29 +11:00
c *ControllerCommon
context CanSwitchToDiffFiles
diffFilesContext *context.CommitFilesContext
2022-02-13 18:26:44 +11:00
}
2022-03-26 17:03:30 +11:00
func NewSwitchToDiffFilesController(
2023-03-23 18:47:29 +11:00
c *ControllerCommon,
2022-03-26 17:03:30 +11:00
context CanSwitchToDiffFiles,
diffFilesContext *context.CommitFilesContext,
2022-03-26 17:03:30 +11:00
) *SwitchToDiffFilesController {
return &SwitchToDiffFilesController{
2022-02-13 18:26:44 +11:00
baseController: baseController{},
2023-03-23 18:47:29 +11:00
c: c,
2022-02-13 18:26:44 +11:00
context: context,
diffFilesContext: diffFilesContext,
2022-02-13 18:26:44 +11:00
}
}
2022-03-26 17:03:30 +11:00
func (self *SwitchToDiffFilesController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding {
2022-02-13 18:26:44 +11:00
bindings := []*types.Binding{
{
Key: opts.GetKey(opts.Config.Universal.GoInto),
Handler: self.checkSelected(self.enter),
Description: self.c.Tr.ViewItemFiles,
2022-02-13 18:26:44 +11:00
},
}
return bindings
}
2022-03-26 17:03:30 +11:00
func (self *SwitchToDiffFilesController) GetOnClick() func() error {
2022-02-27 11:42:22 +11:00
return self.checkSelected(self.enter)
}
2022-03-26 22:18:08 +09:00
func (self *SwitchToDiffFilesController) checkSelected(callback func(types.Ref) error) func() error {
2022-02-13 18:26:44 +11:00
return func() error {
2022-03-26 22:18:08 +09:00
ref := self.context.GetSelectedRef()
if ref == nil {
2022-02-13 18:26:44 +11:00
return nil
}
2022-03-26 22:18:08 +09:00
return callback(ref)
2022-02-13 18:26:44 +11:00
}
}
2022-03-26 22:18:08 +09:00
func (self *SwitchToDiffFilesController) enter(ref types.Ref) error {
2022-02-13 18:26:44 +11:00
return self.viewFiles(SwitchToCommitFilesContextOpts{
2022-03-26 22:18:08 +09:00
Ref: ref,
CanRebase: self.context.CanRebase(),
Context: self.context,
2022-02-13 18:26:44 +11:00
})
}
2022-03-26 17:03:30 +11:00
func (self *SwitchToDiffFilesController) Context() types.Context {
2022-02-13 18:26:44 +11:00
return self.context
}
func (self *SwitchToDiffFilesController) viewFiles(opts SwitchToCommitFilesContextOpts) error {
diffFilesContext := self.diffFilesContext
diffFilesContext.SetSelectedLineIdx(0)
diffFilesContext.SetRef(opts.Ref)
diffFilesContext.SetTitleRef(opts.Ref.Description())
diffFilesContext.SetCanRebase(opts.CanRebase)
diffFilesContext.SetParentContext(opts.Context)
diffFilesContext.SetWindowName(opts.Context.GetWindowName())
diffFilesContext.ClearSearchString()
if err := self.c.Refresh(types.RefreshOptions{
Scope: []types.RefreshableView{types.COMMIT_FILES},
}); err != nil {
return err
}
return self.c.PushContext(diffFilesContext)
}