Skip to content

Commit

Permalink
feat: disable git status detail
Browse files Browse the repository at this point in the history
resolves #93
  • Loading branch information
JanDeDobbeleer committed Oct 24, 2020
1 parent 6ffb8b2 commit f3e46d8
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 8 deletions.
1 change: 1 addition & 0 deletions docs/docs/segment-git.md
Expand Up @@ -42,6 +42,7 @@ Local changes can also shown by default using the following syntax for both the
### Status

- display_status: `boolean` - display the local changes or not - defaults to `true`
- display_status_detail: `boolean` - display the local changes in detail or not - defaults to `true`
- display_stash_count: `boolean` show stash count or not - defaults to `false`
- status_separator_icon: `string` icon/text to display between staging and working area changes - defaults to ` |`
- local_working_icon: `string` - the icon to display in front of the working area changes - defaults to `\uF044`
Expand Down
20 changes: 14 additions & 6 deletions segment_git.go
Expand Up @@ -42,7 +42,7 @@ func (s *gitStatus) string(prefix string, color string) string {
status += stringIfValue(s.untracked, "?")
status += stringIfValue(s.unmerged, "x")
if status != "" {
return fmt.Sprintf(" <%s>%s%s</>", color, prefix, status)
return fmt.Sprintf("<%s>%s%s</>", color, prefix, status)
}
return status
}
Expand Down Expand Up @@ -70,6 +70,8 @@ const (
LocalStagingIcon Property = "local_staged_icon"
//DisplayStatus shows the status of the repository
DisplayStatus Property = "display_status"
//DisplayStatusDetail shows the detailed status of the repository
DisplayStatusDetail Property = "display_status_detail"
//RebaseIcon shows before the rebase context
RebaseIcon Property = "rebase_icon"
//CherryPickIcon shows before the cherry-pick context
Expand Down Expand Up @@ -125,7 +127,6 @@ func (g *git) string() string {
if g.props.getBool(StatusColorsEnabled, false) {
g.SetStatusColor()
}
// g.getGitColor()
buffer := new(bytes.Buffer)
// branchName
if g.repo.upstream != "" && g.props.getBool(DisplayUpstreamIcon, false) {
Expand All @@ -150,15 +151,13 @@ func (g *git) string() string {
fmt.Fprintf(buffer, " %s", g.props.getString(BranchGoneIcon, "\u2262"))
}
if g.repo.staging.changed {
staging := g.repo.staging.string(g.props.getString(LocalStagingIcon, "\uF046"), g.props.getColor(StagingColor, g.props.foreground))
fmt.Fprint(buffer, staging)
fmt.Fprint(buffer, g.getStatusDetailString(g.repo.staging, StagingColor, LocalStagingIcon, " \uF046"))
}
if g.repo.staging.changed && g.repo.working.changed {
fmt.Fprint(buffer, g.props.getString(StatusSeparatorIcon, " |"))
}
if g.repo.working.changed {
working := g.repo.working.string(g.props.getString(LocalWorkingIcon, "\uF044"), g.props.getColor(WorkingColor, g.props.foreground))
fmt.Fprint(buffer, working)
fmt.Fprint(buffer, g.getStatusDetailString(g.repo.working, WorkingColor, LocalWorkingIcon, " \uF044"))
}
if g.props.getBool(DisplayStashCount, false) && g.repo.stashCount != "" {
fmt.Fprintf(buffer, " %s%s", g.props.getString(StashCountIcon, "\uF692"), g.repo.stashCount)
Expand All @@ -171,6 +170,15 @@ func (g *git) init(props *properties, env environmentInfo) {
g.env = env
}

func (g *git) getStatusDetailString(status *gitStatus, color Property, icon Property, defaultIcon string) string {
prefix := g.props.getString(icon, defaultIcon)
foregroundColor := g.props.getColor(color, g.props.foreground)
if !g.props.getBool(DisplayStatusDetail, true) {
return fmt.Sprintf("<%s>%s</>", foregroundColor, prefix)
}
return status.string(prefix, foregroundColor)
}

func (g *git) getUpstreamSymbol() string {
upstreamRegex := regexp.MustCompile("/.*")
upstream := upstreamRegex.ReplaceAllString(g.repo.upstream, "")
Expand Down
53 changes: 51 additions & 2 deletions segment_git_test.go
Expand Up @@ -268,15 +268,15 @@ func TestParseGitBranchInfoNoRemote(t *testing.T) {
}

func TestGitStatusUnmerged(t *testing.T) {
expected := " <#123456>working: x1</>"
expected := "<#123456>working: x1</>"
status := &gitStatus{
unmerged: 1,
}
assert.Equal(t, expected, status.string("working:", "#123456"))
}

func TestGitStatusUnmergedModified(t *testing.T) {
expected := " <#123456>working: ~3 x1</>"
expected := "<#123456>working: ~3 x1</>"
status := &gitStatus{
unmerged: 1,
modified: 3,
Expand Down Expand Up @@ -558,3 +558,52 @@ func TestSetStatusColorForeground(t *testing.T) {
g.SetStatusColor()
assert.Equal(t, expected, g.props.background)
}

func TestGetStatusDetailStringDefault(t *testing.T) {
expected := "<#111111>icon +1</>"
status := &gitStatus{
changed: true,
added: 1,
}
g := &git{
props: &properties{
foreground: "#111111",
},
}
assert.Equal(t, expected, g.getStatusDetailString(status, WorkingColor, LocalWorkingIcon, "icon"))
}

func TestGetStatusDetailStringNoStatus(t *testing.T) {
expected := "<#111111>icon</>"
status := &gitStatus{
changed: true,
added: 1,
}
g := &git{
props: &properties{
values: map[Property]interface{}{
DisplayStatusDetail: false,
},
foreground: "#111111",
},
}
assert.Equal(t, expected, g.getStatusDetailString(status, WorkingColor, LocalWorkingIcon, "icon"))
}

func TestGetStatusDetailStringNoStatusColorOverride(t *testing.T) {
expected := "<#123456>icon</>"
status := &gitStatus{
changed: true,
added: 1,
}
g := &git{
props: &properties{
values: map[Property]interface{}{
DisplayStatusDetail: false,
WorkingColor: "#123456",
},
foreground: "#111111",
},
}
assert.Equal(t, expected, g.getStatusDetailString(status, WorkingColor, LocalWorkingIcon, "icon"))
}

0 comments on commit f3e46d8

Please sign in to comment.