1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-01-10 04:07:18 +02:00
This commit is contained in:
Jesse Duffield 2020-08-16 14:16:28 +10:00
parent 7f89113245
commit 6fb83b740b
6 changed files with 36 additions and 33 deletions

View File

@ -176,6 +176,7 @@ func (gui *Gui) activateContext(c Context) error {
} }
newView := gui.g.CurrentView() newView := gui.g.CurrentView()
newView.Context = c.GetKey()
gui.g.Cursor = newView.Editable gui.g.Cursor = newView.Editable
@ -190,6 +191,7 @@ func (gui *Gui) activateContext(c Context) error {
return err return err
} }
// TODO: consider removing this and instead depending on the .Context field of views
gui.State.ViewContextMap[c.GetViewName()] = c gui.State.ViewContextMap[c.GetViewName()] = c
return nil return nil
@ -228,6 +230,7 @@ func (gui *Gui) createContextTree() {
OnFocus: gui.handleStatusSelect, OnFocus: gui.handleStatusSelect,
Kind: SIDE_CONTEXT, Kind: SIDE_CONTEXT,
ViewName: "status", ViewName: "status",
Key: "status",
}, },
}, },
Files: SimpleContextNode{ Files: SimpleContextNode{
@ -314,7 +317,7 @@ func (gui *Gui) createContextTree() {
OnFocus: func() error { return gui.handleCommitMessageFocused() }, OnFocus: func() error { return gui.handleCommitMessageFocused() },
Kind: PERSISTENT_POPUP, Kind: PERSISTENT_POPUP,
ViewName: "commitMessage", ViewName: "commitMessage",
Key: "commit-message", Key: "commit-message", // admittedly awkward to have view names in camelCase and contexts in kebab-case
}, },
}, },
Search: SimpleContextNode{ Search: SimpleContextNode{

View File

@ -1408,16 +1408,16 @@ func (gui *Gui) GetInitialKeybindings() []*Binding {
for _, listView := range gui.getListViews() { for _, listView := range gui.getListViews() {
bindings = append(bindings, []*Binding{ bindings = append(bindings, []*Binding{
{ViewName: listView.ViewName, Contexts: []string{listView.Context}, Key: gui.getKey("universal.prevItem-alt"), Modifier: gocui.ModNone, Handler: listView.handlePrevLine}, {ViewName: listView.ViewName, Contexts: []string{listView.ContextKey}, Key: gui.getKey("universal.prevItem-alt"), Modifier: gocui.ModNone, Handler: listView.handlePrevLine},
{ViewName: listView.ViewName, Contexts: []string{listView.Context}, Key: gui.getKey("universal.prevItem"), Modifier: gocui.ModNone, Handler: listView.handlePrevLine}, {ViewName: listView.ViewName, Contexts: []string{listView.ContextKey}, Key: gui.getKey("universal.prevItem"), Modifier: gocui.ModNone, Handler: listView.handlePrevLine},
{ViewName: listView.ViewName, Contexts: []string{listView.Context}, Key: gocui.MouseWheelUp, Modifier: gocui.ModNone, Handler: listView.handlePrevLine}, {ViewName: listView.ViewName, Contexts: []string{listView.ContextKey}, Key: gocui.MouseWheelUp, Modifier: gocui.ModNone, Handler: listView.handlePrevLine},
{ViewName: listView.ViewName, Contexts: []string{listView.Context}, Key: gui.getKey("universal.nextItem-alt"), Modifier: gocui.ModNone, Handler: listView.handleNextLine}, {ViewName: listView.ViewName, Contexts: []string{listView.ContextKey}, Key: gui.getKey("universal.nextItem-alt"), Modifier: gocui.ModNone, Handler: listView.handleNextLine},
{ViewName: listView.ViewName, Contexts: []string{listView.Context}, Key: gui.getKey("universal.nextItem"), Modifier: gocui.ModNone, Handler: listView.handleNextLine}, {ViewName: listView.ViewName, Contexts: []string{listView.ContextKey}, Key: gui.getKey("universal.nextItem"), Modifier: gocui.ModNone, Handler: listView.handleNextLine},
{ViewName: listView.ViewName, Contexts: []string{listView.Context}, Key: gui.getKey("universal.prevPage"), Modifier: gocui.ModNone, Handler: listView.handlePrevPage, Description: gui.Tr.SLocalize("prevPage")}, {ViewName: listView.ViewName, Contexts: []string{listView.ContextKey}, Key: gui.getKey("universal.prevPage"), Modifier: gocui.ModNone, Handler: listView.handlePrevPage, Description: gui.Tr.SLocalize("prevPage")},
{ViewName: listView.ViewName, Contexts: []string{listView.Context}, Key: gui.getKey("universal.nextPage"), Modifier: gocui.ModNone, Handler: listView.handleNextPage, Description: gui.Tr.SLocalize("nextPage")}, {ViewName: listView.ViewName, Contexts: []string{listView.ContextKey}, Key: gui.getKey("universal.nextPage"), Modifier: gocui.ModNone, Handler: listView.handleNextPage, Description: gui.Tr.SLocalize("nextPage")},
{ViewName: listView.ViewName, Contexts: []string{listView.Context}, Key: gui.getKey("universal.gotoTop"), Modifier: gocui.ModNone, Handler: listView.handleGotoTop, Description: gui.Tr.SLocalize("gotoTop")}, {ViewName: listView.ViewName, Contexts: []string{listView.ContextKey}, Key: gui.getKey("universal.gotoTop"), Modifier: gocui.ModNone, Handler: listView.handleGotoTop, Description: gui.Tr.SLocalize("gotoTop")},
{ViewName: listView.ViewName, Contexts: []string{listView.Context}, Key: gocui.MouseWheelDown, Modifier: gocui.ModNone, Handler: listView.handleNextLine}, {ViewName: listView.ViewName, Contexts: []string{listView.ContextKey}, Key: gocui.MouseWheelDown, Modifier: gocui.ModNone, Handler: listView.handleNextLine},
{ViewName: listView.ViewName, Contexts: []string{listView.Context}, Key: gocui.MouseLeft, Modifier: gocui.ModNone, Handler: listView.handleClick}, {ViewName: listView.ViewName, Contexts: []string{listView.ContextKey}, Key: gocui.MouseLeft, Modifier: gocui.ModNone, Handler: listView.handleClick},
}...) }...)
// the commits panel needs to lazyload things so it has a couple of its own handlers // the commits panel needs to lazyload things so it has a couple of its own handlers
@ -1431,14 +1431,14 @@ func (gui *Gui) GetInitialKeybindings() []*Binding {
bindings = append(bindings, []*Binding{ bindings = append(bindings, []*Binding{
{ {
ViewName: listView.ViewName, ViewName: listView.ViewName,
Contexts: []string{listView.Context}, Contexts: []string{listView.ContextKey},
Key: gui.getKey("universal.startSearch"), Key: gui.getKey("universal.startSearch"),
Handler: openSearchHandler, Handler: openSearchHandler,
Description: gui.Tr.SLocalize("startSearch"), Description: gui.Tr.SLocalize("startSearch"),
}, },
{ {
ViewName: listView.ViewName, ViewName: listView.ViewName,
Contexts: []string{listView.Context}, Contexts: []string{listView.ContextKey},
Key: gui.getKey("universal.gotoBottom"), Key: gui.getKey("universal.gotoBottom"),
Handler: gotoBottomHandler, Handler: gotoBottomHandler,
Description: gui.Tr.SLocalize("gotoBottom"), Description: gui.Tr.SLocalize("gotoBottom"),

View File

@ -286,20 +286,20 @@ func (gui *Gui) layout(g *gocui.Gui) error {
} }
listViewStates := []listViewState{ listViewStates := []listViewState{
{view: filesView, context: "", selectedLine: gui.State.Panels.Files.SelectedLine, lineCount: len(gui.State.Files), listView: gui.filesListView()}, {view: filesView, context: "files", selectedLine: gui.State.Panels.Files.SelectedLine, lineCount: len(gui.State.Files), listView: gui.filesListView()},
{view: branchesView, context: "local-branches", selectedLine: gui.State.Panels.Branches.SelectedLine, lineCount: len(gui.State.Branches), listView: gui.branchesListView()}, {view: branchesView, context: "local-branches", selectedLine: gui.State.Panels.Branches.SelectedLine, lineCount: len(gui.State.Branches), listView: gui.branchesListView()},
{view: branchesView, context: "remotes", selectedLine: gui.State.Panels.Remotes.SelectedLine, lineCount: len(gui.State.Remotes), listView: gui.remotesListView()}, {view: branchesView, context: "remotes", selectedLine: gui.State.Panels.Remotes.SelectedLine, lineCount: len(gui.State.Remotes), listView: gui.remotesListView()},
{view: branchesView, context: "remote-branches", selectedLine: gui.State.Panels.RemoteBranches.SelectedLine, lineCount: len(gui.State.Remotes), listView: gui.remoteBranchesListView()}, {view: branchesView, context: "remote-branches", selectedLine: gui.State.Panels.RemoteBranches.SelectedLine, lineCount: len(gui.State.Remotes), listView: gui.remoteBranchesListView()},
{view: branchesView, context: "tags", selectedLine: gui.State.Panels.Tags.SelectedLine, lineCount: len(gui.State.Tags), listView: gui.tagsListView()}, {view: branchesView, context: "tags", selectedLine: gui.State.Panels.Tags.SelectedLine, lineCount: len(gui.State.Tags), listView: gui.tagsListView()},
{view: commitsView, context: "branch-commits", selectedLine: gui.State.Panels.Commits.SelectedLine, lineCount: len(gui.State.Commits), listView: gui.branchCommitsListView()}, {view: commitsView, context: "branch-commits", selectedLine: gui.State.Panels.Commits.SelectedLine, lineCount: len(gui.State.Commits), listView: gui.branchCommitsListView()},
{view: commitsView, context: "reflog-commits", selectedLine: gui.State.Panels.ReflogCommits.SelectedLine, lineCount: len(gui.State.FilteredReflogCommits), listView: gui.reflogCommitsListView()}, {view: commitsView, context: "reflog-commits", selectedLine: gui.State.Panels.ReflogCommits.SelectedLine, lineCount: len(gui.State.FilteredReflogCommits), listView: gui.reflogCommitsListView()},
{view: stashView, context: "", selectedLine: gui.State.Panels.Stash.SelectedLine, lineCount: len(gui.State.StashEntries), listView: gui.stashListView()}, {view: stashView, context: "stash", selectedLine: gui.State.Panels.Stash.SelectedLine, lineCount: len(gui.State.StashEntries), listView: gui.stashListView()},
{view: commitFilesView, context: "", selectedLine: gui.State.Panels.CommitFiles.SelectedLine, lineCount: len(gui.State.CommitFiles), listView: gui.commitFilesListView()}, {view: commitFilesView, context: "commit-files", selectedLine: gui.State.Panels.CommitFiles.SelectedLine, lineCount: len(gui.State.CommitFiles), listView: gui.commitFilesListView()},
} }
// menu view might not exist so we check to be safe // menu view might not exist so we check to be safe
if menuView, err := gui.g.View("menu"); err == nil { if menuView, err := gui.g.View("menu"); err == nil {
listViewStates = append(listViewStates, listViewState{view: menuView, context: "", selectedLine: gui.State.Panels.Menu.SelectedLine, lineCount: gui.State.MenuItemCount, listView: gui.menuListView()}) listViewStates = append(listViewStates, listViewState{view: menuView, context: "menu", selectedLine: gui.State.Panels.Menu.SelectedLine, lineCount: gui.State.MenuItemCount, listView: gui.menuListView()})
} }
for _, listViewState := range listViewStates { for _, listViewState := range listViewStates {
// ignore views where the context doesn't match up with the selected line we're trying to focus // ignore views where the context doesn't match up with the selected line we're trying to focus

View File

@ -332,7 +332,7 @@ func (gui *Gui) handleEscapeLineByLinePanel() {
func (gui *Gui) handleOpenFileAtLine() error { func (gui *Gui) handleOpenFileAtLine() error {
// again, would be good to use inheritance here (or maybe even composition) // again, would be good to use inheritance here (or maybe even composition)
var filename string var filename string
switch gui.State.MainContext { switch gui.currentContext().GetKey() {
case "patch-building": case "patch-building":
filename = gui.getSelectedCommitFileName() filename = gui.getSelectedCommitFileName()
case "staging": case "staging":

View File

@ -4,7 +4,7 @@ import "github.com/jesseduffield/gocui"
type ListView struct { type ListView struct {
ViewName string ViewName string
Context string ContextKey string
GetItemsLength func() int GetItemsLength func() int
GetSelectedLineIdxPtr func() *int GetSelectedLineIdxPtr func() *int
OnFocus func() error OnFocus func() error
@ -14,11 +14,10 @@ type ListView struct {
Gui *Gui Gui *Gui
RendersToMainView bool RendersToMainView bool
Kind int Kind int
Key string
} }
func (lv *ListView) GetKey() string { func (lv *ListView) GetKey() string {
return lv.Key return lv.ContextKey
} }
func (lv *ListView) GetKind() int { func (lv *ListView) GetKind() int {
@ -152,6 +151,7 @@ func (lv *ListView) onSearchSelect(selectedLineIdx int) error {
func (gui *Gui) menuListView() *ListView { func (gui *Gui) menuListView() *ListView {
return &ListView{ return &ListView{
ViewName: "menu", ViewName: "menu",
ContextKey: "menu",
GetItemsLength: func() int { return gui.getMenuView().LinesHeight() }, GetItemsLength: func() int { return gui.getMenuView().LinesHeight() },
GetSelectedLineIdxPtr: func() *int { return &gui.State.Panels.Menu.SelectedLine }, GetSelectedLineIdxPtr: func() *int { return &gui.State.Panels.Menu.SelectedLine },
OnFocus: gui.handleMenuSelect, OnFocus: gui.handleMenuSelect,
@ -161,13 +161,13 @@ func (gui *Gui) menuListView() *ListView {
Gui: gui, Gui: gui,
RendersToMainView: false, RendersToMainView: false,
Kind: PERSISTENT_POPUP, Kind: PERSISTENT_POPUP,
Key: "menu",
} }
} }
func (gui *Gui) filesListView() *ListView { func (gui *Gui) filesListView() *ListView {
return &ListView{ return &ListView{
ViewName: "files", ViewName: "files",
ContextKey: "files",
GetItemsLength: func() int { return len(gui.State.Files) }, GetItemsLength: func() int { return len(gui.State.Files) },
GetSelectedLineIdxPtr: func() *int { return &gui.State.Panels.Files.SelectedLine }, GetSelectedLineIdxPtr: func() *int { return &gui.State.Panels.Files.SelectedLine },
OnFocus: gui.focusAndSelectFile, OnFocus: gui.focusAndSelectFile,
@ -176,14 +176,13 @@ func (gui *Gui) filesListView() *ListView {
Gui: gui, Gui: gui,
RendersToMainView: false, RendersToMainView: false,
Kind: SIDE_CONTEXT, Kind: SIDE_CONTEXT,
Key: "files",
} }
} }
func (gui *Gui) branchesListView() *ListView { func (gui *Gui) branchesListView() *ListView {
return &ListView{ return &ListView{
ViewName: "branches", ViewName: "branches",
Context: "local-branches", ContextKey: "local-branches",
GetItemsLength: func() int { return len(gui.State.Branches) }, GetItemsLength: func() int { return len(gui.State.Branches) },
GetSelectedLineIdxPtr: func() *int { return &gui.State.Panels.Branches.SelectedLine }, GetSelectedLineIdxPtr: func() *int { return &gui.State.Panels.Branches.SelectedLine },
OnFocus: gui.handleBranchSelect, OnFocus: gui.handleBranchSelect,
@ -191,14 +190,13 @@ func (gui *Gui) branchesListView() *ListView {
Gui: gui, Gui: gui,
RendersToMainView: true, RendersToMainView: true,
Kind: SIDE_CONTEXT, Kind: SIDE_CONTEXT,
Key: "menu",
} }
} }
func (gui *Gui) remotesListView() *ListView { func (gui *Gui) remotesListView() *ListView {
return &ListView{ return &ListView{
ViewName: "branches", ViewName: "branches",
Context: "remotes", ContextKey: "remotes",
GetItemsLength: func() int { return len(gui.State.Remotes) }, GetItemsLength: func() int { return len(gui.State.Remotes) },
GetSelectedLineIdxPtr: func() *int { return &gui.State.Panels.Remotes.SelectedLine }, GetSelectedLineIdxPtr: func() *int { return &gui.State.Panels.Remotes.SelectedLine },
OnFocus: gui.renderRemotesWithSelection, OnFocus: gui.renderRemotesWithSelection,
@ -213,7 +211,7 @@ func (gui *Gui) remotesListView() *ListView {
func (gui *Gui) remoteBranchesListView() *ListView { func (gui *Gui) remoteBranchesListView() *ListView {
return &ListView{ return &ListView{
ViewName: "branches", ViewName: "branches",
Context: "remote-branches", ContextKey: "remote-branches",
GetItemsLength: func() int { return len(gui.State.RemoteBranches) }, GetItemsLength: func() int { return len(gui.State.RemoteBranches) },
GetSelectedLineIdxPtr: func() *int { return &gui.State.Panels.RemoteBranches.SelectedLine }, GetSelectedLineIdxPtr: func() *int { return &gui.State.Panels.RemoteBranches.SelectedLine },
OnFocus: gui.handleRemoteBranchSelect, OnFocus: gui.handleRemoteBranchSelect,
@ -227,7 +225,7 @@ func (gui *Gui) remoteBranchesListView() *ListView {
func (gui *Gui) tagsListView() *ListView { func (gui *Gui) tagsListView() *ListView {
return &ListView{ return &ListView{
ViewName: "branches", ViewName: "branches",
Context: "tags", ContextKey: "tags",
GetItemsLength: func() int { return len(gui.State.Tags) }, GetItemsLength: func() int { return len(gui.State.Tags) },
GetSelectedLineIdxPtr: func() *int { return &gui.State.Panels.Tags.SelectedLine }, GetSelectedLineIdxPtr: func() *int { return &gui.State.Panels.Tags.SelectedLine },
OnFocus: gui.handleTagSelect, OnFocus: gui.handleTagSelect,
@ -241,7 +239,7 @@ func (gui *Gui) tagsListView() *ListView {
func (gui *Gui) branchCommitsListView() *ListView { func (gui *Gui) branchCommitsListView() *ListView {
return &ListView{ return &ListView{
ViewName: "commits", ViewName: "commits",
Context: "branch-commits", ContextKey: "branch-commits",
GetItemsLength: func() int { return len(gui.State.Commits) }, GetItemsLength: func() int { return len(gui.State.Commits) },
GetSelectedLineIdxPtr: func() *int { return &gui.State.Panels.Commits.SelectedLine }, GetSelectedLineIdxPtr: func() *int { return &gui.State.Panels.Commits.SelectedLine },
OnFocus: gui.handleCommitSelect, OnFocus: gui.handleCommitSelect,
@ -256,7 +254,7 @@ func (gui *Gui) branchCommitsListView() *ListView {
func (gui *Gui) reflogCommitsListView() *ListView { func (gui *Gui) reflogCommitsListView() *ListView {
return &ListView{ return &ListView{
ViewName: "commits", ViewName: "commits",
Context: "reflog-commits", ContextKey: "reflog-commits",
GetItemsLength: func() int { return len(gui.State.FilteredReflogCommits) }, GetItemsLength: func() int { return len(gui.State.FilteredReflogCommits) },
GetSelectedLineIdxPtr: func() *int { return &gui.State.Panels.ReflogCommits.SelectedLine }, GetSelectedLineIdxPtr: func() *int { return &gui.State.Panels.ReflogCommits.SelectedLine },
OnFocus: gui.handleReflogCommitSelect, OnFocus: gui.handleReflogCommitSelect,
@ -270,6 +268,7 @@ func (gui *Gui) reflogCommitsListView() *ListView {
func (gui *Gui) stashListView() *ListView { func (gui *Gui) stashListView() *ListView {
return &ListView{ return &ListView{
ViewName: "stash", ViewName: "stash",
ContextKey: "stash",
GetItemsLength: func() int { return len(gui.State.StashEntries) }, GetItemsLength: func() int { return len(gui.State.StashEntries) },
GetSelectedLineIdxPtr: func() *int { return &gui.State.Panels.Stash.SelectedLine }, GetSelectedLineIdxPtr: func() *int { return &gui.State.Panels.Stash.SelectedLine },
OnFocus: gui.handleStashEntrySelect, OnFocus: gui.handleStashEntrySelect,
@ -283,6 +282,7 @@ func (gui *Gui) stashListView() *ListView {
func (gui *Gui) commitFilesListView() *ListView { func (gui *Gui) commitFilesListView() *ListView {
return &ListView{ return &ListView{
ViewName: "commitFiles", ViewName: "commitFiles",
ContextKey: "commitFiles",
GetItemsLength: func() int { return len(gui.State.CommitFiles) }, GetItemsLength: func() int { return len(gui.State.CommitFiles) },
GetSelectedLineIdxPtr: func() *int { return &gui.State.Panels.CommitFiles.SelectedLine }, GetSelectedLineIdxPtr: func() *int { return &gui.State.Panels.CommitFiles.SelectedLine },
OnFocus: gui.handleCommitFileSelect, OnFocus: gui.handleCommitFileSelect,

View File

@ -25,7 +25,7 @@ func (gui *Gui) nextSideWindow() error {
viewName := gui.getViewNameForWindow(newWindow) viewName := gui.getViewNameForWindow(newWindow)
return gui.switchContext(gui.State.ViewContextMap[viewName]) return gui.switchContextToView(viewName)
} }
func (gui *Gui) previousSideWindow() error { func (gui *Gui) previousSideWindow() error {
@ -51,7 +51,7 @@ func (gui *Gui) previousSideWindow() error {
viewName := gui.getViewNameForWindow(newWindow) viewName := gui.getViewNameForWindow(newWindow)
return gui.switchContext(gui.State.ViewContextMap[viewName]) return gui.switchContextToView(viewName)
} }
func (gui *Gui) goToSideWindow(sideViewName string) func(g *gocui.Gui, v *gocui.View) error { func (gui *Gui) goToSideWindow(sideViewName string) func(g *gocui.Gui, v *gocui.View) error {
@ -61,6 +61,6 @@ func (gui *Gui) goToSideWindow(sideViewName string) func(g *gocui.Gui, v *gocui.
gui.Log.Error(err) gui.Log.Error(err)
return nil return nil
} }
return gui.switchContext(gui.State.ViewContextMap[sideViewName]) return gui.switchContextToView(sideViewName)
} }
} }