1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-06-17 00:18:05 +02:00

refactor keybindings

This commit is contained in:
Jesse Duffield
2022-02-05 10:31:07 +11:00
parent 2db4636815
commit 226985bf76
19 changed files with 215 additions and 201 deletions

View File

@ -1,6 +1,8 @@
package context package context
import "github.com/jesseduffield/lazygit/pkg/gui/types" import (
"github.com/jesseduffield/lazygit/pkg/gui/types"
)
type BaseContext struct { type BaseContext struct {
kind types.ContextKind kind types.ContextKind
@ -9,9 +11,14 @@ type BaseContext struct {
windowName string windowName string
onGetOptionsMap func() map[string]string onGetOptionsMap func() map[string]string
keybindingsFns []types.KeybindingsFn
keybindings []*types.Binding
*ParentContextMgr *ParentContextMgr
} }
var _ types.IBaseContext = &BaseContext{}
type NewBaseContextOpts struct { type NewBaseContextOpts struct {
Kind types.ContextKind Kind types.ContextKind
Key types.ContextKey Key types.ContextKey
@ -58,3 +65,18 @@ func (self *BaseContext) GetKind() types.ContextKind {
func (self *BaseContext) GetKey() types.ContextKey { func (self *BaseContext) GetKey() types.ContextKey {
return self.key return self.key
} }
func (self *BaseContext) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding {
bindings := []*types.Binding{}
for i := range self.keybindingsFns {
// the first binding in the bindings array takes precedence but we want the
// last keybindingsFn to take precedence to we add them in reverse
bindings = append(bindings, self.keybindingsFns[len(self.keybindingsFns)-1-i](opts)...)
}
return bindings
}
func (self *BaseContext) AddKeybindingsFn(fn types.KeybindingsFn) {
self.keybindingsFns = append(self.keybindingsFns, fn)
}

View File

@ -55,6 +55,8 @@ func NewCommitFilesContext(
c: c, c: c,
} }
baseContext.AddKeybindingsFn(listContextTrait.keybindings)
self.BaseContext = baseContext self.BaseContext = baseContext
self.ListContextTrait = listContextTrait self.ListContextTrait = listContextTrait
self.CommitFileTreeViewModel = viewModel self.CommitFileTreeViewModel = viewModel

View File

@ -4,7 +4,6 @@ import (
"fmt" "fmt"
"github.com/jesseduffield/gocui" "github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/config"
"github.com/jesseduffield/lazygit/pkg/gui/types" "github.com/jesseduffield/lazygit/pkg/gui/types"
"github.com/jesseduffield/lazygit/pkg/utils" "github.com/jesseduffield/lazygit/pkg/utils"
) )
@ -182,32 +181,28 @@ func (self *ListContextTrait) HandleRenderToMain() error {
return nil return nil
} }
func (self *ListContextTrait) Keybindings( func (self *ListContextTrait) keybindings(opts types.KeybindingsOpts) []*types.Binding {
getKey func(key string) interface{},
config config.KeybindingConfig,
guards types.KeybindingGuards,
) []*types.Binding {
return []*types.Binding{ return []*types.Binding{
{Tag: "navigation", Key: getKey(config.Universal.PrevItemAlt), Modifier: gocui.ModNone, Handler: self.HandlePrevLine}, {Tag: "navigation", Key: opts.GetKey(opts.Config.Universal.PrevItemAlt), Modifier: gocui.ModNone, Handler: self.HandlePrevLine},
{Tag: "navigation", Key: getKey(config.Universal.PrevItem), Modifier: gocui.ModNone, Handler: self.HandlePrevLine}, {Tag: "navigation", Key: opts.GetKey(opts.Config.Universal.PrevItem), Modifier: gocui.ModNone, Handler: self.HandlePrevLine},
{Tag: "navigation", Key: gocui.MouseWheelUp, Modifier: gocui.ModNone, Handler: self.HandlePrevLine}, {Tag: "navigation", Key: gocui.MouseWheelUp, Modifier: gocui.ModNone, Handler: self.HandlePrevLine},
{Tag: "navigation", Key: getKey(config.Universal.NextItemAlt), Modifier: gocui.ModNone, Handler: self.HandleNextLine}, {Tag: "navigation", Key: opts.GetKey(opts.Config.Universal.NextItemAlt), Modifier: gocui.ModNone, Handler: self.HandleNextLine},
{Tag: "navigation", Key: getKey(config.Universal.NextItem), Modifier: gocui.ModNone, Handler: self.HandleNextLine}, {Tag: "navigation", Key: opts.GetKey(opts.Config.Universal.NextItem), Modifier: gocui.ModNone, Handler: self.HandleNextLine},
{Tag: "navigation", Key: getKey(config.Universal.PrevPage), Modifier: gocui.ModNone, Handler: self.HandlePrevPage, Description: self.c.Tr.LcPrevPage}, {Tag: "navigation", Key: opts.GetKey(opts.Config.Universal.PrevPage), Modifier: gocui.ModNone, Handler: self.HandlePrevPage, Description: self.c.Tr.LcPrevPage},
{Tag: "navigation", Key: getKey(config.Universal.NextPage), Modifier: gocui.ModNone, Handler: self.HandleNextPage, Description: self.c.Tr.LcNextPage}, {Tag: "navigation", Key: opts.GetKey(opts.Config.Universal.NextPage), Modifier: gocui.ModNone, Handler: self.HandleNextPage, Description: self.c.Tr.LcNextPage},
{Tag: "navigation", Key: getKey(config.Universal.GotoTop), Modifier: gocui.ModNone, Handler: self.HandleGotoTop, Description: self.c.Tr.LcGotoTop}, {Tag: "navigation", Key: opts.GetKey(opts.Config.Universal.GotoTop), Modifier: gocui.ModNone, Handler: self.HandleGotoTop, Description: self.c.Tr.LcGotoTop},
{Key: gocui.MouseLeft, Modifier: gocui.ModNone, Handler: func() error { return self.HandleClick(nil) }}, {Key: gocui.MouseLeft, Modifier: gocui.ModNone, Handler: func() error { return self.HandleClick(nil) }},
{Tag: "navigation", Key: gocui.MouseWheelDown, Modifier: gocui.ModNone, Handler: self.HandleNextLine}, {Tag: "navigation", Key: gocui.MouseWheelDown, Modifier: gocui.ModNone, Handler: self.HandleNextLine},
{Tag: "navigation", Key: getKey(config.Universal.ScrollLeft), Modifier: gocui.ModNone, Handler: self.HandleScrollLeft}, {Tag: "navigation", Key: opts.GetKey(opts.Config.Universal.ScrollLeft), Modifier: gocui.ModNone, Handler: self.HandleScrollLeft},
{Tag: "navigation", Key: getKey(config.Universal.ScrollRight), Modifier: gocui.ModNone, Handler: self.HandleScrollRight}, {Tag: "navigation", Key: opts.GetKey(opts.Config.Universal.ScrollRight), Modifier: gocui.ModNone, Handler: self.HandleScrollRight},
{ {
Key: getKey(config.Universal.StartSearch), Key: opts.GetKey(opts.Config.Universal.StartSearch),
Handler: func() error { self.c.OpenSearch(); return nil }, Handler: func() error { self.c.OpenSearch(); return nil },
Description: self.c.Tr.LcStartSearch, Description: self.c.Tr.LcStartSearch,
Tag: "navigation", Tag: "navigation",
}, },
{ {
Key: getKey(config.Universal.GotoBottom), Key: opts.GetKey(opts.Config.Universal.GotoBottom),
Description: self.c.Tr.LcGotoBottom, Description: self.c.Tr.LcGotoBottom,
Handler: self.HandleGotoBottom, Handler: self.HandleGotoBottom,
Tag: "navigation", Tag: "navigation",

View File

@ -55,6 +55,8 @@ func NewTagsContext(
c: c, c: c,
} }
baseContext.AddKeybindingsFn(listContextTrait.keybindings)
self.BaseContext = baseContext self.BaseContext = baseContext
self.ListContextTrait = listContextTrait self.ListContextTrait = listContextTrait
self.TagsViewModel = list self.TagsViewModel = list

View File

@ -55,6 +55,8 @@ func NewWorkingTreeContext(
c: c, c: c,
} }
baseContext.AddKeybindingsFn(listContextTrait.keybindings)
self.BaseContext = baseContext self.BaseContext = baseContext
self.ListContextTrait = listContextTrait self.ListContextTrait = listContextTrait
self.FileTreeViewModel = viewModel self.FileTreeViewModel = viewModel

View File

@ -7,7 +7,6 @@ import (
"github.com/jesseduffield/lazygit/pkg/commands" "github.com/jesseduffield/lazygit/pkg/commands"
"github.com/jesseduffield/lazygit/pkg/commands/git_commands" "github.com/jesseduffield/lazygit/pkg/commands/git_commands"
"github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/config"
"github.com/jesseduffield/lazygit/pkg/gui/types" "github.com/jesseduffield/lazygit/pkg/gui/types"
) )
@ -43,11 +42,11 @@ func NewBisectController(
} }
} }
func (self *BisectController) Keybindings(getKey func(key string) interface{}, config config.KeybindingConfig, guards types.KeybindingGuards) []*types.Binding { func (self *BisectController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding {
bindings := []*types.Binding{ bindings := []*types.Binding{
{ {
Key: getKey(config.Commits.ViewBisectOptions), Key: opts.GetKey(opts.Config.Commits.ViewBisectOptions),
Handler: guards.OutsideFilterMode(self.checkSelected(self.openMenu)), Handler: opts.Guards.OutsideFilterMode(self.checkSelected(self.openMenu)),
Description: self.c.Tr.LcViewBisectOptions, Description: self.c.Tr.LcViewBisectOptions,
OpensMenu: true, OpensMenu: true,
}, },

View File

@ -87,10 +87,10 @@ func NewFilesController(
} }
} }
func (self *FilesController) Keybindings(getKey func(key string) interface{}, config config.KeybindingConfig, guards types.KeybindingGuards) []*types.Binding { func (self *FilesController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding {
bindings := []*types.Binding{ return []*types.Binding{
{ {
Key: getKey(config.Universal.Select), Key: opts.GetKey(opts.Config.Universal.Select),
Handler: self.checkSelectedFileNode(self.press), Handler: self.checkSelectedFileNode(self.press),
Description: self.c.Tr.LcToggleStaged, Description: self.c.Tr.LcToggleStaged,
}, },
@ -99,103 +99,101 @@ func (self *FilesController) Keybindings(getKey func(key string) interface{}, co
Handler: func() error { return self.context.HandleClick(self.checkSelectedFileNode(self.press)) }, Handler: func() error { return self.context.HandleClick(self.checkSelectedFileNode(self.press)) },
}, },
{ {
Key: getKey("<c-b>"), // TODO: softcode Key: opts.GetKey("<c-b>"), // TODO: softcode
Handler: self.handleStatusFilterPressed, Handler: self.handleStatusFilterPressed,
Description: self.c.Tr.LcFileFilter, Description: self.c.Tr.LcFileFilter,
}, },
{ {
Key: getKey(config.Files.CommitChanges), Key: opts.GetKey(opts.Config.Files.CommitChanges),
Handler: self.HandleCommitPress, Handler: self.HandleCommitPress,
Description: self.c.Tr.CommitChanges, Description: self.c.Tr.CommitChanges,
}, },
{ {
Key: getKey(config.Files.CommitChangesWithoutHook), Key: opts.GetKey(opts.Config.Files.CommitChangesWithoutHook),
Handler: self.HandleWIPCommitPress, Handler: self.HandleWIPCommitPress,
Description: self.c.Tr.LcCommitChangesWithoutHook, Description: self.c.Tr.LcCommitChangesWithoutHook,
}, },
{ {
Key: getKey(config.Files.AmendLastCommit), Key: opts.GetKey(opts.Config.Files.AmendLastCommit),
Handler: self.handleAmendCommitPress, Handler: self.handleAmendCommitPress,
Description: self.c.Tr.AmendLastCommit, Description: self.c.Tr.AmendLastCommit,
}, },
{ {
Key: getKey(config.Files.CommitChangesWithEditor), Key: opts.GetKey(opts.Config.Files.CommitChangesWithEditor),
Handler: self.HandleCommitEditorPress, Handler: self.HandleCommitEditorPress,
Description: self.c.Tr.CommitChangesWithEditor, Description: self.c.Tr.CommitChangesWithEditor,
}, },
{ {
Key: getKey(config.Universal.Edit), Key: opts.GetKey(opts.Config.Universal.Edit),
Handler: self.checkSelectedFileNode(self.edit), Handler: self.checkSelectedFileNode(self.edit),
Description: self.c.Tr.LcEditFile, Description: self.c.Tr.LcEditFile,
}, },
{ {
Key: getKey(config.Universal.OpenFile), Key: opts.GetKey(opts.Config.Universal.OpenFile),
Handler: self.Open, Handler: self.Open,
Description: self.c.Tr.LcOpenFile, Description: self.c.Tr.LcOpenFile,
}, },
{ {
Key: getKey(config.Files.IgnoreFile), Key: opts.GetKey(opts.Config.Files.IgnoreFile),
Handler: self.checkSelectedFileNode(self.ignore), Handler: self.checkSelectedFileNode(self.ignore),
Description: self.c.Tr.LcIgnoreFile, Description: self.c.Tr.LcIgnoreFile,
}, },
{ {
Key: getKey(config.Universal.Remove), Key: opts.GetKey(opts.Config.Universal.Remove),
Handler: self.checkSelectedFileNode(self.remove), Handler: self.checkSelectedFileNode(self.remove),
Description: self.c.Tr.LcViewDiscardOptions, Description: self.c.Tr.LcViewDiscardOptions,
OpensMenu: true, OpensMenu: true,
}, },
{ {
Key: getKey(config.Files.RefreshFiles), Key: opts.GetKey(opts.Config.Files.RefreshFiles),
Handler: self.refresh, Handler: self.refresh,
Description: self.c.Tr.LcRefreshFiles, Description: self.c.Tr.LcRefreshFiles,
}, },
{ {
Key: getKey(config.Files.StashAllChanges), Key: opts.GetKey(opts.Config.Files.StashAllChanges),
Handler: self.stash, Handler: self.stash,
Description: self.c.Tr.LcStashAllChanges, Description: self.c.Tr.LcStashAllChanges,
}, },
{ {
Key: getKey(config.Files.ViewStashOptions), Key: opts.GetKey(opts.Config.Files.ViewStashOptions),
Handler: self.createStashMenu, Handler: self.createStashMenu,
Description: self.c.Tr.LcViewStashOptions, Description: self.c.Tr.LcViewStashOptions,
OpensMenu: true, OpensMenu: true,
}, },
{ {
Key: getKey(config.Files.ToggleStagedAll), Key: opts.GetKey(opts.Config.Files.ToggleStagedAll),
Handler: self.stageAll, Handler: self.stageAll,
Description: self.c.Tr.LcToggleStagedAll, Description: self.c.Tr.LcToggleStagedAll,
}, },
{ {
Key: getKey(config.Universal.GoInto), Key: opts.GetKey(opts.Config.Universal.GoInto),
Handler: self.enter, Handler: self.enter,
Description: self.c.Tr.FileEnter, Description: self.c.Tr.FileEnter,
}, },
{ {
ViewName: "", ViewName: "",
Key: getKey(config.Universal.ExecuteCustomCommand), Key: opts.GetKey(opts.Config.Universal.ExecuteCustomCommand),
Handler: self.handleCustomCommand, Handler: self.handleCustomCommand,
Description: self.c.Tr.LcExecuteCustomCommand, Description: self.c.Tr.LcExecuteCustomCommand,
}, },
{ {
Key: getKey(config.Commits.ViewResetOptions), Key: opts.GetKey(opts.Config.Commits.ViewResetOptions),
Handler: self.createResetMenu, Handler: self.createResetMenu,
Description: self.c.Tr.LcViewResetToUpstreamOptions, Description: self.c.Tr.LcViewResetToUpstreamOptions,
OpensMenu: true, OpensMenu: true,
}, },
// here // here
{ {
Key: getKey(config.Files.ToggleTreeView), Key: opts.GetKey(opts.Config.Files.ToggleTreeView),
Handler: self.toggleTreeView, Handler: self.toggleTreeView,
Description: self.c.Tr.LcToggleTreeView, Description: self.c.Tr.LcToggleTreeView,
}, },
{ {
Key: getKey(config.Files.OpenMergeTool), Key: opts.GetKey(opts.Config.Files.OpenMergeTool),
Handler: self.OpenMergeTool, Handler: self.OpenMergeTool,
Description: self.c.Tr.LcOpenMergeTool, Description: self.c.Tr.LcOpenMergeTool,
}, },
} }
return append(bindings, self.context.Keybindings(getKey, config, guards)...)
} }
func (self *FilesController) press(node *filetree.FileNode) error { func (self *FilesController) press(node *filetree.FileNode) error {

View File

@ -8,7 +8,6 @@ import (
"github.com/jesseduffield/lazygit/pkg/commands/hosting_service" "github.com/jesseduffield/lazygit/pkg/commands/hosting_service"
"github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/commands/oscommands" "github.com/jesseduffield/lazygit/pkg/commands/oscommands"
"github.com/jesseduffield/lazygit/pkg/config"
"github.com/jesseduffield/lazygit/pkg/gui/types" "github.com/jesseduffield/lazygit/pkg/gui/types"
"github.com/jesseduffield/lazygit/pkg/utils" "github.com/jesseduffield/lazygit/pkg/utils"
) )
@ -91,108 +90,104 @@ func NewLocalCommitsController(
} }
} }
func (self *LocalCommitsController) Keybindings( func (self *LocalCommitsController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding {
getKey func(key string) interface{},
config config.KeybindingConfig,
guards types.KeybindingGuards,
) []*types.Binding {
outsideFilterModeBindings := []*types.Binding{ outsideFilterModeBindings := []*types.Binding{
{ {
Key: getKey(config.Commits.SquashDown), Key: opts.GetKey(opts.Config.Commits.SquashDown),
Handler: self.squashDown, Handler: self.squashDown,
Description: self.c.Tr.LcSquashDown, Description: self.c.Tr.LcSquashDown,
}, },
{ {
Key: getKey(config.Commits.MarkCommitAsFixup), Key: opts.GetKey(opts.Config.Commits.MarkCommitAsFixup),
Handler: self.fixup, Handler: self.fixup,
Description: self.c.Tr.LcFixupCommit, Description: self.c.Tr.LcFixupCommit,
}, },
{ {
Key: getKey(config.Commits.RenameCommit), Key: opts.GetKey(opts.Config.Commits.RenameCommit),
Handler: self.checkSelected(self.reword), Handler: self.checkSelected(self.reword),
Description: self.c.Tr.LcRewordCommit, Description: self.c.Tr.LcRewordCommit,
}, },
{ {
Key: getKey(config.Commits.RenameCommitWithEditor), Key: opts.GetKey(opts.Config.Commits.RenameCommitWithEditor),
Handler: self.rewordEditor, Handler: self.rewordEditor,
Description: self.c.Tr.LcRenameCommitEditor, Description: self.c.Tr.LcRenameCommitEditor,
}, },
{ {
Key: getKey(config.Universal.Remove), Key: opts.GetKey(opts.Config.Universal.Remove),
Handler: self.drop, Handler: self.drop,
Description: self.c.Tr.LcDeleteCommit, Description: self.c.Tr.LcDeleteCommit,
}, },
{ {
Key: getKey(config.Universal.Edit), Key: opts.GetKey(opts.Config.Universal.Edit),
Handler: self.edit, Handler: self.edit,
Description: self.c.Tr.LcEditCommit, Description: self.c.Tr.LcEditCommit,
}, },
{ {
Key: getKey(config.Commits.PickCommit), Key: opts.GetKey(opts.Config.Commits.PickCommit),
Handler: self.pick, Handler: self.pick,
Description: self.c.Tr.LcPickCommit, Description: self.c.Tr.LcPickCommit,
}, },
{ {
Key: getKey(config.Commits.CreateFixupCommit), Key: opts.GetKey(opts.Config.Commits.CreateFixupCommit),
Handler: self.checkSelected(self.handleCreateFixupCommit), Handler: self.checkSelected(self.handleCreateFixupCommit),
Description: self.c.Tr.LcCreateFixupCommit, Description: self.c.Tr.LcCreateFixupCommit,
}, },
{ {
Key: getKey(config.Commits.SquashAboveCommits), Key: opts.GetKey(opts.Config.Commits.SquashAboveCommits),
Handler: self.checkSelected(self.handleSquashAllAboveFixupCommits), Handler: self.checkSelected(self.handleSquashAllAboveFixupCommits),
Description: self.c.Tr.LcSquashAboveCommits, Description: self.c.Tr.LcSquashAboveCommits,
}, },
{ {
Key: getKey(config.Commits.MoveDownCommit), Key: opts.GetKey(opts.Config.Commits.MoveDownCommit),
Handler: self.handleCommitMoveDown, Handler: self.handleCommitMoveDown,
Description: self.c.Tr.LcMoveDownCommit, Description: self.c.Tr.LcMoveDownCommit,
}, },
{ {
Key: getKey(config.Commits.MoveUpCommit), Key: opts.GetKey(opts.Config.Commits.MoveUpCommit),
Handler: self.handleCommitMoveUp, Handler: self.handleCommitMoveUp,
Description: self.c.Tr.LcMoveUpCommit, Description: self.c.Tr.LcMoveUpCommit,
}, },
{ {
Key: getKey(config.Commits.AmendToCommit), Key: opts.GetKey(opts.Config.Commits.AmendToCommit),
Handler: self.handleCommitAmendTo, Handler: self.handleCommitAmendTo,
Description: self.c.Tr.LcAmendToCommit, Description: self.c.Tr.LcAmendToCommit,
}, },
{ {
Key: getKey(config.Commits.RevertCommit), Key: opts.GetKey(opts.Config.Commits.RevertCommit),
Handler: self.checkSelected(self.handleCommitRevert), Handler: self.checkSelected(self.handleCommitRevert),
Description: self.c.Tr.LcRevertCommit, Description: self.c.Tr.LcRevertCommit,
}, },
{ {
Key: getKey(config.Universal.New), Key: opts.GetKey(opts.Config.Universal.New),
Modifier: gocui.ModNone, Modifier: gocui.ModNone,
Handler: self.checkSelected(self.newBranch), Handler: self.checkSelected(self.newBranch),
Description: self.c.Tr.LcCreateNewBranchFromCommit, Description: self.c.Tr.LcCreateNewBranchFromCommit,
}, },
{ {
Key: getKey(config.Commits.CherryPickCopy), Key: opts.GetKey(opts.Config.Commits.CherryPickCopy),
Handler: self.checkSelected(self.copy), Handler: self.checkSelected(self.copy),
Description: self.c.Tr.LcCherryPickCopy, Description: self.c.Tr.LcCherryPickCopy,
}, },
{ {
Key: getKey(config.Commits.CherryPickCopyRange), Key: opts.GetKey(opts.Config.Commits.CherryPickCopyRange),
Handler: self.checkSelected(self.copyRange), Handler: self.checkSelected(self.copyRange),
Description: self.c.Tr.LcCherryPickCopyRange, Description: self.c.Tr.LcCherryPickCopyRange,
}, },
{ {
Key: getKey(config.Commits.PasteCommits), Key: opts.GetKey(opts.Config.Commits.PasteCommits),
Handler: guards.OutsideFilterMode(self.paste), Handler: opts.Guards.OutsideFilterMode(self.paste),
Description: self.c.Tr.LcPasteCommits, Description: self.c.Tr.LcPasteCommits,
}, },
// overriding these navigation keybindings because we might need to load // overriding these navigation keybindings because we might need to load
// more commits on demand // more commits on demand
{ {
Key: getKey(config.Universal.StartSearch), Key: opts.GetKey(opts.Config.Universal.StartSearch),
Handler: self.openSearch, Handler: self.openSearch,
Description: self.c.Tr.LcStartSearch, Description: self.c.Tr.LcStartSearch,
Tag: "navigation", Tag: "navigation",
}, },
{ {
Key: getKey(config.Universal.GotoBottom), Key: opts.GetKey(opts.Config.Universal.GotoBottom),
Handler: self.gotoBottom, Handler: self.gotoBottom,
Description: self.c.Tr.LcGotoBottom, Description: self.c.Tr.LcGotoBottom,
Tag: "navigation", Tag: "navigation",
@ -204,49 +199,49 @@ func (self *LocalCommitsController) Keybindings(
} }
for _, binding := range outsideFilterModeBindings { for _, binding := range outsideFilterModeBindings {
binding.Handler = guards.OutsideFilterMode(binding.Handler) binding.Handler = opts.Guards.OutsideFilterMode(binding.Handler)
} }
bindings := append(outsideFilterModeBindings, []*types.Binding{ bindings := append(outsideFilterModeBindings, []*types.Binding{
{ {
Key: getKey(config.Commits.OpenLogMenu), Key: opts.GetKey(opts.Config.Commits.OpenLogMenu),
Handler: self.handleOpenLogMenu, Handler: self.handleOpenLogMenu,
Description: self.c.Tr.LcOpenLogMenu, Description: self.c.Tr.LcOpenLogMenu,
OpensMenu: true, OpensMenu: true,
}, },
{ {
Key: getKey(config.Commits.ViewResetOptions), Key: opts.GetKey(opts.Config.Commits.ViewResetOptions),
Handler: self.checkSelected(self.handleCreateCommitResetMenu), Handler: self.checkSelected(self.handleCreateCommitResetMenu),
Description: self.c.Tr.LcResetToThisCommit, Description: self.c.Tr.LcResetToThisCommit,
}, },
{ {
Key: getKey(config.Universal.GoInto), Key: opts.GetKey(opts.Config.Universal.GoInto),
Handler: self.checkSelected(self.enter), Handler: self.checkSelected(self.enter),
Description: self.c.Tr.LcViewCommitFiles, Description: self.c.Tr.LcViewCommitFiles,
}, },
{ {
Key: getKey(config.Commits.CheckoutCommit), Key: opts.GetKey(opts.Config.Commits.CheckoutCommit),
Handler: self.checkSelected(self.handleCheckoutCommit), Handler: self.checkSelected(self.handleCheckoutCommit),
Description: self.c.Tr.LcCheckoutCommit, Description: self.c.Tr.LcCheckoutCommit,
}, },
{ {
Key: getKey(config.Commits.TagCommit), Key: opts.GetKey(opts.Config.Commits.TagCommit),
Handler: self.checkSelected(self.handleTagCommit), Handler: self.checkSelected(self.handleTagCommit),
Description: self.c.Tr.LcTagCommit, Description: self.c.Tr.LcTagCommit,
}, },
{ {
Key: getKey(config.Commits.CopyCommitMessageToClipboard), Key: opts.GetKey(opts.Config.Commits.CopyCommitMessageToClipboard),
Handler: self.checkSelected(self.handleCopySelectedCommitMessageToClipboard), Handler: self.checkSelected(self.handleCopySelectedCommitMessageToClipboard),
Description: self.c.Tr.LcCopyCommitMessageToClipboard, Description: self.c.Tr.LcCopyCommitMessageToClipboard,
}, },
{ {
Key: getKey(config.Commits.OpenInBrowser), Key: opts.GetKey(opts.Config.Commits.OpenInBrowser),
Handler: self.checkSelected(self.handleOpenCommitInBrowser), Handler: self.checkSelected(self.handleOpenCommitInBrowser),
Description: self.c.Tr.LcOpenCommitInBrowser, Description: self.c.Tr.LcOpenCommitInBrowser,
}, },
}...) }...)
return append(bindings, self.context.Keybindings(getKey, config, guards)...) return bindings
} }
func (self *LocalCommitsController) squashDown() error { func (self *LocalCommitsController) squashDown() error {

View File

@ -2,7 +2,6 @@ package controllers
import ( import (
"github.com/jesseduffield/gocui" "github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/config"
"github.com/jesseduffield/lazygit/pkg/gui/types" "github.com/jesseduffield/lazygit/pkg/gui/types"
) )
@ -27,18 +26,18 @@ func NewMenuController(
} }
} }
func (self *MenuController) Keybindings(getKey func(key string) interface{}, config config.KeybindingConfig, guards types.KeybindingGuards) []*types.Binding { func (self *MenuController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding {
bindings := []*types.Binding{ bindings := []*types.Binding{
{ {
Key: getKey(config.Universal.Select), Key: opts.GetKey(opts.Config.Universal.Select),
Handler: self.press, Handler: self.press,
}, },
{ {
Key: getKey(config.Universal.Confirm), Key: opts.GetKey(opts.Config.Universal.Confirm),
Handler: self.press, Handler: self.press,
}, },
{ {
Key: getKey(config.Universal.ConfirmAlt1), Key: opts.GetKey(opts.Config.Universal.ConfirmAlt1),
Handler: self.press, Handler: self.press,
}, },
{ {
@ -47,7 +46,7 @@ func (self *MenuController) Keybindings(getKey func(key string) interface{}, con
}, },
} }
return append(bindings, self.context.Keybindings(getKey, config, guards)...) return bindings
} }
func (self *MenuController) press() error { func (self *MenuController) press() error {

View File

@ -4,7 +4,6 @@ import (
"github.com/jesseduffield/gocui" "github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/commands" "github.com/jesseduffield/lazygit/pkg/commands"
"github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/config"
"github.com/jesseduffield/lazygit/pkg/gui/context" "github.com/jesseduffield/lazygit/pkg/gui/context"
"github.com/jesseduffield/lazygit/pkg/gui/types" "github.com/jesseduffield/lazygit/pkg/gui/types"
"github.com/jesseduffield/lazygit/pkg/utils" "github.com/jesseduffield/lazygit/pkg/utils"
@ -40,10 +39,10 @@ func NewRemotesController(
} }
} }
func (self *RemotesController) Keybindings(getKey func(key string) interface{}, config config.KeybindingConfig, guards types.KeybindingGuards) []*types.Binding { func (self *RemotesController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding {
bindings := []*types.Binding{ bindings := []*types.Binding{
{ {
Key: getKey(config.Universal.GoInto), Key: opts.GetKey(opts.Config.Universal.GoInto),
Handler: self.checkSelected(self.enter), Handler: self.checkSelected(self.enter),
}, },
{ {
@ -51,28 +50,28 @@ func (self *RemotesController) Keybindings(getKey func(key string) interface{},
Handler: func() error { return self.context.HandleClick(self.checkSelected(self.enter)) }, Handler: func() error { return self.context.HandleClick(self.checkSelected(self.enter)) },
}, },
{ {
Key: getKey(config.Branches.FetchRemote), Key: opts.GetKey(opts.Config.Branches.FetchRemote),
Handler: self.checkSelected(self.fetch), Handler: self.checkSelected(self.fetch),
Description: self.c.Tr.LcFetchRemote, Description: self.c.Tr.LcFetchRemote,
}, },
{ {
Key: getKey(config.Universal.New), Key: opts.GetKey(opts.Config.Universal.New),
Handler: self.add, Handler: self.add,
Description: self.c.Tr.LcAddNewRemote, Description: self.c.Tr.LcAddNewRemote,
}, },
{ {
Key: getKey(config.Universal.Remove), Key: opts.GetKey(opts.Config.Universal.Remove),
Handler: self.checkSelected(self.remove), Handler: self.checkSelected(self.remove),
Description: self.c.Tr.LcRemoveRemote, Description: self.c.Tr.LcRemoveRemote,
}, },
{ {
Key: getKey(config.Universal.Edit), Key: opts.GetKey(opts.Config.Universal.Edit),
Handler: self.checkSelected(self.edit), Handler: self.checkSelected(self.edit),
Description: self.c.Tr.LcEditRemote, Description: self.c.Tr.LcEditRemote,
}, },
} }
return append(bindings, self.context.Keybindings(getKey, config, guards)...) return bindings
} }
func (self *RemotesController) enter(remote *models.Remote) error { func (self *RemotesController) enter(remote *models.Remote) error {

View File

@ -8,7 +8,6 @@ import (
"github.com/jesseduffield/gocui" "github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/commands" "github.com/jesseduffield/lazygit/pkg/commands"
"github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/config"
"github.com/jesseduffield/lazygit/pkg/gui/style" "github.com/jesseduffield/lazygit/pkg/gui/style"
"github.com/jesseduffield/lazygit/pkg/gui/types" "github.com/jesseduffield/lazygit/pkg/gui/types"
) )
@ -40,40 +39,40 @@ func NewSubmodulesController(
} }
} }
func (self *SubmodulesController) Keybindings(getKey func(key string) interface{}, config config.KeybindingConfig, guards types.KeybindingGuards) []*types.Binding { func (self *SubmodulesController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding {
bindings := []*types.Binding{ return []*types.Binding{
{ {
Key: getKey(config.Universal.GoInto), Key: opts.GetKey(opts.Config.Universal.GoInto),
Handler: self.checkSelected(self.enter), Handler: self.checkSelected(self.enter),
Description: self.c.Tr.LcEnterSubmodule, Description: self.c.Tr.LcEnterSubmodule,
}, },
{ {
Key: getKey(config.Universal.Remove), Key: opts.GetKey(opts.Config.Universal.Remove),
Handler: self.checkSelected(self.remove), Handler: self.checkSelected(self.remove),
Description: self.c.Tr.LcRemoveSubmodule, Description: self.c.Tr.LcRemoveSubmodule,
}, },
{ {
Key: getKey(config.Submodules.Update), Key: opts.GetKey(opts.Config.Submodules.Update),
Handler: self.checkSelected(self.update), Handler: self.checkSelected(self.update),
Description: self.c.Tr.LcSubmoduleUpdate, Description: self.c.Tr.LcSubmoduleUpdate,
}, },
{ {
Key: getKey(config.Universal.New), Key: opts.GetKey(opts.Config.Universal.New),
Handler: self.add, Handler: self.add,
Description: self.c.Tr.LcAddSubmodule, Description: self.c.Tr.LcAddSubmodule,
}, },
{ {
Key: getKey(config.Universal.Edit), Key: opts.GetKey(opts.Config.Universal.Edit),
Handler: self.checkSelected(self.editURL), Handler: self.checkSelected(self.editURL),
Description: self.c.Tr.LcEditSubmoduleUrl, Description: self.c.Tr.LcEditSubmoduleUrl,
}, },
{ {
Key: getKey(config.Submodules.Init), Key: opts.GetKey(opts.Config.Submodules.Init),
Handler: self.checkSelected(self.init), Handler: self.checkSelected(self.init),
Description: self.c.Tr.LcInitSubmodule, Description: self.c.Tr.LcInitSubmodule,
}, },
{ {
Key: getKey(config.Submodules.BulkMenu), Key: opts.GetKey(opts.Config.Submodules.BulkMenu),
Handler: self.openBulkActionsMenu, Handler: self.openBulkActionsMenu,
Description: self.c.Tr.LcViewBulkSubmoduleOptions, Description: self.c.Tr.LcViewBulkSubmoduleOptions,
OpensMenu: true, OpensMenu: true,
@ -83,8 +82,6 @@ func (self *SubmodulesController) Keybindings(getKey func(key string) interface{
Handler: func() error { return self.context.HandleClick(self.checkSelected(self.enter)) }, Handler: func() error { return self.context.HandleClick(self.checkSelected(self.enter)) },
}, },
} }
return append(bindings, self.context.Keybindings(getKey, config, guards)...)
} }
func (self *SubmodulesController) enter(submodule *models.SubmoduleConfig) error { func (self *SubmodulesController) enter(submodule *models.SubmoduleConfig) error {

View File

@ -7,7 +7,6 @@ import (
"github.com/jesseduffield/lazygit/pkg/commands" "github.com/jesseduffield/lazygit/pkg/commands"
"github.com/jesseduffield/lazygit/pkg/commands/git_commands" "github.com/jesseduffield/lazygit/pkg/commands/git_commands"
"github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/config"
"github.com/jesseduffield/lazygit/pkg/gui/types" "github.com/jesseduffield/lazygit/pkg/gui/types"
) )
@ -46,16 +45,16 @@ func NewSyncController(
} }
} }
func (self *SyncController) Keybindings(getKey func(key string) interface{}, config config.KeybindingConfig, guards types.KeybindingGuards) []*types.Binding { func (self *SyncController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding {
bindings := []*types.Binding{ bindings := []*types.Binding{
{ {
Key: getKey(config.Universal.PushFiles), Key: opts.GetKey(opts.Config.Universal.PushFiles),
Handler: guards.NoPopupPanel(self.HandlePush), Handler: opts.Guards.NoPopupPanel(self.HandlePush),
Description: self.c.Tr.LcPush, Description: self.c.Tr.LcPush,
}, },
{ {
Key: getKey(config.Universal.PullFiles), Key: opts.GetKey(opts.Config.Universal.PullFiles),
Handler: guards.NoPopupPanel(self.HandlePull), Handler: opts.Guards.NoPopupPanel(self.HandlePull),
Description: self.c.Tr.LcPull, Description: self.c.Tr.LcPull,
}, },
} }

View File

@ -3,7 +3,6 @@ package controllers
import ( import (
"github.com/jesseduffield/lazygit/pkg/commands" "github.com/jesseduffield/lazygit/pkg/commands"
"github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/config"
"github.com/jesseduffield/lazygit/pkg/gui/context" "github.com/jesseduffield/lazygit/pkg/gui/context"
"github.com/jesseduffield/lazygit/pkg/gui/types" "github.com/jesseduffield/lazygit/pkg/gui/types"
"github.com/jesseduffield/lazygit/pkg/utils" "github.com/jesseduffield/lazygit/pkg/utils"
@ -48,42 +47,42 @@ func NewTagsController(
} }
} }
func (self *TagsController) Keybindings(getKey func(key string) interface{}, config config.KeybindingConfig, guards types.KeybindingGuards) []*types.Binding { func (self *TagsController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding {
bindings := []*types.Binding{ bindings := []*types.Binding{
{ {
Key: getKey(config.Universal.Select), Key: opts.GetKey(opts.Config.Universal.Select),
Handler: self.withSelectedTag(self.checkout), Handler: self.withSelectedTag(self.checkout),
Description: self.c.Tr.LcCheckout, Description: self.c.Tr.LcCheckout,
}, },
{ {
Key: getKey(config.Universal.Remove), Key: opts.GetKey(opts.Config.Universal.Remove),
Handler: self.withSelectedTag(self.delete), Handler: self.withSelectedTag(self.delete),
Description: self.c.Tr.LcDeleteTag, Description: self.c.Tr.LcDeleteTag,
}, },
{ {
Key: getKey(config.Branches.PushTag), Key: opts.GetKey(opts.Config.Branches.PushTag),
Handler: self.withSelectedTag(self.push), Handler: self.withSelectedTag(self.push),
Description: self.c.Tr.LcPushTag, Description: self.c.Tr.LcPushTag,
}, },
{ {
Key: getKey(config.Universal.New), Key: opts.GetKey(opts.Config.Universal.New),
Handler: self.create, Handler: self.create,
Description: self.c.Tr.LcCreateTag, Description: self.c.Tr.LcCreateTag,
}, },
{ {
Key: getKey(config.Commits.ViewResetOptions), Key: opts.GetKey(opts.Config.Commits.ViewResetOptions),
Handler: self.withSelectedTag(self.createResetMenu), Handler: self.withSelectedTag(self.createResetMenu),
Description: self.c.Tr.LcViewResetOptions, Description: self.c.Tr.LcViewResetOptions,
OpensMenu: true, OpensMenu: true,
}, },
{ {
Key: getKey(config.Universal.GoInto), Key: opts.GetKey(opts.Config.Universal.GoInto),
Handler: self.withSelectedTag(self.enter), Handler: self.withSelectedTag(self.enter),
Description: self.c.Tr.LcViewCommits, Description: self.c.Tr.LcViewCommits,
}, },
} }
return append(bindings, self.context.Keybindings(getKey, config, guards)...) return bindings
} }
func (self *TagsController) checkout(tag *models.Tag) error { func (self *TagsController) checkout(tag *models.Tag) error {

View File

@ -4,7 +4,6 @@ import (
"github.com/jesseduffield/lazygit/pkg/commands" "github.com/jesseduffield/lazygit/pkg/commands"
"github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/commands/types/enums" "github.com/jesseduffield/lazygit/pkg/commands/types/enums"
"github.com/jesseduffield/lazygit/pkg/config"
"github.com/jesseduffield/lazygit/pkg/gui/types" "github.com/jesseduffield/lazygit/pkg/gui/types"
"github.com/jesseduffield/lazygit/pkg/utils" "github.com/jesseduffield/lazygit/pkg/utils"
) )
@ -64,19 +63,15 @@ type reflogAction struct {
to string to string
} }
func (self *UndoController) Keybindings( func (self *UndoController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding {
getKey func(key string) interface{},
config config.KeybindingConfig,
guards types.KeybindingGuards,
) []*types.Binding {
bindings := []*types.Binding{ bindings := []*types.Binding{
{ {
Key: getKey(config.Universal.Undo), Key: opts.GetKey(opts.Config.Universal.Undo),
Handler: self.reflogUndo, Handler: self.reflogUndo,
Description: self.c.Tr.LcUndoReflog, Description: self.c.Tr.LcUndoReflog,
}, },
{ {
Key: getKey(config.Universal.Redo), Key: opts.GetKey(opts.Config.Universal.Redo),
Handler: self.reflogRedo, Handler: self.reflogRedo,
Description: self.c.Tr.LcRedoReflog, Description: self.c.Tr.LcRedoReflog,
}, },

View File

@ -581,14 +581,16 @@ func (gui *Gui) resetControllers() {
gui.helpers.Rebase.CheckMergeOrRebase, gui.helpers.Rebase.CheckMergeOrRebase,
) )
submodulesController := controllers.NewSubmodulesController(
controllerCommon,
gui.State.Contexts.Submodules,
gui.git,
gui.enterSubmodule,
gui.getSelectedSubmodule,
)
gui.Controllers = Controllers{ gui.Controllers = Controllers{
Submodules: controllers.NewSubmodulesController( Submodules: submodulesController,
controllerCommon,
gui.State.Contexts.Submodules,
gui.git,
gui.enterSubmodule,
gui.getSelectedSubmodule,
),
Files: controllers.NewFilesController( Files: controllers.NewFilesController(
controllerCommon, controllerCommon,
gui.State.Contexts.Files, gui.State.Contexts.Files,
@ -670,6 +672,17 @@ func (gui *Gui) resetControllers() {
), ),
Sync: syncController, Sync: syncController,
} }
gui.State.Contexts.Submodules.AddKeybindingsFn(gui.Controllers.Submodules.GetKeybindings)
gui.State.Contexts.Files.AddKeybindingsFn(gui.Controllers.Files.GetKeybindings)
gui.State.Contexts.Tags.AddKeybindingsFn(gui.Controllers.Tags.GetKeybindings)
// TODO: commit to one name here: local commits or branch commits
gui.State.Contexts.BranchCommits.AddKeybindingsFn(gui.Controllers.LocalCommits.GetKeybindings)
gui.State.Contexts.BranchCommits.AddKeybindingsFn(gui.Controllers.Bisect.GetKeybindings)
gui.State.Contexts.Remotes.AddKeybindingsFn(gui.Controllers.Remotes.GetKeybindings)
gui.State.Contexts.Menu.AddKeybindingsFn(gui.Controllers.Menu.GetKeybindings)
gui.State.Contexts.Menu.AddKeybindingsFn(gui.Controllers.Menu.GetKeybindings)
// TODO: handle global contexts
} }
var RuneReplacements = map[rune]string{ var RuneReplacements = map[rune]string{

View File

@ -1355,16 +1355,16 @@ func (gui *Gui) GetInitialKeybindings() []*types.Binding {
}, },
} }
keybindingsOpts := types.KeybindingsOpts{
GetKey: gui.getKey,
Config: config,
Guards: guards,
}
// global bindings
for _, controller := range []types.IController{ for _, controller := range []types.IController{
gui.Controllers.LocalCommits,
gui.Controllers.Submodules,
gui.Controllers.Files,
gui.Controllers.Remotes,
gui.Controllers.Menu,
gui.Controllers.Bisect,
gui.Controllers.Undo,
gui.Controllers.Sync, gui.Controllers.Sync,
gui.Controllers.Tags, gui.Controllers.Undo,
} { } {
context := controller.Context() context := controller.Context()
viewName := "" viewName := ""
@ -1375,27 +1375,17 @@ func (gui *Gui) GetInitialKeybindings() []*types.Binding {
contextKeys = []string{string(context.GetKey())} contextKeys = []string{string(context.GetKey())}
} }
for _, binding := range controller.Keybindings(gui.getKey, config, guards) { for _, binding := range controller.GetKeybindings(keybindingsOpts) {
binding.Contexts = contextKeys binding.Contexts = contextKeys
binding.ViewName = viewName binding.ViewName = viewName
bindings = append(bindings, binding) bindings = append(bindings, binding)
} }
} }
// while migrating we'll continue providing keybindings from the list contexts themselves. for _, context := range gui.allContexts() {
// for each controller we add above we need to remove the corresponding list context from here. viewName := context.GetViewName()
for _, listContext := range []types.IListContext{ contextKey := context.GetKey()
gui.State.Contexts.Branches, for _, binding := range context.GetKeybindings(keybindingsOpts) {
gui.State.Contexts.RemoteBranches,
gui.State.Contexts.ReflogCommits,
gui.State.Contexts.SubCommits,
gui.State.Contexts.Stash,
gui.State.Contexts.CommitFiles,
gui.State.Contexts.Suggestions,
} {
viewName := listContext.GetViewName()
contextKey := listContext.GetKey()
for _, binding := range listContext.Keybindings(gui.getKey, config, guards) {
binding.Contexts = []string{string(contextKey)} binding.Contexts = []string{string(contextKey)}
binding.ViewName = viewName binding.ViewName = viewName
bindings = append(bindings, binding) bindings = append(bindings, binding)

View File

@ -4,7 +4,6 @@ import (
"fmt" "fmt"
"github.com/jesseduffield/gocui" "github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/config"
"github.com/jesseduffield/lazygit/pkg/gui/context" "github.com/jesseduffield/lazygit/pkg/gui/context"
"github.com/jesseduffield/lazygit/pkg/gui/types" "github.com/jesseduffield/lazygit/pkg/gui/types"
) )
@ -232,32 +231,34 @@ func (self *ListContext) HandleRenderToMain() error {
return nil return nil
} }
func (self *ListContext) Keybindings( func (self *ListContext) attachKeybindings() *ListContext {
getKey func(key string) interface{}, self.BaseContext.AddKeybindingsFn(self.keybindings)
config config.KeybindingConfig,
guards types.KeybindingGuards, return self
) []*types.Binding { }
func (self *ListContext) keybindings(opts types.KeybindingsOpts) []*types.Binding {
return []*types.Binding{ return []*types.Binding{
{Tag: "navigation", Key: getKey(config.Universal.PrevItemAlt), Modifier: gocui.ModNone, Handler: self.HandlePrevLine}, {Tag: "navigation", Key: opts.GetKey(opts.Config.Universal.PrevItemAlt), Modifier: gocui.ModNone, Handler: self.HandlePrevLine},
{Tag: "navigation", Key: getKey(config.Universal.PrevItem), Modifier: gocui.ModNone, Handler: self.HandlePrevLine}, {Tag: "navigation", Key: opts.GetKey(opts.Config.Universal.PrevItem), Modifier: gocui.ModNone, Handler: self.HandlePrevLine},
{Tag: "navigation", Key: gocui.MouseWheelUp, Modifier: gocui.ModNone, Handler: self.HandlePrevLine}, {Tag: "navigation", Key: gocui.MouseWheelUp, Modifier: gocui.ModNone, Handler: self.HandlePrevLine},
{Tag: "navigation", Key: getKey(config.Universal.NextItemAlt), Modifier: gocui.ModNone, Handler: self.HandleNextLine}, {Tag: "navigation", Key: opts.GetKey(opts.Config.Universal.NextItemAlt), Modifier: gocui.ModNone, Handler: self.HandleNextLine},
{Tag: "navigation", Key: getKey(config.Universal.NextItem), Modifier: gocui.ModNone, Handler: self.HandleNextLine}, {Tag: "navigation", Key: opts.GetKey(opts.Config.Universal.NextItem), Modifier: gocui.ModNone, Handler: self.HandleNextLine},
{Tag: "navigation", Key: getKey(config.Universal.PrevPage), Modifier: gocui.ModNone, Handler: self.HandlePrevPage, Description: self.Gui.c.Tr.LcPrevPage}, {Tag: "navigation", Key: opts.GetKey(opts.Config.Universal.PrevPage), Modifier: gocui.ModNone, Handler: self.HandlePrevPage, Description: self.Gui.c.Tr.LcPrevPage},
{Tag: "navigation", Key: getKey(config.Universal.NextPage), Modifier: gocui.ModNone, Handler: self.HandleNextPage, Description: self.Gui.c.Tr.LcNextPage}, {Tag: "navigation", Key: opts.GetKey(opts.Config.Universal.NextPage), Modifier: gocui.ModNone, Handler: self.HandleNextPage, Description: self.Gui.c.Tr.LcNextPage},
{Tag: "navigation", Key: getKey(config.Universal.GotoTop), Modifier: gocui.ModNone, Handler: self.HandleGotoTop, Description: self.Gui.c.Tr.LcGotoTop}, {Tag: "navigation", Key: opts.GetKey(opts.Config.Universal.GotoTop), Modifier: gocui.ModNone, Handler: self.HandleGotoTop, Description: self.Gui.c.Tr.LcGotoTop},
{Key: gocui.MouseLeft, Modifier: gocui.ModNone, Handler: func() error { return self.HandleClick(nil) }}, {Key: gocui.MouseLeft, Modifier: gocui.ModNone, Handler: func() error { return self.HandleClick(nil) }},
{Tag: "navigation", Key: gocui.MouseWheelDown, Modifier: gocui.ModNone, Handler: self.HandleNextLine}, {Tag: "navigation", Key: gocui.MouseWheelDown, Modifier: gocui.ModNone, Handler: self.HandleNextLine},
{Tag: "navigation", Key: getKey(config.Universal.ScrollLeft), Modifier: gocui.ModNone, Handler: self.HandleScrollLeft}, {Tag: "navigation", Key: opts.GetKey(opts.Config.Universal.ScrollLeft), Modifier: gocui.ModNone, Handler: self.HandleScrollLeft},
{Tag: "navigation", Key: getKey(config.Universal.ScrollRight), Modifier: gocui.ModNone, Handler: self.HandleScrollRight}, {Tag: "navigation", Key: opts.GetKey(opts.Config.Universal.ScrollRight), Modifier: gocui.ModNone, Handler: self.HandleScrollRight},
{ {
Key: getKey(config.Universal.StartSearch), Key: opts.GetKey(opts.Config.Universal.StartSearch),
Handler: func() error { return self.Gui.handleOpenSearch(self.GetViewName()) }, Handler: func() error { return self.Gui.handleOpenSearch(self.GetViewName()) },
Description: self.Gui.c.Tr.LcStartSearch, Description: self.Gui.c.Tr.LcStartSearch,
Tag: "navigation", Tag: "navigation",
}, },
{ {
Key: getKey(config.Universal.GotoBottom), Key: opts.GetKey(opts.Config.Universal.GotoBottom),
Description: self.Gui.c.Tr.LcGotoBottom, Description: self.Gui.c.Tr.LcGotoBottom,
Handler: self.HandleGotoBottom, Handler: self.HandleGotoBottom,
Tag: "navigation", Tag: "navigation",

View File

@ -13,7 +13,7 @@ import (
) )
func (gui *Gui) menuListContext() types.IListContext { func (gui *Gui) menuListContext() types.IListContext {
return &ListContext{ return (&ListContext{
BaseContext: context.NewBaseContext(context.NewBaseContextOpts{ BaseContext: context.NewBaseContext(context.NewBaseContextOpts{
ViewName: "menu", ViewName: "menu",
Key: "menu", Key: "menu",
@ -25,7 +25,7 @@ func (gui *Gui) menuListContext() types.IListContext {
Gui: gui, Gui: gui,
// no GetDisplayStrings field because we do a custom render on menu creation // no GetDisplayStrings field because we do a custom render on menu creation
} }).attachKeybindings()
} }
func (gui *Gui) filesListContext() *context.WorkingTreeContext { func (gui *Gui) filesListContext() *context.WorkingTreeContext {
@ -49,7 +49,7 @@ func (gui *Gui) filesListContext() *context.WorkingTreeContext {
} }
func (gui *Gui) branchesListContext() types.IListContext { func (gui *Gui) branchesListContext() types.IListContext {
return &ListContext{ return (&ListContext{
BaseContext: context.NewBaseContext(context.NewBaseContextOpts{ BaseContext: context.NewBaseContext(context.NewBaseContextOpts{
ViewName: "branches", ViewName: "branches",
WindowName: "branches", WindowName: "branches",
@ -70,11 +70,11 @@ func (gui *Gui) branchesListContext() types.IListContext {
} }
return item.ID() return item.ID()
}, },
} }).attachKeybindings()
} }
func (gui *Gui) remotesListContext() types.IListContext { func (gui *Gui) remotesListContext() types.IListContext {
return &ListContext{ return (&ListContext{
BaseContext: context.NewBaseContext(context.NewBaseContextOpts{ BaseContext: context.NewBaseContext(context.NewBaseContextOpts{
ViewName: "branches", ViewName: "branches",
WindowName: "branches", WindowName: "branches",
@ -95,11 +95,11 @@ func (gui *Gui) remotesListContext() types.IListContext {
} }
return item.ID() return item.ID()
}, },
} }).attachKeybindings()
} }
func (gui *Gui) remoteBranchesListContext() types.IListContext { func (gui *Gui) remoteBranchesListContext() types.IListContext {
return &ListContext{ return (&ListContext{
BaseContext: context.NewBaseContext(context.NewBaseContextOpts{ BaseContext: context.NewBaseContext(context.NewBaseContextOpts{
ViewName: "branches", ViewName: "branches",
WindowName: "branches", WindowName: "branches",
@ -120,7 +120,7 @@ func (gui *Gui) remoteBranchesListContext() types.IListContext {
} }
return item.ID() return item.ID()
}, },
} }).attachKeybindings()
} }
func (gui *Gui) withDiffModeCheck(f func() error) func() error { func (gui *Gui) withDiffModeCheck(f func() error) func() error {
@ -149,7 +149,7 @@ func (gui *Gui) tagsListContext() *context.TagsContext {
func (gui *Gui) branchCommitsListContext() types.IListContext { func (gui *Gui) branchCommitsListContext() types.IListContext {
parseEmoji := gui.c.UserConfig.Git.ParseEmoji parseEmoji := gui.c.UserConfig.Git.ParseEmoji
return &ListContext{ return (&ListContext{
BaseContext: context.NewBaseContext(context.NewBaseContextOpts{ BaseContext: context.NewBaseContext(context.NewBaseContextOpts{
ViewName: "commits", ViewName: "commits",
WindowName: "commits", WindowName: "commits",
@ -190,12 +190,12 @@ func (gui *Gui) branchCommitsListContext() types.IListContext {
return item.ID() return item.ID()
}, },
RenderSelection: true, RenderSelection: true,
} }).attachKeybindings()
} }
func (gui *Gui) subCommitsListContext() types.IListContext { func (gui *Gui) subCommitsListContext() types.IListContext {
parseEmoji := gui.c.UserConfig.Git.ParseEmoji parseEmoji := gui.c.UserConfig.Git.ParseEmoji
return &ListContext{ return (&ListContext{
BaseContext: context.NewBaseContext(context.NewBaseContextOpts{ BaseContext: context.NewBaseContext(context.NewBaseContextOpts{
ViewName: "branches", ViewName: "branches",
WindowName: "branches", WindowName: "branches",
@ -235,7 +235,7 @@ func (gui *Gui) subCommitsListContext() types.IListContext {
return item.ID() return item.ID()
}, },
RenderSelection: true, RenderSelection: true,
} }).attachKeybindings()
} }
func (gui *Gui) shouldShowGraph() bool { func (gui *Gui) shouldShowGraph() bool {
@ -259,7 +259,7 @@ func (gui *Gui) shouldShowGraph() bool {
func (gui *Gui) reflogCommitsListContext() types.IListContext { func (gui *Gui) reflogCommitsListContext() types.IListContext {
parseEmoji := gui.c.UserConfig.Git.ParseEmoji parseEmoji := gui.c.UserConfig.Git.ParseEmoji
return &ListContext{ return (&ListContext{
BaseContext: context.NewBaseContext(context.NewBaseContextOpts{ BaseContext: context.NewBaseContext(context.NewBaseContextOpts{
ViewName: "commits", ViewName: "commits",
WindowName: "commits", WindowName: "commits",
@ -286,11 +286,11 @@ func (gui *Gui) reflogCommitsListContext() types.IListContext {
} }
return item.ID() return item.ID()
}, },
} }).attachKeybindings()
} }
func (gui *Gui) stashListContext() types.IListContext { func (gui *Gui) stashListContext() types.IListContext {
return &ListContext{ return (&ListContext{
BaseContext: context.NewBaseContext(context.NewBaseContextOpts{ BaseContext: context.NewBaseContext(context.NewBaseContextOpts{
ViewName: "stash", ViewName: "stash",
WindowName: "stash", WindowName: "stash",
@ -311,7 +311,7 @@ func (gui *Gui) stashListContext() types.IListContext {
} }
return item.ID() return item.ID()
}, },
} }).attachKeybindings()
} }
func (gui *Gui) commitFilesListContext() *context.CommitFilesContext { func (gui *Gui) commitFilesListContext() *context.CommitFilesContext {
@ -339,7 +339,7 @@ func (gui *Gui) commitFilesListContext() *context.CommitFilesContext {
} }
func (gui *Gui) submodulesListContext() types.IListContext { func (gui *Gui) submodulesListContext() types.IListContext {
return &ListContext{ return (&ListContext{
BaseContext: context.NewBaseContext(context.NewBaseContextOpts{ BaseContext: context.NewBaseContext(context.NewBaseContextOpts{
ViewName: "files", ViewName: "files",
WindowName: "files", WindowName: "files",
@ -360,11 +360,11 @@ func (gui *Gui) submodulesListContext() types.IListContext {
} }
return item.ID() return item.ID()
}, },
} }).attachKeybindings()
} }
func (gui *Gui) suggestionsListContext() types.IListContext { func (gui *Gui) suggestionsListContext() types.IListContext {
return &ListContext{ return (&ListContext{
BaseContext: context.NewBaseContext(context.NewBaseContextOpts{ BaseContext: context.NewBaseContext(context.NewBaseContextOpts{
ViewName: "suggestions", ViewName: "suggestions",
WindowName: "suggestions", WindowName: "suggestions",
@ -377,7 +377,7 @@ func (gui *Gui) suggestionsListContext() types.IListContext {
GetDisplayStrings: func(startIdx int, length int) [][]string { GetDisplayStrings: func(startIdx int, length int) [][]string {
return presentation.GetSuggestionListDisplayStrings(gui.State.Suggestions) return presentation.GetSuggestionListDisplayStrings(gui.State.Suggestions)
}, },
} }).attachKeybindings()
} }
func (gui *Gui) getListContexts() []types.IListContext { func (gui *Gui) getListContexts() []types.IListContext {

View File

@ -28,6 +28,9 @@ type IBaseContext interface {
GetKey() ContextKey GetKey() ContextKey
GetOptionsMap() map[string]string GetOptionsMap() map[string]string
GetKeybindings(opts KeybindingsOpts) []*Binding
AddKeybindingsFn(KeybindingsFn)
} }
type Context interface { type Context interface {
@ -46,12 +49,16 @@ type OnFocusOpts struct {
type ContextKey string type ContextKey string
type KeybindingsOpts struct {
GetKey func(key string) interface{}
Config config.KeybindingConfig
Guards KeybindingGuards
}
type KeybindingsFn func(opts KeybindingsOpts) []*Binding
type HasKeybindings interface { type HasKeybindings interface {
Keybindings( GetKeybindings(opts KeybindingsOpts) []*Binding
getKey func(key string) interface{},
config config.KeybindingConfig,
guards KeybindingGuards,
) []*Binding
} }
type IController interface { type IController interface {