mirror of
https://github.com/jesseduffield/lazygit.git
synced 2026-09-10 07:36:27 -04:00
Constructing a menu item key from a literal character requires
gocui.NewKeyRune('r'), which is a bit noisy. Add a private menuKey helper in
both the controllers and helpers packages so the common case in either reads as
menuKey('r'). Duplicating the one-liner is cheaper than a cross-package import
dependency and avoids forcing every controller file to qualify the call.
The reason for doing this now is that we are going to change MenuItem.Key to a
slice of keys later in the branch, which means we'd have to add `[]gocui.Key{`
at each call site, making them even more noisy. With the menuKey helper we can
just change its signature and leave all clients unchanged.
115 lines
2.8 KiB
Go
115 lines
2.8 KiB
Go
package controllers
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands/models"
|
|
"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(
|
|
c *ControllerCommon,
|
|
) *GitFlowController {
|
|
return &GitFlowController{
|
|
baseController: baseController{},
|
|
ListControllerTrait: NewListControllerTrait(
|
|
c,
|
|
c.Contexts().Branches,
|
|
c.Contexts().Branches.GetSelected,
|
|
c.Contexts().Branches.GetSelectedItems,
|
|
),
|
|
c: c,
|
|
}
|
|
}
|
|
|
|
func (self *GitFlowController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding {
|
|
bindings := []*types.Binding{
|
|
{
|
|
Key: opts.GetKey(opts.Config.Branches.ViewGitFlowOptions),
|
|
Handler: self.withItem(self.handleCreateGitFlowMenu),
|
|
Description: self.c.Tr.GitFlowOptions,
|
|
OpensMenu: true,
|
|
},
|
|
}
|
|
|
|
return bindings
|
|
}
|
|
|
|
func (self *GitFlowController) handleCreateGitFlowMenu(branch *models.Branch) error {
|
|
if !self.c.Git().Flow.GitFlowEnabled() {
|
|
return errors.New("You need to install git-flow and enable it in this repo to use git-flow features")
|
|
}
|
|
|
|
startHandler := func(branchType string) func() error {
|
|
return func() error {
|
|
title := utils.ResolvePlaceholderString(self.c.Tr.NewGitFlowBranchPrompt, map[string]string{"branchType": branchType})
|
|
|
|
self.c.Prompt(types.PromptOpts{
|
|
Title: title,
|
|
HandleConfirm: func(name string) error {
|
|
self.c.LogAction(self.c.Tr.Actions.GitFlowStart)
|
|
return self.c.RunSubprocessAndRefresh(
|
|
self.c.Git().Flow.StartCmdObj(branchType, name),
|
|
)
|
|
},
|
|
})
|
|
|
|
return nil
|
|
}
|
|
}
|
|
|
|
return self.c.Menu(types.CreateMenuOptions{
|
|
Title: "git flow",
|
|
Items: []*types.MenuItem{
|
|
{
|
|
// not localising here because it's one to one with the actual git flow commands
|
|
Label: fmt.Sprintf("finish branch '%s'", branch.Name),
|
|
OnPress: func() error {
|
|
return self.gitFlowFinishBranch(branch.Name)
|
|
},
|
|
DisabledReason: self.require(self.singleItemSelected())(),
|
|
},
|
|
{
|
|
Label: "start feature",
|
|
OnPress: startHandler("feature"),
|
|
Key: menuKey('f'),
|
|
},
|
|
{
|
|
Label: "start hotfix",
|
|
OnPress: startHandler("hotfix"),
|
|
Key: menuKey('h'),
|
|
},
|
|
{
|
|
Label: "start bugfix",
|
|
OnPress: startHandler("bugfix"),
|
|
Key: menuKey('b'),
|
|
},
|
|
{
|
|
Label: "start release",
|
|
OnPress: startHandler("release"),
|
|
Key: menuKey('r'),
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
func (self *GitFlowController) gitFlowFinishBranch(branchName string) error {
|
|
cmdObj, err := self.c.Git().Flow.FinishCmdObj(branchName)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
self.c.LogAction(self.c.Tr.Actions.GitFlowFinish)
|
|
return self.c.RunSubprocessAndRefresh(cmdObj)
|
|
}
|