From 1402dcbcfeb728fccc402ae790fc4e5e5720be89 Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Sat, 6 Nov 2021 20:32:24 +0100 Subject: [PATCH] refactor(git): rename properties to fetch --- src/segment_git.go | 26 +++++++++--------- src/segment_git_deprecated.go | 17 ++++++++++++ src/segment_git_test.go | 51 ++++++++++++++++++++++++++--------- 3 files changed, 68 insertions(+), 26 deletions(-) diff --git a/src/segment_git.go b/src/segment_git.go index fe1ef628..89a09800 100644 --- a/src/segment_git.go +++ b/src/segment_git.go @@ -87,17 +87,17 @@ type git struct { } const ( - // DisplayBranchStatus show branch status or not - DisplayBranchStatus Property = "display_branch_status" - // DisplayStatus shows the status of the repository - DisplayStatus Property = "display_status" - // DisplayStashCount show stash count or not - DisplayStashCount Property = "display_stash_count" - // DisplayWorktreeCount show worktree count or not - DisplayWorktreeCount Property = "display_worktree_count" + // FetchBranchStatus show branch status or not + FetchBranchStatus Property = "fetch_branch_status" + // FetchStatus shows the status of the repository + FetchStatus Property = "fetch_status" + // FetchStashCount show stash count or not + FetchStashCount Property = "fetch_stash_count" + // FetchWorktreeCount show worktree count or not + FetchWorktreeCount Property = "fetch_worktree_count" + // DisplayUpstreamIcon show or hide the upstream icon DisplayUpstreamIcon Property = "display_upstream_icon" - // BranchMaxLength truncates the length of the branch name BranchMaxLength Property = "branch_max_length" // BranchIcon the icon to use as branch indicator @@ -189,7 +189,7 @@ func (g *git) shouldIgnoreRootRepository(rootDir string) bool { func (g *git) string() string { statusColorsEnabled := g.props.getBool(StatusColorsEnabled, false) - displayStatus := g.props.getBool(DisplayStatus, false) + displayStatus := g.getBool(FetchStatus, DisplayStatus, false) if !displayStatus { g.repo.HEAD = g.getPrettyHEADName() } @@ -199,7 +199,7 @@ 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) { + if g.getBool(FetchBranchStatus, DisplayBranchStatus, true) { g.repo.BranchStatus = g.getBranchStatus() } // use template if available @@ -281,10 +281,10 @@ func (g *git) setGitStatus() { } } g.repo.HEAD = g.getGitHEADContext(status["local"]) - if g.props.getBool(DisplayStashCount, false) { + if g.getBool(FetchStashCount, DisplayStashCount, false) { g.repo.StashCount = g.getStashContext() } - if g.props.getBool(DisplayWorktreeCount, false) { + if g.getBool(FetchWorktreeCount, DisplayWorktreeCount, false) { g.repo.WorktreeCount = g.getWorktreeContext() } } diff --git a/src/segment_git_deprecated.go b/src/segment_git_deprecated.go index d90ae408..a6a68317 100644 --- a/src/segment_git_deprecated.go +++ b/src/segment_git_deprecated.go @@ -7,6 +7,15 @@ import ( ) const ( + // DisplayBranchStatus show branch status or not + DisplayBranchStatus Property = "display_branch_status" + // DisplayStatus shows the status of the repository + DisplayStatus Property = "display_status" + // DisplayStashCount show stash count or not + DisplayStashCount Property = "display_stash_count" + // DisplayWorktreeCount show worktree count or not + DisplayWorktreeCount Property = "display_worktree_count" + // LocalWorkingIcon the icon to use as the local working area changes indicator LocalWorkingIcon Property = "local_working_icon" // LocalStagingIcon the icon to use as the local staging area changes indicator @@ -35,6 +44,14 @@ const ( StatusSeparatorIcon Property = "status_separator_icon" ) +func (g *git) getBool(property, legacyProperty Property, defaultValue bool) bool { + _, found := g.props.values[legacyProperty] + if found { + return g.props.getBool(legacyProperty, defaultValue) + } + return g.props.getBool(property, defaultValue) +} + func (g *git) renderDeprecatedString(statusColorsEnabled bool) string { if statusColorsEnabled { g.SetStatusColor() diff --git a/src/segment_git_test.go b/src/segment_git_test.go index 3d70dbd8..8a66505e 100644 --- a/src/segment_git_test.go +++ b/src/segment_git_test.go @@ -756,6 +756,43 @@ func TestGitTemplateString(t *testing.T) { }, }, }, + { + Case: "Working and staging area changes with separator", + Expected: "main \uF046 +5 ~1 | \uF044 +2 ~3", + Template: "{{ .HEAD }}{{ if .Staging.Changed }} \uF046 {{ .Staging.String }}{{ end }}{{ if and (.Working.Changed) (.Staging.Changed) }} |{{ end }}{{ if .Working.Changed }} \uF044 {{ .Working.String }}{{ end }}", //nolint:lll + Repo: &Repo{ + HEAD: "main", + Working: &GitStatus{ + Added: 2, + Modified: 3, + Changed: true, + }, + Staging: &GitStatus{ + Added: 5, + Modified: 1, + Changed: true, + }, + }, + }, + { + Case: "Working and staging area changes with separator and stash count", + Expected: "main \uF046 +5 ~1 | \uF044 +2 ~3 \uf692 3", + Template: "{{ .HEAD }}{{ if .Staging.Changed }} \uF046 {{ .Staging.String }}{{ end }}{{ if and (.Working.Changed) (.Staging.Changed) }} |{{ end }}{{ if .Working.Changed }} \uF044 {{ .Working.String }}{{ end }}{{ if gt .StashCount 0 }} \uF692 {{ .StashCount }}{{ end }}", //nolint:lll + Repo: &Repo{ + HEAD: "main", + Working: &GitStatus{ + Added: 2, + Modified: 3, + Changed: true, + }, + Staging: &GitStatus{ + Added: 5, + Modified: 1, + Changed: true, + }, + StashCount: 3, + }, + }, { Case: "No local changes", Expected: "main", @@ -777,25 +814,13 @@ func TestGitTemplateString(t *testing.T) { 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{}{ - DisplayStatus: true, + FetchStatus: true, }, }, repo: tc.Repo,