mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-01-24 05:36:19 +02:00
51fb82d6bf
We want to show an error when the user tries to invoke an action that expects only a single item to be selected. We're using the GetDisabledReason field to enforce this (as well as DisabledReason on menu items). I've created a ListControllerTrait to store some shared convenience functions for this.
71 lines
1.8 KiB
Go
71 lines
1.8 KiB
Go
package controllers
|
|
|
|
import (
|
|
"github.com/jesseduffield/lazygit/pkg/gui/controllers/helpers"
|
|
"github.com/jesseduffield/lazygit/pkg/gui/types"
|
|
)
|
|
|
|
var _ types.IController = &SwitchToSubCommitsController{}
|
|
|
|
type CanSwitchToSubCommits interface {
|
|
types.IListContext
|
|
GetSelectedRef() types.Ref
|
|
ShowBranchHeadsInSubCommits() bool
|
|
}
|
|
|
|
// 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.
|
|
type SwitchToSubCommitsController struct {
|
|
baseController
|
|
*ListControllerTrait[types.Ref]
|
|
c *ControllerCommon
|
|
context CanSwitchToSubCommits
|
|
}
|
|
|
|
func NewSwitchToSubCommitsController(
|
|
c *ControllerCommon,
|
|
context CanSwitchToSubCommits,
|
|
) *SwitchToSubCommitsController {
|
|
return &SwitchToSubCommitsController{
|
|
baseController: baseController{},
|
|
ListControllerTrait: NewListControllerTrait[types.Ref](
|
|
c,
|
|
context,
|
|
context.GetSelectedRef,
|
|
),
|
|
c: c,
|
|
context: context,
|
|
}
|
|
}
|
|
|
|
func (self *SwitchToSubCommitsController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding {
|
|
bindings := []*types.Binding{
|
|
{
|
|
Handler: self.viewCommits,
|
|
GetDisabledReason: self.require(self.singleItemSelected()),
|
|
Key: opts.GetKey(opts.Config.Universal.GoInto),
|
|
Description: self.c.Tr.ViewCommits,
|
|
},
|
|
}
|
|
|
|
return bindings
|
|
}
|
|
|
|
func (self *SwitchToSubCommitsController) GetOnClick() func() error {
|
|
return self.viewCommits
|
|
}
|
|
|
|
func (self *SwitchToSubCommitsController) viewCommits() error {
|
|
ref := self.context.GetSelectedRef()
|
|
if ref == nil {
|
|
return nil
|
|
}
|
|
|
|
return self.c.Helpers().SubCommits.ViewSubCommits(helpers.ViewSubCommitsOpts{
|
|
Ref: ref,
|
|
TitleRef: ref.RefName(),
|
|
Context: self.context,
|
|
ShowBranchHeads: self.context.ShowBranchHeadsInSubCommits(),
|
|
})
|
|
}
|