refactor(git): branch status as setter

This commit is contained in:
Jan De Dobbeleer 2021-12-08 13:36:44 +01:00 committed by Jan De Dobbeleer
parent 04f841d1b3
commit 930fc2bfcc
2 changed files with 22 additions and 18 deletions

View file

@ -182,7 +182,7 @@ func (g *git) string() string {
if displayStatus || statusColorsEnabled {
g.setGitStatus()
g.setGitHEADContext()
g.BranchStatus = g.getBranchStatus()
g.setBranchStatus()
} else {
g.Working = &GitStatus{}
g.Staging = &GitStatus{}
@ -224,23 +224,26 @@ func (g *git) init(props properties, env environmentInfo) {
g.env = env
}
func (g *git) getBranchStatus() string {
if g.Ahead > 0 && g.Behind > 0 {
return fmt.Sprintf(" %s%d %s%d", g.props.getString(BranchAheadIcon, "\u2191"), g.Ahead, g.props.getString(BranchBehindIcon, "\u2193"), g.Behind)
func (g *git) setBranchStatus() {
getBranchStatus := func() string {
if g.Ahead > 0 && g.Behind > 0 {
return fmt.Sprintf(" %s%d %s%d", g.props.getString(BranchAheadIcon, "\u2191"), g.Ahead, g.props.getString(BranchBehindIcon, "\u2193"), g.Behind)
}
if g.Ahead > 0 {
return fmt.Sprintf(" %s%d", g.props.getString(BranchAheadIcon, "\u2191"), g.Ahead)
}
if g.Behind > 0 {
return fmt.Sprintf(" %s%d", g.props.getString(BranchBehindIcon, "\u2193"), g.Behind)
}
if g.Behind == 0 && g.Ahead == 0 && g.Upstream != "" {
return fmt.Sprintf(" %s", g.props.getString(BranchIdenticalIcon, "\u2261"))
}
if g.Upstream == "" {
return fmt.Sprintf(" %s", g.props.getString(BranchGoneIcon, "\u2262"))
}
return ""
}
if g.Ahead > 0 {
return fmt.Sprintf(" %s%d", g.props.getString(BranchAheadIcon, "\u2191"), g.Ahead)
}
if g.Behind > 0 {
return fmt.Sprintf(" %s%d", g.props.getString(BranchBehindIcon, "\u2193"), g.Behind)
}
if g.Behind == 0 && g.Ahead == 0 && g.Upstream != "" {
return fmt.Sprintf(" %s", g.props.getString(BranchIdenticalIcon, "\u2261"))
}
if g.Upstream == "" {
return fmt.Sprintf(" %s", g.props.getString(BranchGoneIcon, "\u2262"))
}
return ""
g.BranchStatus = getBranchStatus()
}
func (g *git) getUpstreamIcon() string {

View file

@ -486,7 +486,8 @@ func TestGetBranchStatus(t *testing.T) {
Behind: tc.Behind,
Upstream: tc.Upstream,
}
assert.Equal(t, tc.Expected, g.getBranchStatus(), tc.Case)
g.setBranchStatus()
assert.Equal(t, tc.Expected, g.BranchStatus, tc.Case)
}
}