mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-06-23 00:39:13 +02: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:
@ -4,24 +4,29 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/jesseduffield/lazygit/pkg/commands/models"
|
||||
"github.com/jesseduffield/lazygit/pkg/gui/context"
|
||||
"github.com/jesseduffield/lazygit/pkg/gui/types"
|
||||
"github.com/jesseduffield/lazygit/pkg/utils"
|
||||
)
|
||||
|
||||
type GitFlowController struct {
|
||||
baseController
|
||||
*ListControllerTrait[*models.Branch]
|
||||
c *ControllerCommon
|
||||
}
|
||||
|
||||
var _ types.IController = &GitFlowController{}
|
||||
|
||||
func NewGitFlowController(
|
||||
common *ControllerCommon,
|
||||
c *ControllerCommon,
|
||||
) *GitFlowController {
|
||||
return &GitFlowController{
|
||||
baseController: baseController{},
|
||||
c: common,
|
||||
ListControllerTrait: NewListControllerTrait[*models.Branch](
|
||||
c,
|
||||
c.Contexts().Branches,
|
||||
c.Contexts().Branches.GetSelected,
|
||||
),
|
||||
c: c,
|
||||
}
|
||||
}
|
||||
|
||||
@ -29,7 +34,7 @@ func (self *GitFlowController) GetKeybindings(opts types.KeybindingsOpts) []*typ
|
||||
bindings := []*types.Binding{
|
||||
{
|
||||
Key: opts.GetKey(opts.Config.Branches.ViewGitFlowOptions),
|
||||
Handler: self.checkSelected(self.handleCreateGitFlowMenu),
|
||||
Handler: self.withItem(self.handleCreateGitFlowMenu),
|
||||
Description: self.c.Tr.GitFlowOptions,
|
||||
OpensMenu: true,
|
||||
},
|
||||
@ -68,6 +73,7 @@ func (self *GitFlowController) handleCreateGitFlowMenu(branch *models.Branch) er
|
||||
OnPress: func() error {
|
||||
return self.gitFlowFinishBranch(branch.Name)
|
||||
},
|
||||
DisabledReason: self.require(self.singleItemSelected())(),
|
||||
},
|
||||
{
|
||||
Label: "start feature",
|
||||
@ -102,22 +108,3 @@ func (self *GitFlowController) gitFlowFinishBranch(branchName string) error {
|
||||
self.c.LogAction(self.c.Tr.Actions.GitFlowFinish)
|
||||
return self.c.RunSubprocessAndRefresh(cmdObj)
|
||||
}
|
||||
|
||||
func (self *GitFlowController) checkSelected(callback func(*models.Branch) error) func() error {
|
||||
return func() error {
|
||||
node := self.context().GetSelected()
|
||||
if node == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return callback(node)
|
||||
}
|
||||
}
|
||||
|
||||
func (self *GitFlowController) Context() types.Context {
|
||||
return self.context()
|
||||
}
|
||||
|
||||
func (self *GitFlowController) context() *context.BranchesContext {
|
||||
return self.c.Contexts().Branches
|
||||
}
|
||||
|
Reference in New Issue
Block a user