1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-07-30 03:23:08 +03:00

Enforce single-item selection in various actions

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.
This commit is contained in:
Jesse Duffield
2024-01-14 13:51:20 +11:00
parent 280b4d60f8
commit 51fb82d6bf
45 changed files with 854 additions and 757 deletions

View File

@ -9,46 +9,57 @@ import (
type StashController struct {
baseController
*ListControllerTrait[*models.StashEntry]
c *ControllerCommon
}
var _ types.IController = &StashController{}
func NewStashController(
common *ControllerCommon,
c *ControllerCommon,
) *StashController {
return &StashController{
baseController: baseController{},
c: common,
ListControllerTrait: NewListControllerTrait[*models.StashEntry](
c,
c.Contexts().Stash,
c.Contexts().Stash.GetSelected,
),
c: c,
}
}
func (self *StashController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding {
bindings := []*types.Binding{
{
Key: opts.GetKey(opts.Config.Universal.Select),
Handler: self.checkSelected(self.handleStashApply),
Description: self.c.Tr.Apply,
Key: opts.GetKey(opts.Config.Universal.Select),
Handler: self.withItem(self.handleStashApply),
GetDisabledReason: self.require(self.singleItemSelected()),
Description: self.c.Tr.Apply,
},
{
Key: opts.GetKey(opts.Config.Stash.PopStash),
Handler: self.checkSelected(self.handleStashPop),
Description: self.c.Tr.Pop,
Key: opts.GetKey(opts.Config.Stash.PopStash),
Handler: self.withItem(self.handleStashPop),
GetDisabledReason: self.require(self.singleItemSelected()),
Description: self.c.Tr.Pop,
},
{
Key: opts.GetKey(opts.Config.Universal.Remove),
Handler: self.checkSelected(self.handleStashDrop),
Description: self.c.Tr.Drop,
Key: opts.GetKey(opts.Config.Universal.Remove),
Handler: self.withItem(self.handleStashDrop),
GetDisabledReason: self.require(self.singleItemSelected()),
Description: self.c.Tr.Drop,
},
{
Key: opts.GetKey(opts.Config.Universal.New),
Handler: self.checkSelected(self.handleNewBranchOffStashEntry),
Description: self.c.Tr.NewBranch,
Key: opts.GetKey(opts.Config.Universal.New),
Handler: self.withItem(self.handleNewBranchOffStashEntry),
GetDisabledReason: self.require(self.singleItemSelected()),
Description: self.c.Tr.NewBranch,
},
{
Key: opts.GetKey(opts.Config.Stash.RenameStash),
Handler: self.checkSelected(self.handleRenameStashEntry),
Description: self.c.Tr.RenameStash,
Key: opts.GetKey(opts.Config.Stash.RenameStash),
Handler: self.withItem(self.handleRenameStashEntry),
GetDisabledReason: self.require(self.singleItemSelected()),
Description: self.c.Tr.RenameStash,
},
}
@ -80,21 +91,6 @@ func (self *StashController) GetOnRenderToMain() func() error {
}
}
func (self *StashController) checkSelected(callback func(*models.StashEntry) error) func() error {
return func() error {
item := self.context().GetSelected()
if item == nil {
return nil
}
return callback(item)
}
}
func (self *StashController) Context() types.Context {
return self.context()
}
func (self *StashController) context() *context.StashContext {
return self.c.Contexts().Stash
}