2022-02-13 18:26:44 +11:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
2022-12-30 23:24:24 +11:00
|
|
|
"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 {
|
2024-01-14 13:51:20 +11:00
|
|
|
types.IListContext
|
2022-02-13 18:26:44 +11:00
|
|
|
CanRebase() bool
|
2022-03-26 22:18:08 +09:00
|
|
|
GetSelectedRef() types.Ref
|
2022-02-13 18:26:44 +11:00
|
|
|
}
|
|
|
|
|
2024-01-14 13:51:20 +11:00
|
|
|
// Not using our ListControllerTrait because our 'selected' item is not a list item
|
|
|
|
// but an attribute on it i.e. the ref of an item.
|
2022-03-26 17:03:30 +11:00
|
|
|
type SwitchToDiffFilesController struct {
|
2022-02-13 18:26:44 +11:00
|
|
|
baseController
|
2024-01-14 13:51:20 +11:00
|
|
|
*ListControllerTrait[types.Ref]
|
2023-03-23 18:47:29 +11:00
|
|
|
c *ControllerCommon
|
2022-12-30 23:24:24 +11:00
|
|
|
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,
|
2022-12-30 23:24:24 +11:00
|
|
|
diffFilesContext *context.CommitFilesContext,
|
2022-03-26 17:03:30 +11:00
|
|
|
) *SwitchToDiffFilesController {
|
|
|
|
return &SwitchToDiffFilesController{
|
2024-01-14 13:51:20 +11:00
|
|
|
baseController: baseController{},
|
|
|
|
ListControllerTrait: NewListControllerTrait[types.Ref](
|
|
|
|
c,
|
|
|
|
context,
|
|
|
|
context.GetSelectedRef,
|
|
|
|
),
|
2023-03-23 18:47:29 +11:00
|
|
|
c: c,
|
2022-02-13 18:26:44 +11:00
|
|
|
context: context,
|
2022-12-30 23:24:24 +11:00
|
|
|
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{
|
|
|
|
{
|
2024-01-14 13:51:20 +11:00
|
|
|
Key: opts.GetKey(opts.Config.Universal.GoInto),
|
|
|
|
Handler: self.withItem(self.enter),
|
|
|
|
GetDisabledReason: self.require(self.singleItemSelected()),
|
|
|
|
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 {
|
2024-01-14 13:51:20 +11:00
|
|
|
return self.withItemGraceful(self.enter)
|
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-12-30 23:24:24 +11:00
|
|
|
func (self *SwitchToDiffFilesController) viewFiles(opts SwitchToCommitFilesContextOpts) error {
|
|
|
|
diffFilesContext := self.diffFilesContext
|
|
|
|
|
2024-01-14 09:50:52 +11:00
|
|
|
diffFilesContext.SetSelection(0)
|
2022-12-30 23:24:24 +11:00
|
|
|
diffFilesContext.SetRef(opts.Ref)
|
|
|
|
diffFilesContext.SetTitleRef(opts.Ref.Description())
|
|
|
|
diffFilesContext.SetCanRebase(opts.CanRebase)
|
|
|
|
diffFilesContext.SetParentContext(opts.Context)
|
|
|
|
diffFilesContext.SetWindowName(opts.Context.GetWindowName())
|
2023-06-26 11:15:47 +10:00
|
|
|
diffFilesContext.ClearSearchString()
|
2023-09-06 18:16:53 -07:00
|
|
|
diffFilesContext.GetView().TitlePrefix = opts.Context.GetView().TitlePrefix
|
2022-12-30 23:24:24 +11:00
|
|
|
|
|
|
|
if err := self.c.Refresh(types.RefreshOptions{
|
|
|
|
Scope: []types.RefreshableView{types.COMMIT_FILES},
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return self.c.PushContext(diffFilesContext)
|
|
|
|
}
|