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

refactor to get view tab context map into gui state

This commit is contained in:
Jesse Duffield 2021-04-03 14:36:22 +11:00
parent c527ee9984
commit 56fd21d859
3 changed files with 27 additions and 12 deletions

View File

@ -694,7 +694,7 @@ func (gui *Gui) changeMainViewsContext(contextKey string) {
} }
func (gui *Gui) viewTabNames(viewName string) []string { func (gui *Gui) viewTabNames(viewName string) []string {
tabContexts := gui.ViewTabContextMap[viewName] tabContexts := gui.State.ViewTabContextMap[viewName]
if len(tabContexts) == 0 { if len(tabContexts) == 0 {
return nil return nil
@ -710,7 +710,7 @@ func (gui *Gui) viewTabNames(viewName string) []string {
func (gui *Gui) setViewTabForContext(c Context) { 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 // 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 { if !ok {
return return
} }

View File

@ -60,6 +60,8 @@ func NewContextManager(contexts ContextTree) ContextManager {
} }
} }
type Repo string
// Gui wraps the gocui Gui object which handles rendering and events // Gui wraps the gocui Gui object which handles rendering and events
type Gui struct { type Gui struct {
g *gocui.Gui g *gocui.Gui
@ -68,6 +70,7 @@ type Gui struct {
OSCommand *oscommands.OSCommand OSCommand *oscommands.OSCommand
SubProcess *exec.Cmd SubProcess *exec.Cmd
State *guiState State *guiState
RepoStateMap map[Repo]*guiState
Config config.AppConfigurer Config config.AppConfigurer
Tr *i18n.TranslationSet Tr *i18n.TranslationSet
Errors SentinelErrors Errors SentinelErrors
@ -81,9 +84,8 @@ type Gui struct {
// when lazygit is opened outside a git directory we want to open to the most // when lazygit is opened outside a git directory we want to open to the most
// recent repo with the recent repos popup showing // recent repo with the recent repos popup showing
showRecentRepos bool showRecentRepos bool
Contexts ContextTree Contexts ContextTree
ViewTabContextMap map[string][]tabContext
// this array either includes the events that we're recording in this session // this array either includes the events that we're recording in this session
// or the events we've recorded in a prior session // or the events we've recorded in a prior session
@ -314,8 +316,9 @@ type guiState struct {
Modes Modes Modes Modes
ContextManager ContextManager ContextManager ContextManager
ViewContextMap map[string]Context ViewContextMap map[string]Context
ViewTabContextMap map[string][]tabContext
// WindowViewNameMap is a mapping of windows to the current view of that window. // 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 // 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) { 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 showTree := gui.Config.GetUserConfig().Gui.ShowFileTree
screenMode := SCREEN_NORMAL screenMode := SCREEN_NORMAL
@ -374,12 +387,13 @@ func (gui *Gui) resetState(filterPath string) {
}, },
Diffing: Diffing{}, Diffing: Diffing{},
}, },
ViewContextMap: gui.initialViewContextMap(), ViewContextMap: gui.initialViewContextMap(),
ScreenMode: screenMode, ViewTabContextMap: gui.initialViewTabContextMap(),
ContextManager: NewContextManager(gui.Contexts), 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 // 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, showRecentRepos: showRecentRepos,
RecordedEvents: []RecordedEvent{}, RecordedEvents: []RecordedEvent{},
RepoPathStack: []string{}, RepoPathStack: []string{},
RepoStateMap: map[Repo]*guiState{},
} }
gui.Contexts = gui.contextTree() gui.Contexts = gui.contextTree()

View File

@ -431,7 +431,7 @@ func (gui *Gui) clearEditorView(v *gocui.View) {
} }
func (gui *Gui) onViewTabClick(viewName string, tabIndex int) error { 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) return gui.pushContext(context)
} }