diff options
-rw-r--r-- | pkg/gui/branches_panel.go | 39 | ||||
-rw-r--r-- | pkg/gui/diffing.go | 28 | ||||
-rw-r--r-- | pkg/gui/main_panels.go | 69 | ||||
-rw-r--r-- | pkg/gui/reflog_panel.go | 30 | ||||
-rw-r--r-- | pkg/gui/view_helpers.go | 13 |
5 files changed, 122 insertions, 57 deletions
diff --git a/pkg/gui/branches_panel.go b/pkg/gui/branches_panel.go index 1cb1ba5fa..b856b4007 100644 --- a/pkg/gui/branches_panel.go +++ b/pkg/gui/branches_panel.go @@ -13,6 +13,10 @@ import ( // list panel functions func (gui *Gui) getSelectedBranch() *commands.Branch { + if len(gui.State.Branches) == 0 { + return nil + } + selectedLine := gui.State.Panels.Branches.SelectedLine if selectedLine == -1 { return nil @@ -26,27 +30,32 @@ func (gui *Gui) handleBranchSelect() error { return nil } - gui.splitMainPanel(false) - - gui.getMainView().Title = "Log" + if gui.inDiffMode() { + return gui.renderDiff() + } - // This really shouldn't happen: there should always be a master branch - if len(gui.State.Branches) == 0 { - return gui.newStringTask("main", gui.Tr.SLocalize("NoBranchesThisRepo")) + refreshOpts := refreshMainOpts{ + main: &viewUpdateOpts{ + title: "Log", + task: { + kind: RENDER_STRING, + str: gui.Tr.SLocalize("NoBranchesThisRepo"), + }, + }, } + branch := gui.getSelectedBranch() + if branch == nil { + refreshOpts.main.task = func() error { return gui.newStringTask("main", gui.Tr.SLocalize("NoBranchesThisRepo")) } + } else { + cmd := gui.OSCommand.ExecutableFromString( + gui.GitCommand.GetBranchGraphCmdStr(branch.Name), + ) - if gui.inDiffMode() { - return gui.renderDiff() + refreshOpts.main.task = func() error { return gui.newPtyTask("main", cmd) } } - cmd := gui.OSCommand.ExecutableFromString( - gui.GitCommand.GetBranchGraphCmdStr(branch.Name), - ) - if err := gui.newCmdTask("main", cmd); err != nil { - gui.Log.Error(err) - } - return nil + return gui.refreshMain(refreshOpts) } // gui.refreshStatus is called at the end of this because that's when we can diff --git a/pkg/gui/diffing.go b/pkg/gui/diffing.go index 7418cf3aa..4e88f2e8b 100644 --- a/pkg/gui/diffing.go +++ b/pkg/gui/diffing.go @@ -18,20 +18,22 @@ func (gui *Gui) exitDiffMode() error { } func (gui *Gui) renderDiff() error { - gui.getMainView().Title = "Diff" - gui.splitMainPanel(false) - filterArg := "" - if gui.inFilterMode() { - filterArg = fmt.Sprintf(" -- %s", gui.State.FilterPath) - } + return gui.refreshMain(refreshMainOpts{ + main: &viewUpdateOpts{ + title: "Diff", + task: func() error { + filterArg := "" + if gui.inFilterMode() { + filterArg = fmt.Sprintf(" -- %s", gui.State.FilterPath) + } - cmd := gui.OSCommand.ExecutableFromString( - fmt.Sprintf("git diff --color %s %s", gui.diffStr(), filterArg), - ) - if err := gui.newPtyTask("main", cmd); err != nil { - gui.Log.Error(err) - } - return nil + cmd := gui.OSCommand.ExecutableFromString( + fmt.Sprintf("git diff --color %s %s", gui.diffStr(), filterArg), + ) + return gui.newPtyTask("main", cmd) + }, + }, + }) } // currentDiffTerminals returns the current diff terminals of the currently selected item. diff --git a/pkg/gui/main_panels.go b/pkg/gui/main_panels.go new file mode 100644 index 000000000..af6718c60 --- /dev/null +++ b/pkg/gui/main_panels.go @@ -0,0 +1,69 @@ +package gui + +import "os/exec" + +type viewUpdateOpts struct { + title string + task func() error +} + +type refreshMainOpts struct { + main *viewUpdateOpts + secondary *viewUpdateOpts +} + +// constants for updateTask's kind field +const ( + RENDER_STRING = iota + RUN_FUNCTION + RUN_COMMAND +) + +type updateTask struct { + kind int + str string + f func(chan struct{}) error + cmd *exec.Cmd +} + +func (gui *Gui) createRenderStringTask(str string) { + +} + +func (gui *Gui) refreshMain(opts refreshMainOpts) error { + mainView := gui.getMainView() + secondaryView := gui.getSecondaryView() + + if opts.main != nil { + mainView.Title = opts.main.title + if err := opts.main.task(); err != nil { + gui.Log.Error(err) + return nil + } + } + + gui.splitMainPanel(opts.secondary != nil) + + if opts.secondary != nil { + secondaryView.Title = opts.secondary.title + if err := opts.secondary.task(); err != nil { + gui.Log.Error(err) + return nil + } + } + + return nil +} + +func (gui *Gui) splitMainPanel(splitMainPanel bool) { + gui.State.SplitMainPanel = splitMainPanel + + // no need to set view on bottom when splitMainPanel is false: it will have zero size anyway thanks to our view arrangement code. + if splitMainPanel { + _, _ = gui.g.SetViewOnTop("secondary") + } +} + +func (gui *Gui) isMainPanelSplit() bool { + return gui.State.SplitMainPanel +} diff --git a/pkg/gui/reflog_panel.go b/pkg/gui/reflog_panel.go index 038e2bf7e..c1cc118de 100644 --- a/pkg/gui/reflog_panel.go +++ b/pkg/gui/reflog_panel.go @@ -19,30 +19,28 @@ func (gui *Gui) getSelectedReflogCommit() *commands.Commit { } func (gui *Gui) handleReflogCommitSelect() error { - if gui.popupPanelFocused() { - return nil + if gui.inDiffMode() { + return gui.renderDiff() } - gui.splitMainPanel(false) - - gui.getMainView().Title = "Reflog Entry" + refreshOpts := refreshMainOpts{ + main: &viewUpdateOpts{ + title: "Reflog Entry", + }, + } commit := gui.getSelectedReflogCommit() if commit == nil { - return gui.newStringTask("main", "No reflog history") - } - if gui.inDiffMode() { - return gui.renderDiff() - } + refreshOpts.main.task = func() error { return gui.newStringTask("main", "No reflog history") } + } else { + cmd := gui.OSCommand.ExecutableFromString( + gui.GitCommand.ShowCmdStr(commit.Sha, gui.State.FilterPath), + ) - cmd := gui.OSCommand.ExecutableFromString( - gui.GitCommand.ShowCmdStr(commit.Sha, gui.State.FilterPath), - ) - if err := gui.newPtyTask("main", cmd); err != nil { - gui.Log.Error(err) + refreshOpts.main.task = func() error { return gui.newPtyTask("main", cmd) } } - return nil + return gui.refreshMain(refreshOpts) } // the reflogs panel is the only panel where we cache data, in that we only diff --git a/pkg/gui/view_helpers.go b/pkg/gui/view_helpers.go index 137e0e96f..df3dc5ece 100644 --- a/pkg/gui/view_helpers.go +++ b/pkg/gui/view_helpers.go @@ -368,16 +368,3 @@ func (gui *Gui) clearEditorView(v *gocui.View) { _ = v.SetCursor(0, 0) _ = v.SetOrigin(0, 0) } - -func (gui *Gui) splitMainPanel(state bool) { - gui.State.SplitMainPanel = state - - // no need to set view on bottom when state is false: it will have zero size anyway thanks to our view arrangement code. - if state { - _, _ = gui.g.SetViewOnTop("secondary") - } -} - -func (gui *Gui) isMainPanelSplit() bool { - return gui.State.SplitMainPanel -} |