1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-08-13 20:04:25 +02:00

stop crash due to context stack not being initialized

This commit is contained in:
Jesse Duffield
2020-08-19 08:26:22 +10:00
parent f0c3d3fc4d
commit 9a2dc3fe15
5 changed files with 23 additions and 12 deletions

View File

@@ -96,7 +96,7 @@ func (gui *Gui) refreshCommits() error {
go func() { go func() {
_ = gui.refreshCommitsWithLimit() _ = gui.refreshCommitsWithLimit()
if gui.g.CurrentView() == gui.getCommitFilesView() || (gui.currentContext().GetKey() == gui.Contexts.PatchBuilding.Context.GetKey()) { if gui.g.CurrentView() == gui.getCommitFilesView() || (gui.currentContextKey() == gui.Contexts.PatchBuilding.Context.GetKey()) {
_ = gui.refreshCommitFilesView() _ = gui.refreshCommitFilesView()
} }
wg.Done() wg.Done()

View File

@@ -208,12 +208,17 @@ func (gui *Gui) renderContextStack() string {
return result return result
} }
func (gui *Gui) currentContext() Context { func (gui *Gui) currentContextKey() string {
return gui.State.ContextStack[len(gui.State.ContextStack)-1] // on startup the stack can be empty so we'll return an empty string in that case
if len(gui.State.ContextStack) == 0 {
return ""
}
return gui.State.ContextStack[len(gui.State.ContextStack)-1].GetKey()
} }
func (gui *Gui) createContextTree() { func (gui *Gui) contextTree() ContextTree {
gui.Contexts = ContextTree{ return ContextTree{
Status: SimpleContextNode{ Status: SimpleContextNode{
Context: BasicContext{ Context: BasicContext{
OnFocus: gui.handleStatusSelect, OnFocus: gui.handleStatusSelect,
@@ -329,8 +334,10 @@ func (gui *Gui) createContextTree() {
}, },
}, },
} }
}
gui.State.ViewContextMap = map[string]Context{ func (gui *Gui) initialViewContextMap() map[string]Context {
return map[string]Context{
"status": gui.Contexts.Status.Context, "status": gui.Contexts.Status.Context,
"files": gui.Contexts.Files.Context, "files": gui.Contexts.Files.Context,
"branches": gui.Contexts.Branches.Context, "branches": gui.Contexts.Branches.Context,
@@ -344,7 +351,9 @@ func (gui *Gui) createContextTree() {
"main": gui.Contexts.Normal.Context, "main": gui.Contexts.Normal.Context,
"secondary": gui.Contexts.Normal.Context, "secondary": gui.Contexts.Normal.Context,
} }
}
func (gui *Gui) setInitialViewContexts() {
// arguably we should only have our ViewContextMap and we should do away with // arguably we should only have our ViewContextMap and we should do away with
// contexts on views, or vice versa // contexts on views, or vice versa
for viewName, context := range gui.State.ViewContextMap { for viewName, context := range gui.State.ViewContextMap {

View File

@@ -286,6 +286,7 @@ func (gui *Gui) resetState() {
Ptmx: nil, Ptmx: nil,
FilterPath: prevFilterPath, FilterPath: prevFilterPath,
Diff: prevDiff, Diff: prevDiff,
ViewContextMap: gui.initialViewContextMap(),
} }
} }
@@ -306,6 +307,7 @@ func NewGui(log *logrus.Entry, gitCommand *commands.GitCommand, oSCommand *comma
gui.resetState() gui.resetState()
gui.State.FilterPath = filterPath gui.State.FilterPath = filterPath
gui.Contexts = gui.contextTree()
gui.watchFilesForChanges() gui.watchFilesForChanges()

View File

@@ -337,7 +337,7 @@ func (gui *Gui) layout(g *gocui.Gui) error {
} }
func (gui *Gui) onInitialViewsCreation() error { func (gui *Gui) onInitialViewsCreation() error {
gui.createContextTree() gui.setInitialViewContexts()
if err := gui.switchContext(gui.Contexts.Files.Context); err != nil { if err := gui.switchContext(gui.Contexts.Files.Context); err != nil {
return err return err

View File

@@ -234,7 +234,7 @@ func (gui *Gui) refreshMainView() error {
var includedLineIndices []int var includedLineIndices []int
// I'd prefer not to have knowledge of contexts using this file but I'm not sure // I'd prefer not to have knowledge of contexts using this file but I'm not sure
// how to get around this // how to get around this
if gui.currentContext().GetKey() == gui.Contexts.PatchBuilding.Context.GetKey() { if gui.currentContextKey() == gui.Contexts.PatchBuilding.Context.GetKey() {
filename := gui.getSelectedCommitFileName() filename := gui.getSelectedCommitFileName()
includedLineIndices = gui.GitCommand.PatchManager.GetFileIncLineIndices(filename) includedLineIndices = gui.GitCommand.PatchManager.GetFileIncLineIndices(filename)
} }