1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-12 11:15:00 +02:00
lazygit/pkg/gui/context/context.go

192 lines
4.9 KiB
Go
Raw Normal View History

package context
2022-02-05 08:04:10 +02:00
import (
"sync"
"github.com/jesseduffield/lazygit/pkg/gui/types"
)
2022-01-29 10:15:46 +02:00
const (
2022-02-05 05:42:56 +02:00
GLOBAL_CONTEXT_KEY types.ContextKey = "global"
2022-01-29 10:15:46 +02:00
STATUS_CONTEXT_KEY types.ContextKey = "status"
FILES_CONTEXT_KEY types.ContextKey = "files"
LOCAL_BRANCHES_CONTEXT_KEY types.ContextKey = "localBranches"
REMOTES_CONTEXT_KEY types.ContextKey = "remotes"
REMOTE_BRANCHES_CONTEXT_KEY types.ContextKey = "remoteBranches"
TAGS_CONTEXT_KEY types.ContextKey = "tags"
2022-02-13 08:01:53 +02:00
LOCAL_COMMITS_CONTEXT_KEY types.ContextKey = "commits"
2022-01-29 10:15:46 +02:00
REFLOG_COMMITS_CONTEXT_KEY types.ContextKey = "reflogCommits"
SUB_COMMITS_CONTEXT_KEY types.ContextKey = "subCommits"
COMMIT_FILES_CONTEXT_KEY types.ContextKey = "commitFiles"
STASH_CONTEXT_KEY types.ContextKey = "stash"
MAIN_NORMAL_CONTEXT_KEY types.ContextKey = "normal"
MAIN_MERGING_CONTEXT_KEY types.ContextKey = "merging"
MAIN_PATCH_BUILDING_CONTEXT_KEY types.ContextKey = "patchBuilding"
MAIN_STAGING_CONTEXT_KEY types.ContextKey = "staging"
MENU_CONTEXT_KEY types.ContextKey = "menu"
CONFIRMATION_CONTEXT_KEY types.ContextKey = "confirmation"
SEARCH_CONTEXT_KEY types.ContextKey = "search"
COMMIT_MESSAGE_CONTEXT_KEY types.ContextKey = "commitMessage"
SUBMODULES_CONTEXT_KEY types.ContextKey = "submodules"
SUGGESTIONS_CONTEXT_KEY types.ContextKey = "suggestions"
COMMAND_LOG_CONTEXT_KEY types.ContextKey = "cmdLog"
)
var AllContextKeys = []types.ContextKey{
2022-02-05 07:56:36 +02:00
GLOBAL_CONTEXT_KEY, // not focusable
2022-01-29 10:15:46 +02:00
STATUS_CONTEXT_KEY,
FILES_CONTEXT_KEY,
LOCAL_BRANCHES_CONTEXT_KEY,
REMOTES_CONTEXT_KEY,
REMOTE_BRANCHES_CONTEXT_KEY,
TAGS_CONTEXT_KEY,
2022-02-13 08:01:53 +02:00
LOCAL_COMMITS_CONTEXT_KEY,
2022-01-29 10:15:46 +02:00
REFLOG_COMMITS_CONTEXT_KEY,
SUB_COMMITS_CONTEXT_KEY,
COMMIT_FILES_CONTEXT_KEY,
STASH_CONTEXT_KEY,
2022-02-05 07:56:36 +02:00
MAIN_NORMAL_CONTEXT_KEY, // not focusable
2022-01-29 10:15:46 +02:00
MAIN_MERGING_CONTEXT_KEY,
MAIN_PATCH_BUILDING_CONTEXT_KEY,
2022-02-05 07:56:36 +02:00
MAIN_STAGING_CONTEXT_KEY, // not focusable for secondary view
2022-01-29 10:15:46 +02:00
MENU_CONTEXT_KEY,
CONFIRMATION_CONTEXT_KEY,
SEARCH_CONTEXT_KEY,
COMMIT_MESSAGE_CONTEXT_KEY,
SUBMODULES_CONTEXT_KEY,
SUGGESTIONS_CONTEXT_KEY,
COMMAND_LOG_CONTEXT_KEY,
}
type ContextTree struct {
2022-02-05 05:42:56 +02:00
Global types.Context
Status types.Context
2022-01-30 04:08:09 +02:00
Files *WorkingTreeContext
2022-02-05 08:04:10 +02:00
Menu *MenuContext
Branches *BranchesContext
2022-01-29 10:09:20 +02:00
Tags *TagsContext
2022-02-13 08:01:53 +02:00
LocalCommits *LocalCommitsContext
CommitFiles *CommitFilesContext
2022-02-05 08:04:10 +02:00
Remotes *RemotesContext
Submodules *SubmodulesContext
RemoteBranches *RemoteBranchesContext
ReflogCommits *ReflogCommitsContext
SubCommits *SubCommitsContext
Stash *StashContext
Suggestions *SuggestionsContext
Normal types.Context
Staging types.Context
PatchBuilding types.Context
Merging types.Context
Confirmation types.Context
CommitMessage types.Context
Search types.Context
CommandLog types.Context
}
2022-02-05 07:56:36 +02:00
func (self *ContextTree) Flatten() []types.Context {
return []types.Context{
self.Global,
self.Status,
self.Files,
self.Submodules,
self.Branches,
self.Remotes,
self.RemoteBranches,
self.Tags,
2022-02-13 08:01:53 +02:00
self.LocalCommits,
2022-02-05 07:56:36 +02:00
self.CommitFiles,
self.ReflogCommits,
self.Stash,
self.Menu,
self.Confirmation,
self.CommitMessage,
self.Normal,
self.Staging,
self.Merging,
self.PatchBuilding,
self.SubCommits,
self.Suggestions,
self.CommandLog,
}
}
2022-02-05 07:56:36 +02:00
type ViewContextMap struct {
content map[string]types.Context
2022-02-05 08:04:10 +02:00
sync.RWMutex
2022-02-05 07:56:36 +02:00
}
func NewViewContextMap() *ViewContextMap {
return &ViewContextMap{content: map[string]types.Context{}}
}
func (self *ViewContextMap) Get(viewName string) types.Context {
2022-02-05 08:04:10 +02:00
self.RLock()
defer self.RUnlock()
2022-02-05 07:56:36 +02:00
return self.content[viewName]
}
func (self *ViewContextMap) Set(viewName string, context types.Context) {
2022-02-05 08:04:10 +02:00
self.Lock()
defer self.Unlock()
2022-02-05 07:56:36 +02:00
self.content[viewName] = context
}
func (self *ViewContextMap) Entries() map[string]types.Context {
self.Lock()
defer self.Unlock()
return self.content
}
type TabContext struct {
Tab string
Contexts []types.Context
}
func (tree ContextTree) InitialViewTabContextMap() map[string][]TabContext {
return map[string][]TabContext{
"branches": {
{
Tab: "Local Branches",
Contexts: []types.Context{tree.Branches},
},
{
Tab: "Remotes",
Contexts: []types.Context{
tree.Remotes,
tree.RemoteBranches,
},
},
{
Tab: "Tags",
Contexts: []types.Context{tree.Tags},
},
},
"commits": {
{
Tab: "Commits",
2022-02-13 08:01:53 +02:00
Contexts: []types.Context{tree.LocalCommits},
},
{
Tab: "Reflog",
Contexts: []types.Context{
tree.ReflogCommits,
},
},
},
"files": {
{
Tab: "Files",
Contexts: []types.Context{tree.Files},
},
{
Tab: "Submodules",
Contexts: []types.Context{
tree.Submodules,
},
},
},
}
}