From fafd5234bd7be6916e00712a3a138bf2ee92e99b Mon Sep 17 00:00:00 2001
From: Jesse Duffield <jessedduffield@gmail.com>
Date: Sat, 3 Apr 2021 14:36:22 +1100
Subject: [PATCH] refactor to get view tab context map into gui state

---
 pkg/gui/context.go      |  4 ++--
 pkg/gui/gui.go          | 33 ++++++++++++++++++++++++---------
 pkg/gui/view_helpers.go |  2 +-
 3 files changed, 27 insertions(+), 12 deletions(-)

diff --git a/pkg/gui/context.go b/pkg/gui/context.go
index 6d8492411..82c89f535 100644
--- a/pkg/gui/context.go
+++ b/pkg/gui/context.go
@@ -694,7 +694,7 @@ func (gui *Gui) changeMainViewsContext(contextKey string) {
 }
 
 func (gui *Gui) viewTabNames(viewName string) []string {
-	tabContexts := gui.ViewTabContextMap[viewName]
+	tabContexts := gui.State.ViewTabContextMap[viewName]
 
 	if len(tabContexts) == 0 {
 		return nil
@@ -710,7 +710,7 @@ func (gui *Gui) viewTabNames(viewName string) []string {
 
 func (gui *Gui) setViewTabForContext(c Context) {
 	// search for the context in our map and if we find it, set the tab for the corresponding view
-	tabContexts, ok := gui.ViewTabContextMap[c.GetViewName()]
+	tabContexts, ok := gui.State.ViewTabContextMap[c.GetViewName()]
 	if !ok {
 		return
 	}
diff --git a/pkg/gui/gui.go b/pkg/gui/gui.go
index 0a7770a01..dd2b38c26 100644
--- a/pkg/gui/gui.go
+++ b/pkg/gui/gui.go
@@ -60,6 +60,8 @@ func NewContextManager(contexts ContextTree) ContextManager {
 	}
 }
 
+type Repo string
+
 // Gui wraps the gocui Gui object which handles rendering and events
 type Gui struct {
 	g                    *gocui.Gui
@@ -68,6 +70,7 @@ type Gui struct {
 	OSCommand            *oscommands.OSCommand
 	SubProcess           *exec.Cmd
 	State                *guiState
+	RepoStateMap         map[Repo]*guiState
 	Config               config.AppConfigurer
 	Tr                   *i18n.TranslationSet
 	Errors               SentinelErrors
@@ -81,9 +84,8 @@ type Gui struct {
 
 	// when lazygit is opened outside a git directory we want to open to the most
 	// recent repo with the recent repos popup showing
-	showRecentRepos   bool
-	Contexts          ContextTree
-	ViewTabContextMap map[string][]tabContext
+	showRecentRepos bool
+	Contexts        ContextTree
 
 	// this array either includes the events that we're recording in this session
 	// or the events we've recorded in a prior session
@@ -314,8 +316,9 @@ type guiState struct {
 
 	Modes Modes
 
-	ContextManager ContextManager
-	ViewContextMap map[string]Context
+	ContextManager    ContextManager
+	ViewContextMap    map[string]Context
+	ViewTabContextMap map[string][]tabContext
 
 	// WindowViewNameMap is a mapping of windows to the current view of that window.
 	// Some views move between windows for example the commitFiles view and when cycling through
@@ -327,6 +330,16 @@ type guiState struct {
 }
 
 func (gui *Gui) resetState(filterPath string) {
+	currentDir, err := os.Getwd()
+	if err == nil {
+		if state := gui.RepoStateMap[Repo(currentDir)]; state != nil {
+			gui.State = state
+			return
+		}
+	} else {
+		gui.Log.Error(err)
+	}
+
 	showTree := gui.Config.GetUserConfig().Gui.ShowFileTree
 
 	screenMode := SCREEN_NORMAL
@@ -374,12 +387,13 @@ func (gui *Gui) resetState(filterPath string) {
 			},
 			Diffing: Diffing{},
 		},
-		ViewContextMap: gui.initialViewContextMap(),
-		ScreenMode:     screenMode,
-		ContextManager: NewContextManager(gui.Contexts),
+		ViewContextMap:    gui.initialViewContextMap(),
+		ViewTabContextMap: gui.initialViewTabContextMap(),
+		ScreenMode:        screenMode,
+		ContextManager:    NewContextManager(gui.Contexts),
 	}
 
-	gui.ViewTabContextMap = gui.initialViewTabContextMap()
+	gui.RepoStateMap[Repo(gui.GitCommand.DotGitDir)] = gui.State
 }
 
 // for now the split view will always be on
@@ -397,6 +411,7 @@ func NewGui(log *logrus.Entry, gitCommand *commands.GitCommand, oSCommand *oscom
 		showRecentRepos:      showRecentRepos,
 		RecordedEvents:       []RecordedEvent{},
 		RepoPathStack:        []string{},
+		RepoStateMap:         map[Repo]*guiState{},
 	}
 
 	gui.Contexts = gui.contextTree()
diff --git a/pkg/gui/view_helpers.go b/pkg/gui/view_helpers.go
index 3afbf7b09..6fc5fda04 100644
--- a/pkg/gui/view_helpers.go
+++ b/pkg/gui/view_helpers.go
@@ -431,7 +431,7 @@ func (gui *Gui) clearEditorView(v *gocui.View) {
 }
 
 func (gui *Gui) onViewTabClick(viewName string, tabIndex int) error {
-	context := gui.ViewTabContextMap[viewName][tabIndex].contexts[0]
+	context := gui.State.ViewTabContextMap[viewName][tabIndex].contexts[0]
 
 	return gui.pushContext(context)
 }