diff --git a/src/segment_git.go b/src/segment_git.go index caafeedc..c7d072b0 100644 --- a/src/segment_git.go +++ b/src/segment_git.go @@ -139,11 +139,18 @@ func (g *git) enabled() bool { } func (g *git) string() string { + statusColorsEnabled := g.props.getBool(StatusColorsEnabled, false) displayStatus := g.props.getBool(DisplayStatus, true) + + if displayStatus || statusColorsEnabled { + g.setGitStatus() + } + if statusColorsEnabled { + g.SetStatusColor() + } if !displayStatus { return g.getPrettyHEADName() } - g.setGitStatus() buffer := new(bytes.Buffer) // remote (if available) if g.repo.upstream != "" && g.props.getBool(DisplayUpstreamIcon, false) { @@ -166,9 +173,6 @@ func (g *git) string() string { if g.repo.stashCount != 0 { fmt.Fprintf(buffer, " %s%d", g.props.getString(StashCountIcon, "\uF692 "), g.repo.stashCount) } - if g.props.getBool(StatusColorsEnabled, false) { - g.SetStatusColor() - } return buffer.String() } diff --git a/src/segment_git_test.go b/src/segment_git_test.go index 66b692ee..3439d1ca 100644 --- a/src/segment_git_test.go +++ b/src/segment_git_test.go @@ -82,6 +82,7 @@ type detachedContext struct { cherryPickSHA string merge bool mergeHEAD string + status string } func setupHEADContextEnv(context *detachedContext) *git { @@ -105,6 +106,7 @@ func setupHEADContextEnv(context *detachedContext) *git { env.mockGitCommand(context.origin, "name-rev", "--name-only", "--exclude=tags/*", context.origin) env.mockGitCommand(context.onto, "name-rev", "--name-only", "--exclude=tags/*", context.onto) env.mockGitCommand(context.branchName, "branch", "--show-current") + env.mockGitCommand(context.status, "status", "-unormal", "--short", "--branch") env.On("getRuntimeGOOS", nil).Return("unix") g := &git{ env: env, @@ -560,7 +562,7 @@ func TestGetStatusColorDefault(t *testing.T) { assert.Equal(t, expected, g.getStatusColor(expected)) } -func TestSetStatusColorBackground(t *testing.T) { +func TestSetStatusColorForeground(t *testing.T) { expected := changesColor repo := &gitRepo{ staging: &gitStatus{ @@ -582,7 +584,7 @@ func TestSetStatusColorBackground(t *testing.T) { assert.Equal(t, expected, g.props.foreground) } -func TestSetStatusColorForeground(t *testing.T) { +func TestSetStatusColorBackground(t *testing.T) { expected := changesColor repo := &gitRepo{ staging: &gitStatus{ @@ -604,6 +606,23 @@ func TestSetStatusColorForeground(t *testing.T) { assert.Equal(t, expected, g.props.background) } +func TestStatusColorsWithoutDisplayStatus(t *testing.T) { + expected := changesColor + context := &detachedContext{ + status: "## main...origin/main [ahead 33]\n M myfile", + } + g := setupHEADContextEnv(context) + g.props = &properties{ + values: map[Property]interface{}{ + DisplayStatus: false, + StatusColorsEnabled: true, + LocalChangesColor: expected, + }, + } + g.string() + assert.Equal(t, expected, g.props.background) +} + func TestGetStatusDetailStringDefault(t *testing.T) { expected := "icon +1" status := &gitStatus{