From 930fc2bfcc9ca5ef24c131a8f1b049567df975c1 Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Wed, 8 Dec 2021 13:36:44 +0100 Subject: [PATCH] refactor(git): branch status as setter --- src/segment_git.go | 37 ++++++++++++++++++++----------------- src/segment_git_test.go | 3 ++- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/src/segment_git.go b/src/segment_git.go index 53166742..e02fe8a9 100644 --- a/src/segment_git.go +++ b/src/segment_git.go @@ -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 { diff --git a/src/segment_git_test.go b/src/segment_git_test.go index 6568f46b..feebbe1c 100644 --- a/src/segment_git_test.go +++ b/src/segment_git_test.go @@ -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) } }