Skip to content

Commit

Permalink
more efficient refreshing
Browse files Browse the repository at this point in the history
  • Loading branch information
jesseduffield committed Mar 28, 2020
1 parent fbbd16b commit efb51ee
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 83 deletions.
34 changes: 11 additions & 23 deletions pkg/gui/branches_panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,32 +53,20 @@ func (gui *Gui) handleBranchSelect(g *gocui.Gui, v *gocui.View) error {

// gui.refreshStatus is called at the end of this because that's when we can
// be sure there is a state.Branches array to pick the current branch from
func (gui *Gui) refreshBranches(g *gocui.Gui) error {
if err := gui.refreshRemotes(); err != nil {
return err
}
func (gui *Gui) refreshBranches() {
_ = gui.refreshRemotes()
_ = gui.refreshTags()

if err := gui.refreshTags(); err != nil {
return err
builder, err := commands.NewBranchListBuilder(gui.Log, gui.GitCommand, gui.State.ReflogCommits)
if err != nil {
_ = gui.createErrorPanel(gui.g, err.Error())
}
gui.State.Branches = builder.Build()

g.Update(func(g *gocui.Gui) error {
builder, err := commands.NewBranchListBuilder(gui.Log, gui.GitCommand, gui.State.ReflogCommits)
if err != nil {
return err
}
gui.State.Branches = builder.Build()

// TODO: if we're in the remotes view and we've just deleted a remote we need to refresh accordingly
if gui.getBranchesView().Context == "local-branches" {
if err := gui.renderLocalBranchesWithSelection(); err != nil {
return err
}
}

return gui.refreshStatus(g)
})
return nil
// TODO: if we're in the remotes view and we've just deleted a remote we need to refresh accordingly
if gui.getBranchesView().Context == "local-branches" {
gui.renderLocalBranchesWithSelection()
}
}

func (gui *Gui) renderLocalBranchesWithSelection() error {
Expand Down
33 changes: 16 additions & 17 deletions pkg/gui/commits_panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,27 +71,26 @@ func (gui *Gui) handleCommitSelect(g *gocui.Gui, v *gocui.View) error {
}

func (gui *Gui) refreshCommits(g *gocui.Gui) error {
g.Update(func(*gocui.Gui) error {
// I think this is here for the sake of some kind of rebasing thing
_ = gui.refreshStatus(g)
if err := gui.updateWorkTreeState(); err != nil {
return gui.createErrorPanel(gui.g, err.Error())
}

if err := gui.refreshCommitsWithLimit(); err != nil {
return err
}
if err := gui.refreshCommitsWithLimit(); err != nil {
return gui.createErrorPanel(gui.g, err.Error())
}

if err := gui.refreshReflogCommits(); err != nil {
return gui.createErrorPanel(gui.g, err.Error())
}
if err := gui.refreshReflogCommits(); err != nil {
return gui.createErrorPanel(gui.g, err.Error())
}

if err := gui.refreshBranches(gui.g); err != nil {
return gui.createErrorPanel(gui.g, err.Error())
}
gui.refreshBranches()

gui.refreshStatus()

if g.CurrentView() == gui.getCommitFilesView() || (g.CurrentView() == gui.getMainView() || gui.State.MainContext == "patch-building") {
return gui.refreshCommitFilesView()
}

if g.CurrentView() == gui.getCommitFilesView() || (g.CurrentView() == gui.getMainView() || gui.State.MainContext == "patch-building") {
return gui.refreshCommitFilesView()
}
return nil
})
return nil
}

Expand Down
11 changes: 3 additions & 8 deletions pkg/gui/gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,6 @@ type commitFilesPanelState struct {
SelectedLine int
}

type statusPanelState struct {
pushables string
pullables string
}

type panelStates struct {
Files *filePanelState
Branches *branchPanelState
Expand All @@ -175,7 +170,6 @@ type panelStates struct {
LineByLine *lineByLinePanelState
Merging *mergingPanelState
CommitFiles *commitFilesPanelState
Status *statusPanelState
}

type searchingState struct {
Expand Down Expand Up @@ -246,7 +240,6 @@ func NewGui(log *logrus.Entry, gitCommand *commands.GitCommand, oSCommand *comma
Conflicts: []commands.Conflict{},
EditHistory: stack.New(),
},
Status: &statusPanelState{},
},
ScreenMode: SCREEN_NORMAL,
SideView: nil,
Expand Down Expand Up @@ -930,7 +923,9 @@ func (gui *Gui) fetch(g *gocui.Gui, v *gocui.View, canAskForCredentials bool) (u
_ = gui.createConfirmationPanel(g, v, true, gui.Tr.SLocalize("Error"), coloredMessage, close, close)
}

_ = gui.refreshStatus(g)
if err := gui.refreshCommits(g); err != nil {
return unamePassOpend, err
}

return unamePassOpend, err
}
Expand Down
54 changes: 19 additions & 35 deletions pkg/gui/status_panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,50 +10,34 @@ import (
"github.com/jesseduffield/lazygit/pkg/utils"
)

func (gui *Gui) refreshStatus(g *gocui.Gui) error {
state := gui.State.Panels.Status

v, err := g.View("status")
if err != nil {
panic(err)
}
// for some reason if this isn't wrapped in an update the clear seems to
// be applied after the other things or something like that; the panel's
// contents end up cleared
g.Update(func(*gocui.Gui) error {
v.Clear()
// TODO: base this off of the current branch
state.pushables, state.pullables = gui.GitCommand.GetCurrentBranchUpstreamDifferenceCount()
if err := gui.updateWorkTreeState(); err != nil {
return err
}
// never call this on its own, it should only be called from within refreshCommits()
func (gui *Gui) refreshStatus() {
currentBranch := gui.currentBranch()
status := ""

if currentBranch.Pushables != "" && currentBranch.Pullables != "" {
trackColor := color.FgYellow
if state.pushables == "0" && state.pullables == "0" {
if currentBranch.Pushables == "0" && currentBranch.Pullables == "0" {
trackColor = color.FgGreen
} else if state.pushables == "?" && state.pullables == "?" {
} else if currentBranch.Pushables == "?" && currentBranch.Pullables == "?" {
trackColor = color.FgRed
}

status := utils.ColoredString(fmt.Sprintf("↑%s↓%s", state.pushables, state.pullables), trackColor)
branches := gui.State.Branches
status = utils.ColoredString(fmt.Sprintf("↑%s↓%s ", currentBranch.Pushables, currentBranch.Pullables), trackColor)
}

if gui.State.WorkingTreeState != "normal" {
status += utils.ColoredString(fmt.Sprintf(" (%s)", gui.State.WorkingTreeState), color.FgYellow)
}
if gui.State.WorkingTreeState != "normal" {
status += utils.ColoredString(fmt.Sprintf("(%s) ", gui.State.WorkingTreeState), color.FgYellow)
}

if len(branches) > 0 {
branch := branches[0]
name := utils.ColoredString(branch.Name, presentation.GetBranchColor(branch.Name))
repoName := utils.GetCurrentRepoName()
status += fmt.Sprintf(" %s → %s", repoName, name)
}
name := utils.ColoredString(currentBranch.Name, presentation.GetBranchColor(currentBranch.Name))
repoName := utils.GetCurrentRepoName()
status += fmt.Sprintf("%s → %s ", repoName, name)

fmt.Fprint(v, status)
gui.g.Update(func(*gocui.Gui) error {
gui.setViewContent(gui.g, gui.getStatusView(), status)
return nil
})

return nil
}

func runeCount(str string) int {
Expand All @@ -70,10 +54,10 @@ func (gui *Gui) handleCheckForUpdate(g *gocui.Gui, v *gocui.View) error {
}

func (gui *Gui) handleStatusClick(g *gocui.Gui, v *gocui.View) error {
state := gui.State.Panels.Status
currentBranch := gui.currentBranch()

cx, _ := v.Cursor()
upstreamStatus := fmt.Sprintf("↑%s↓%s", state.pushables, state.pullables)
upstreamStatus := fmt.Sprintf("↑%s↓%s", currentBranch.Pushables, currentBranch.Pullables)
repoName := utils.GetCurrentRepoName()
gui.Log.Warn(gui.State.WorkingTreeState)
switch gui.State.WorkingTreeState {
Expand Down
5 changes: 5 additions & 0 deletions pkg/gui/view_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,11 @@ func (gui *Gui) getSearchView() *gocui.View {
return v
}

func (gui *Gui) getStatusView() *gocui.View {
v, _ := gui.g.View("status")
return v
}

func (gui *Gui) trimmedContent(v *gocui.View) string {
return strings.TrimSpace(v.Buffer())
}
Expand Down

0 comments on commit efb51ee

Please sign in to comment.