From 3f8400e8f18659f225305e67342f8548892b2614 Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Sun, 31 Oct 2021 20:59:35 +0100 Subject: [PATCH] refactor(git): add branch status to template --- src/segment_git.go | 54 +++++++++++++++++++------------- src/segment_git_deprecated.go | 9 ++---- src/segment_git_test.go | 59 ++++++++++++++++++++++++----------- 3 files changed, 76 insertions(+), 46 deletions(-) diff --git a/src/segment_git.go b/src/segment_git.go index ffe14cfc..91811cc3 100644 --- a/src/segment_git.go +++ b/src/segment_git.go @@ -10,16 +10,17 @@ import ( // Repo represents a git repository type Repo struct { - Working *GitStatus - Staging *GitStatus - Ahead int - Behind int - HEAD string - Upstream string - UpstreamIcon string - StashCount int - WorktreeCount int - IsWorkTree bool + Working *GitStatus + Staging *GitStatus + Ahead int + Behind int + HEAD string + BranchStatus string + Upstream string + UpstreamIcon string + StashCount int + WorktreeCount int + IsWorkTree bool gitWorkingFolder string // .git working folder, can be different of root if using worktree gitRootFolder string // .git root folder @@ -191,7 +192,9 @@ func (g *git) shouldIgnoreRootRepository(rootDir string) bool { func (g *git) string() string { statusColorsEnabled := g.props.getBool(StatusColorsEnabled, false) displayStatus := g.props.getBool(DisplayStatus, false) - + if !displayStatus { + g.repo.HEAD = g.getPrettyHEADName() + } if displayStatus || statusColorsEnabled { g.setGitStatus() } @@ -201,23 +204,30 @@ func (g *git) string() string { if g.repo.Upstream != "" && g.props.getBool(DisplayUpstreamIcon, false) { g.repo.UpstreamIcon = g.getUpstreamIcon() } + if g.props.getBool(DisplayBranchStatus, true) { + g.repo.BranchStatus = g.getBranchStatus() + } // use template if available segmentTemplate := g.props.getString(SegmentTemplate, "") if len(segmentTemplate) > 0 { - template := &textTemplate{ - Template: segmentTemplate, - Context: g.repo, - Env: g.env, - } - text, err := template.render() - if err != nil { - return err.Error() - } - return text + return g.templateString(segmentTemplate) } // legacy render string if no template // remove this for 6.0 - return g.renderDeprecatedString(displayStatus) + return g.renderDeprecatedString() +} + +func (g *git) templateString(segmentTemplate string) string { + template := &textTemplate{ + Template: segmentTemplate, + Context: g.repo, + Env: g.env, + } + text, err := template.render() + if err != nil { + return err.Error() + } + return text } func (g *git) init(props *properties, env environmentInfo) { diff --git a/src/segment_git_deprecated.go b/src/segment_git_deprecated.go index 215a2776..3317ff41 100644 --- a/src/segment_git_deprecated.go +++ b/src/segment_git_deprecated.go @@ -32,10 +32,7 @@ const ( WorktreeCountIcon Property = "worktree_count_icon" ) -func (g *git) renderDeprecatedString(displayStatus bool) string { - if !displayStatus { - return g.getPrettyHEADName() - } +func (g *git) renderDeprecatedString() string { buffer := new(bytes.Buffer) // remote (if available) if len(g.repo.UpstreamIcon) != 0 { @@ -43,8 +40,8 @@ func (g *git) renderDeprecatedString(displayStatus bool) string { } // branchName fmt.Fprintf(buffer, "%s", g.repo.HEAD) - if g.props.getBool(DisplayBranchStatus, true) { - buffer.WriteString(g.getBranchStatus()) + if len(g.repo.BranchStatus) != 0 { + buffer.WriteString(g.repo.BranchStatus) } if g.repo.Staging.Changed { fmt.Fprint(buffer, g.getStatusDetailString(g.repo.Staging, StagingColor, LocalStagingIcon, " \uF046")) diff --git a/src/segment_git_test.go b/src/segment_git_test.go index 1024eacd..a099b5d5 100644 --- a/src/segment_git_test.go +++ b/src/segment_git_test.go @@ -131,8 +131,8 @@ func setupHEADContextEnv(context *detachedContext) *git { env: env, repo: &Repo{ gitWorkingFolder: "", - Working: &GitStatus{}, - Staging: &GitStatus{}, + Working: &GitStatus{}, + Staging: &GitStatus{}, }, } return g @@ -993,7 +993,7 @@ func TestGetGitCommand(t *testing.T) { } } -func TestGitTemplate(t *testing.T) { +func TestGitTemplateString(t *testing.T) { cases := []struct { Case string Expected string @@ -1001,29 +1001,29 @@ func TestGitTemplate(t *testing.T) { Repo *Repo }{ { - Case: "Only HEAD name", + Case: "Only HEAD name", Expected: "main", Template: "{{ .HEAD }}", Repo: &Repo{ - HEAD: "main", + HEAD: "main", Behind: 2, }, }, { - Case: "Working area changes", + Case: "Working area changes", Expected: "main \uF044 +2 ~3", Template: "{{ .HEAD }}{{ if .Working.Changed }} \uF044{{ .Working.String }}{{ end }}", Repo: &Repo{ HEAD: "main", Working: &GitStatus{ - Added: 2, + Added: 2, Modified: 3, - Changed: true, + Changed: true, }, }, }, { - Case: "No working area changes", + Case: "No working area changes", Expected: "main", Template: "{{ .HEAD }}{{ if .Working.Changed }} \uF044{{ .Working.String }}{{ end }}", Repo: &Repo{ @@ -1034,44 +1034,67 @@ func TestGitTemplate(t *testing.T) { }, }, { - Case: "Working and staging area changes", + Case: "Working and staging area changes", Expected: "main \uF046 +5 ~1 \uF044 +2 ~3", Template: "{{ .HEAD }}{{ if .Staging.Changed }} \uF046{{ .Staging.String }}{{ end }}{{ if .Working.Changed }} \uF044{{ .Working.String }}{{ end }}", Repo: &Repo{ HEAD: "main", Working: &GitStatus{ - Added: 2, + Added: 2, Modified: 3, - Changed: true, + Changed: true, }, Staging: &GitStatus{ - Added: 5, + Added: 5, Modified: 1, - Changed: true, + Changed: true, }, }, }, { - Case: "No local changes", + Case: "No local changes", Expected: "main", Template: "{{ .HEAD }}{{ if .Staging.Changed }} \uF046{{ .Staging.String }}{{ end }}{{ if .Working.Changed }} \uF044{{ .Working.String }}{{ end }}", Repo: &Repo{ - HEAD: "main", + HEAD: "main", Staging: &GitStatus{}, Working: &GitStatus{}, }, }, + { + Case: "Upstream Icon", + Expected: "from GitHub on main", + Template: "from {{ .UpstreamIcon }} on {{ .HEAD }}", + Repo: &Repo{ + HEAD: "main", + Staging: &GitStatus{}, + Working: &GitStatus{}, + UpstreamIcon: "GitHub", + }, + }, + { + Case: "Branch status", + Expected: "from GitHub on main \u21912", + Template: "from {{ .UpstreamIcon }} on {{ .HEAD }} {{ .BranchStatus }}", + Repo: &Repo{ + HEAD: "main", + Staging: &GitStatus{}, + Working: &GitStatus{}, + UpstreamIcon: "GitHub", + BranchStatus: "\u21912", + }, + }, } for _, tc := range cases { g := &git{ props: &properties{ values: map[Property]interface{}{ - SegmentTemplate: tc.Template, + DisplayStatus: true, }, }, repo: tc.Repo, } - assert.Equal(t, tc.Expected, g.string(), tc.Case) + assert.Equal(t, tc.Expected, g.templateString(tc.Template), tc.Case) } }