mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-05-11 04:15:48 +02:00
simplify how the context system works
This commit is contained in:
parent
e85310c0a9
commit
131113b065
11 changed files with 362 additions and 374 deletions
|
@ -29,6 +29,7 @@ func (gui *Gui) handleCommitFileSelect(g *gocui.Gui, v *gocui.View) error {
|
|||
}
|
||||
|
||||
gui.getMainView().Title = "Patch"
|
||||
gui.State.Panels.LineByLine = nil
|
||||
|
||||
commitFile := gui.getSelectedCommitFile(g)
|
||||
if commitFile == nil {
|
||||
|
@ -207,10 +208,7 @@ func (gui *Gui) enterCommitFile(selectedLineIdx int) error {
|
|||
}
|
||||
}
|
||||
|
||||
if err := gui.changeContext("main", "patch-building"); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := gui.changeContext("secondary", "patch-building"); err != nil {
|
||||
if err := gui.changeContext("patch-building"); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := gui.switchFocus(gui.g, gui.getCommitFilesView(), gui.getMainView()); err != nil {
|
||||
|
|
|
@ -47,6 +47,7 @@ func (gui *Gui) handleCommitSelect(g *gocui.Gui, v *gocui.View) error {
|
|||
|
||||
gui.getMainView().Title = "Patch"
|
||||
gui.getSecondaryView().Title = "Custom Patch"
|
||||
gui.State.Panels.LineByLine = nil
|
||||
|
||||
commit := gui.getSelectedCommit(g)
|
||||
if commit == nil {
|
||||
|
@ -97,7 +98,7 @@ func (gui *Gui) refreshCommits(g *gocui.Gui) error {
|
|||
if g.CurrentView() == v {
|
||||
gui.handleCommitSelect(g, v)
|
||||
}
|
||||
if g.CurrentView() == gui.getCommitFilesView() || (g.CurrentView() == gui.getMainView() || gui.State.Contexts["main"] == "patch-building") {
|
||||
if g.CurrentView() == gui.getCommitFilesView() || (g.CurrentView() == gui.getMainView() || gui.State.Context == "patch-building") {
|
||||
return gui.refreshCommitFilesView()
|
||||
}
|
||||
return nil
|
||||
|
|
|
@ -1,42 +1,45 @@
|
|||
package gui
|
||||
|
||||
func (gui *Gui) changeContext(viewName, context string) error {
|
||||
if gui.State.Contexts[viewName] == context {
|
||||
func (gui *Gui) changeContext(context string) error {
|
||||
oldContext := gui.State.Context
|
||||
|
||||
if gui.State.Context == context {
|
||||
return nil
|
||||
}
|
||||
|
||||
contextMap := gui.GetContextMap()
|
||||
|
||||
gui.g.DeleteKeybindings(viewName)
|
||||
oldBindings := contextMap[oldContext]
|
||||
for _, binding := range oldBindings {
|
||||
if err := gui.g.DeleteKeybinding(binding.ViewName, binding.Key, binding.Modifier); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
bindings := contextMap[viewName][context]
|
||||
bindings := contextMap[context]
|
||||
for _, binding := range bindings {
|
||||
if err := gui.g.SetKeybinding(binding.ViewName, binding.Key, binding.Modifier, binding.Handler); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
gui.State.Contexts[viewName] = context
|
||||
|
||||
gui.State.Context = context
|
||||
return nil
|
||||
}
|
||||
|
||||
func (gui *Gui) setInitialContexts() error {
|
||||
func (gui *Gui) setInitialContext() error {
|
||||
contextMap := gui.GetContextMap()
|
||||
|
||||
initialContexts := map[string]string{
|
||||
"main": "normal",
|
||||
"secondary": "normal",
|
||||
}
|
||||
initialContext := "normal"
|
||||
|
||||
for viewName, context := range initialContexts {
|
||||
bindings := contextMap[viewName][context]
|
||||
bindings := contextMap[initialContext]
|
||||
for _, binding := range bindings {
|
||||
if err := gui.g.SetKeybinding(binding.ViewName, binding.Key, binding.Modifier, binding.Handler); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
gui.State.Contexts = initialContexts
|
||||
gui.State.Context = initialContext
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -207,10 +207,7 @@ func (gui *Gui) enterFile(forceSecondaryFocused bool, selectedLineIdx int) error
|
|||
if file.HasMergeConflicts {
|
||||
return gui.createErrorPanel(gui.g, gui.Tr.SLocalize("FileStagingRequirements"))
|
||||
}
|
||||
if err := gui.changeContext("main", "staging"); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := gui.changeContext("secondary", "staging"); err != nil {
|
||||
if err := gui.changeContext("staging"); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := gui.switchFocus(gui.g, gui.getFilesView(), gui.getMainView()); err != nil {
|
||||
|
@ -466,7 +463,7 @@ func (gui *Gui) handleSwitchToMerge(g *gocui.Gui, v *gocui.View) error {
|
|||
if !file.HasInlineMergeConflicts {
|
||||
return gui.createErrorPanel(g, gui.Tr.SLocalize("FileNoMergeCons"))
|
||||
}
|
||||
if err := gui.changeContext("main", "merging"); err != nil {
|
||||
if err := gui.changeContext("merging"); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := gui.switchFocus(g, v, gui.getMainView()); err != nil {
|
||||
|
|
|
@ -155,7 +155,7 @@ type guiState struct {
|
|||
Updating bool
|
||||
Panels *panelStates
|
||||
WorkingTreeState string // one of "merging", "rebasing", "normal"
|
||||
Contexts map[string]string
|
||||
Context string // important not to set this value directly but to use gui.changeContext("new context")
|
||||
CherryPickedCommits []*commands.Commit
|
||||
SplitMainPanel bool
|
||||
}
|
||||
|
@ -281,11 +281,11 @@ func (gui *Gui) onFocusLost(v *gocui.View, newView *gocui.View) error {
|
|||
}
|
||||
case "main":
|
||||
// if we have lost focus to a first-class panel, we need to do some cleanup
|
||||
if err := gui.changeContext("main", "normal"); err != nil {
|
||||
if err := gui.changeContext("normal"); err != nil {
|
||||
return err
|
||||
}
|
||||
case "commitFiles":
|
||||
if gui.State.Contexts["main"] != "patch-building" {
|
||||
if gui.State.Context != "patch-building" {
|
||||
if _, err := gui.g.SetViewOnBottom(v.Name()); err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -592,9 +592,8 @@ func (gui *Gui) GetInitialKeybindings() []*Binding {
|
|||
// GetCurrentKeybindings gets the list of keybindings given the current context
|
||||
func (gui *Gui) GetCurrentKeybindings() []*Binding {
|
||||
bindings := gui.GetInitialKeybindings()
|
||||
viewName := gui.currentViewName()
|
||||
currentContext := gui.State.Contexts[viewName]
|
||||
contextBindings := gui.GetContextMap()[viewName][currentContext]
|
||||
currentContext := gui.State.Context
|
||||
contextBindings := gui.GetContextMap()[currentContext]
|
||||
|
||||
return append(bindings, contextBindings...)
|
||||
}
|
||||
|
@ -607,15 +606,14 @@ func (gui *Gui) keybindings(g *gocui.Gui) error {
|
|||
return err
|
||||
}
|
||||
}
|
||||
if err := gui.setInitialContexts(); err != nil {
|
||||
if err := gui.setInitialContext(); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (gui *Gui) GetContextMap() map[string]map[string][]*Binding {
|
||||
return map[string]map[string][]*Binding{
|
||||
"secondary": {
|
||||
func (gui *Gui) GetContextMap() map[string][]*Binding {
|
||||
return map[string][]*Binding{
|
||||
"normal": {
|
||||
{
|
||||
ViewName: "secondary",
|
||||
|
@ -623,18 +621,6 @@ func (gui *Gui) GetContextMap() map[string]map[string][]*Binding {
|
|||
Modifier: gocui.ModNone,
|
||||
Handler: gui.handleMouseDownSecondary,
|
||||
},
|
||||
},
|
||||
"staging": {
|
||||
{
|
||||
ViewName: "secondary",
|
||||
Key: gocui.MouseLeft,
|
||||
Modifier: gocui.ModNone,
|
||||
Handler: gui.handleTogglePanelClick,
|
||||
},
|
||||
},
|
||||
},
|
||||
"main": {
|
||||
"normal": {
|
||||
{
|
||||
ViewName: "main",
|
||||
Key: gocui.MouseWheelDown,
|
||||
|
@ -657,6 +643,12 @@ func (gui *Gui) GetContextMap() map[string]map[string][]*Binding {
|
|||
},
|
||||
},
|
||||
"staging": {
|
||||
{
|
||||
ViewName: "secondary",
|
||||
Key: gocui.MouseLeft,
|
||||
Modifier: gocui.ModNone,
|
||||
Handler: gui.handleTogglePanelClick,
|
||||
},
|
||||
{
|
||||
ViewName: "main",
|
||||
Key: gocui.KeyEsc,
|
||||
|
@ -949,6 +941,5 @@ func (gui *Gui) GetContextMap() map[string]map[string][]*Binding {
|
|||
Description: gui.Tr.SLocalize("Undo"),
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
|
@ -226,7 +226,7 @@ func (gui *Gui) refreshMainView() error {
|
|||
var includedLineIndices []int
|
||||
// I'd prefer not to have knowledge of contexts using this file but I'm not sure
|
||||
// how to get around this
|
||||
if gui.State.Contexts["main"] == "patch-building" {
|
||||
if gui.State.Context == "patch-building" {
|
||||
filename := gui.State.CommitFiles[gui.State.Panels.CommitFiles.SelectedLine].Name
|
||||
includedLineIndices = gui.GitCommand.PatchManager.GetFileIncLineIndices(filename)
|
||||
}
|
||||
|
|
|
@ -88,7 +88,7 @@ func (gui *Gui) handleRemoveSelectionFromPatch(g *gocui.Gui, v *gocui.View) erro
|
|||
|
||||
func (gui *Gui) handleEscapePatchBuildingPanel(g *gocui.Gui, v *gocui.View) error {
|
||||
gui.State.Panels.LineByLine = nil
|
||||
gui.State.Contexts["main"] = "normal"
|
||||
gui.changeContext("normal")
|
||||
|
||||
if gui.GitCommand.PatchManager.IsEmpty() {
|
||||
gui.GitCommand.PatchManager.Reset()
|
||||
|
|
|
@ -67,7 +67,7 @@ func (gui *Gui) validateNormalWorkingTreeState() (bool, error) {
|
|||
}
|
||||
|
||||
func (gui *Gui) returnFocusFromLineByLinePanelIfNecessary() error {
|
||||
if gui.State.Contexts["main"] == "patch-building" {
|
||||
if gui.State.Context == "patch-building" {
|
||||
return gui.handleEscapePatchBuildingPanel(gui.g, nil)
|
||||
}
|
||||
return nil
|
||||
|
|
|
@ -117,7 +117,7 @@ func (gui *Gui) newLineFocused(g *gocui.Gui, v *gocui.View) error {
|
|||
case "credentials":
|
||||
return gui.handleCredentialsViewFocused(g, v)
|
||||
case "main":
|
||||
if gui.State.Contexts["main"] == "merging" {
|
||||
if gui.State.Context == "merging" {
|
||||
return gui.refreshMergePanel()
|
||||
}
|
||||
v.Highlight = false
|
||||
|
@ -406,7 +406,7 @@ func (gui *Gui) renderPanelOptions() error {
|
|||
case "menu":
|
||||
return gui.renderMenuOptions()
|
||||
case "main":
|
||||
if gui.State.Contexts["main"] == "merging" {
|
||||
if gui.State.Context == "merging" {
|
||||
return gui.renderMergeOptions()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -83,9 +83,8 @@ func getBindingSections(mApp *app.App) []*bindingSection {
|
|||
bindingSections = addBinding(title, bindingSections, binding)
|
||||
}
|
||||
|
||||
for view, contexts := range mApp.Gui.GetContextMap() {
|
||||
for contextName, contextBindings := range contexts {
|
||||
translatedView := localisedTitle(mApp, view)
|
||||
for contextName, contextBindings := range mApp.Gui.GetContextMap() {
|
||||
translatedView := localisedTitle(mApp, contextBindings[0].ViewName)
|
||||
translatedContextName := localisedTitle(mApp, contextName)
|
||||
title := fmt.Sprintf("%s (%s)", translatedView, translatedContextName)
|
||||
|
||||
|
@ -93,7 +92,6 @@ func getBindingSections(mApp *app.App) []*bindingSection {
|
|||
bindingSections = addBinding(title, bindingSections, binding)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return bindingSections
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue