1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-01-12 04:23:03 +02:00

refactor list view

This commit is contained in:
Jesse Duffield 2020-08-15 17:27:34 +10:00
parent 9b7a6934b3
commit 28208e8364

View File

@ -7,9 +7,9 @@ type listView struct {
context string context string
getItemsLength func() int getItemsLength func() int
getSelectedLineIdxPtr func() *int getSelectedLineIdxPtr func() *int
handleFocus func(g *gocui.Gui, v *gocui.View) error handleFocus func() error
handleItemSelect func(g *gocui.Gui, v *gocui.View) error handleItemSelect func() error
handleClickSelectedItem func(g *gocui.Gui, v *gocui.View) error handleClickSelectedItem func() error
gui *Gui gui *Gui
rendersToMainView bool rendersToMainView bool
} }
@ -35,11 +35,7 @@ func (lv *listView) handleLineChange(change int) error {
} }
} }
view, err := lv.gui.g.View(lv.viewName) return lv.handleItemSelect()
if err != nil {
return err
}
return lv.handleItemSelect(lv.gui.g, view)
} }
func (lv *listView) handleNextPage(g *gocui.Gui, v *gocui.View) error { func (lv *listView) handleNextPage(g *gocui.Gui, v *gocui.View) error {
@ -86,7 +82,7 @@ func (lv *listView) handleClick(g *gocui.Gui, v *gocui.View) error {
newSelectedLineIdx := v.SelectedLineIdx() newSelectedLineIdx := v.SelectedLineIdx()
if newSelectedLineIdx > lv.getItemsLength()-1 { if newSelectedLineIdx > lv.getItemsLength()-1 {
return lv.handleFocus(lv.gui.g, v) return lv.handleFocus()
} }
*selectedLineIdxPtr = newSelectedLineIdx *selectedLineIdxPtr = newSelectedLineIdx
@ -99,9 +95,9 @@ func (lv *listView) handleClick(g *gocui.Gui, v *gocui.View) error {
prevViewName := lv.gui.currentViewName() prevViewName := lv.gui.currentViewName()
if prevSelectedLineIdx == newSelectedLineIdx && prevViewName == lv.viewName && lv.handleClickSelectedItem != nil { if prevSelectedLineIdx == newSelectedLineIdx && prevViewName == lv.viewName && lv.handleClickSelectedItem != nil {
return lv.handleClickSelectedItem(lv.gui.g, v) return lv.handleClickSelectedItem()
} }
return lv.handleItemSelect(lv.gui.g, v) return lv.handleItemSelect()
} }
func (gui *Gui) getListViews() []*listView { func (gui *Gui) getListViews() []*listView {
@ -110,10 +106,10 @@ func (gui *Gui) getListViews() []*listView {
viewName: "menu", viewName: "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 },
handleFocus: gui.wrappedHandler(gui.handleMenuSelect), handleFocus: gui.handleMenuSelect,
handleItemSelect: gui.wrappedHandler(gui.handleMenuSelect), handleItemSelect: gui.handleMenuSelect,
// need to add a layer of indirection here because the callback changes during runtime // need to add a layer of indirection here because the callback changes during runtime
handleClickSelectedItem: gui.wrappedHandler(func() error { return gui.State.Panels.Menu.OnPress(gui.g, nil) }), handleClickSelectedItem: func() error { return gui.State.Panels.Menu.OnPress(gui.g, nil) },
gui: gui, gui: gui,
rendersToMainView: false, rendersToMainView: false,
}, },
@ -121,9 +117,9 @@ func (gui *Gui) getListViews() []*listView {
viewName: "files", viewName: "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 },
handleFocus: gui.wrappedHandler(gui.focusAndSelectFile), handleFocus: gui.focusAndSelectFile,
handleItemSelect: gui.wrappedHandler(gui.focusAndSelectFile), handleItemSelect: gui.focusAndSelectFile,
handleClickSelectedItem: gui.wrappedHandler(gui.handleFilePress), handleClickSelectedItem: gui.handleFilePress,
gui: gui, gui: gui,
rendersToMainView: true, rendersToMainView: true,
}, },
@ -132,8 +128,8 @@ func (gui *Gui) getListViews() []*listView {
context: "local-branches", context: "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 },
handleFocus: gui.wrappedHandler(gui.handleBranchSelect), handleFocus: gui.handleBranchSelect,
handleItemSelect: gui.wrappedHandler(gui.handleBranchSelect), handleItemSelect: gui.handleBranchSelect,
gui: gui, gui: gui,
rendersToMainView: true, rendersToMainView: true,
}, },
@ -142,9 +138,9 @@ func (gui *Gui) getListViews() []*listView {
context: "remotes", context: "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 },
handleFocus: gui.wrappedHandler(gui.renderRemotesWithSelection), handleFocus: gui.renderRemotesWithSelection,
handleItemSelect: gui.wrappedHandler(gui.handleRemoteSelect), handleItemSelect: gui.handleRemoteSelect,
handleClickSelectedItem: gui.wrappedHandler(gui.handleRemoteEnter), handleClickSelectedItem: gui.handleRemoteEnter,
gui: gui, gui: gui,
rendersToMainView: true, rendersToMainView: true,
}, },
@ -153,8 +149,8 @@ func (gui *Gui) getListViews() []*listView {
context: "remote-branches", context: "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 },
handleFocus: gui.wrappedHandler(gui.handleRemoteBranchSelect), handleFocus: gui.handleRemoteBranchSelect,
handleItemSelect: gui.wrappedHandler(gui.handleRemoteBranchSelect), handleItemSelect: gui.handleRemoteBranchSelect,
gui: gui, gui: gui,
rendersToMainView: true, rendersToMainView: true,
}, },
@ -163,8 +159,8 @@ func (gui *Gui) getListViews() []*listView {
context: "tags", context: "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 },
handleFocus: gui.wrappedHandler(gui.handleTagSelect), handleFocus: gui.handleTagSelect,
handleItemSelect: gui.wrappedHandler(gui.handleTagSelect), handleItemSelect: gui.handleTagSelect,
gui: gui, gui: gui,
rendersToMainView: true, rendersToMainView: true,
}, },
@ -174,9 +170,9 @@ func (gui *Gui) getListViews() []*listView {
context: "branch-commits", context: "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 },
handleFocus: gui.wrappedHandler(gui.handleCommitSelect), handleFocus: gui.handleCommitSelect,
handleItemSelect: gui.wrappedHandler(gui.handleCommitSelect), handleItemSelect: gui.handleCommitSelect,
handleClickSelectedItem: gui.wrappedHandler(gui.handleSwitchToCommitFilesPanel), handleClickSelectedItem: gui.handleSwitchToCommitFilesPanel,
gui: gui, gui: gui,
rendersToMainView: true, rendersToMainView: true,
}, },
@ -185,8 +181,8 @@ func (gui *Gui) getListViews() []*listView {
context: "reflog-commits", context: "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 },
handleFocus: gui.wrappedHandler(gui.handleReflogCommitSelect), handleFocus: gui.handleReflogCommitSelect,
handleItemSelect: gui.wrappedHandler(gui.handleReflogCommitSelect), handleItemSelect: gui.handleReflogCommitSelect,
gui: gui, gui: gui,
rendersToMainView: true, rendersToMainView: true,
}, },
@ -194,8 +190,8 @@ func (gui *Gui) getListViews() []*listView {
viewName: "stash", viewName: "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 },
handleFocus: gui.wrappedHandler(gui.handleStashEntrySelect), handleFocus: gui.handleStashEntrySelect,
handleItemSelect: gui.wrappedHandler(gui.handleStashEntrySelect), handleItemSelect: gui.handleStashEntrySelect,
gui: gui, gui: gui,
rendersToMainView: true, rendersToMainView: true,
}, },
@ -203,8 +199,8 @@ func (gui *Gui) getListViews() []*listView {
viewName: "commitFiles", viewName: "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 },
handleFocus: gui.wrappedHandler(gui.handleCommitFileSelect), handleFocus: gui.handleCommitFileSelect,
handleItemSelect: gui.wrappedHandler(gui.handleCommitFileSelect), handleItemSelect: gui.handleCommitFileSelect,
gui: gui, gui: gui,
rendersToMainView: true, rendersToMainView: true,
}, },