From 4dbed1176c5bdc77afe1f9bf5d339ed66fc81336 Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Sun, 31 Oct 2021 20:45:55 +0100 Subject: [PATCH] refactor(git): add upstream icon to repo --- src/segment_git.go | 84 ++++++----------------------------- src/segment_git_deprecated.go | 65 +++++++++++++++++++++++++++ src/segment_git_test.go | 12 ++--- 3 files changed, 85 insertions(+), 76 deletions(-) create mode 100644 src/segment_git_deprecated.go diff --git a/src/segment_git.go b/src/segment_git.go index 9a9fcbad..ffe14cfc 100644 --- a/src/segment_git.go +++ b/src/segment_git.go @@ -1,7 +1,6 @@ package main import ( - "bytes" "fmt" "strconv" "strings" @@ -11,15 +10,16 @@ import ( // Repo represents a git repository type Repo struct { - Working *GitStatus - Staging *GitStatus - Ahead int - Behind int - HEAD string - Upstream string - StashCount int - WorktreeCount int - IsWorkTree bool + Working *GitStatus + Staging *GitStatus + Ahead int + Behind int + HEAD 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 @@ -107,10 +107,6 @@ const ( BranchBehindIcon Property = "branch_behind_icon" // BranchGoneIcon the icon to use when ther's no remote BranchGoneIcon Property = "branch_gone_icon" - // 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 - LocalStagingIcon Property = "local_staged_icon" // RebaseIcon shows before the rebase context RebaseIcon Property = "rebase_icon" // CherryPickIcon shows before the cherry-pick context @@ -139,29 +135,6 @@ const ( GitlabIcon Property = "gitlab_icon" // GitIcon shows when the upstream can't be identified GitIcon Property = "git_icon" - - // Deprecated - - // DisplayStatusDetail shows the detailed status of the repository - DisplayStatusDetail Property = "display_status_detail" - // WorkingColor if set, the color to use on the working area - WorkingColor Property = "working_color" - // StagingColor if set, the color to use on the staging area - StagingColor Property = "staging_color" - // StatusColorsEnabled enables status colors - StatusColorsEnabled Property = "status_colors_enabled" - // LocalChangesColor if set, the color to use when there are local changes - LocalChangesColor Property = "local_changes_color" - // AheadAndBehindColor if set, the color to use when the branch is ahead and behind the remote - AheadAndBehindColor Property = "ahead_and_behind_color" - // BehindColor if set, the color to use when the branch is ahead and behind the remote - BehindColor Property = "behind_color" - // AheadColor if set, the color to use when the branch is ahead and behind the remote - AheadColor Property = "ahead_color" - // BranchMaxLength truncates the length of the branch name - BranchMaxLength Property = "branch_max_length" - // WorktreeCountIcon shows before the worktree context - WorktreeCountIcon Property = "worktree_count_icon" ) func (g *git) enabled() bool { @@ -225,6 +198,9 @@ func (g *git) string() string { if statusColorsEnabled { g.SetStatusColor() } + if g.repo.Upstream != "" && g.props.getBool(DisplayUpstreamIcon, false) { + g.repo.UpstreamIcon = g.getUpstreamIcon() + } // use template if available segmentTemplate := g.props.getString(SegmentTemplate, "") if len(segmentTemplate) > 0 { @@ -244,38 +220,6 @@ func (g *git) string() string { return g.renderDeprecatedString(displayStatus) } -func (g *git) renderDeprecatedString(displayStatus bool) string { - if !displayStatus { - return g.getPrettyHEADName() - } - buffer := new(bytes.Buffer) - // remote (if available) - if g.repo.Upstream != "" && g.props.getBool(DisplayUpstreamIcon, false) { - fmt.Fprintf(buffer, "%s", g.getUpstreamSymbol()) - } - // branchName - fmt.Fprintf(buffer, "%s", g.repo.HEAD) - if g.props.getBool(DisplayBranchStatus, true) { - buffer.WriteString(g.getBranchStatus()) - } - if g.repo.Staging.Changed { - 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 { - fmt.Fprint(buffer, g.getStatusDetailString(g.repo.Working, WorkingColor, LocalWorkingIcon, " \uF044")) - } - if g.repo.StashCount != 0 { - fmt.Fprintf(buffer, " %s%d", g.props.getString(StashCountIcon, "\uF692 "), g.repo.StashCount) - } - if g.repo.WorktreeCount != 0 { - fmt.Fprintf(buffer, " %s%d", g.props.getString(WorktreeCountIcon, "\uf1bb "), g.repo.WorktreeCount) - } - return buffer.String() -} - func (g *git) init(props *properties, env environmentInfo) { g.props = props g.env = env @@ -319,7 +263,7 @@ func (g *git) colorStatusString(prefix, status, color string) string { return fmt.Sprintf("<%s>%s%s", color, prefix, status) } -func (g *git) getUpstreamSymbol() string { +func (g *git) getUpstreamIcon() string { upstream := replaceAllString("/.*", g.repo.Upstream, "") url := g.getOriginURL(upstream) if strings.Contains(url, "github") { diff --git a/src/segment_git_deprecated.go b/src/segment_git_deprecated.go new file mode 100644 index 00000000..215a2776 --- /dev/null +++ b/src/segment_git_deprecated.go @@ -0,0 +1,65 @@ +package main + +import ( + "bytes" + "fmt" +) + +const ( + // 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 + LocalStagingIcon Property = "local_staged_icon" + // DisplayStatusDetail shows the detailed status of the repository + DisplayStatusDetail Property = "display_status_detail" + // WorkingColor if set, the color to use on the working area + WorkingColor Property = "working_color" + // StagingColor if set, the color to use on the staging area + StagingColor Property = "staging_color" + // StatusColorsEnabled enables status colors + StatusColorsEnabled Property = "status_colors_enabled" + // LocalChangesColor if set, the color to use when there are local changes + LocalChangesColor Property = "local_changes_color" + // AheadAndBehindColor if set, the color to use when the branch is ahead and behind the remote + AheadAndBehindColor Property = "ahead_and_behind_color" + // BehindColor if set, the color to use when the branch is ahead and behind the remote + BehindColor Property = "behind_color" + // AheadColor if set, the color to use when the branch is ahead and behind the remote + AheadColor Property = "ahead_color" + // BranchMaxLength truncates the length of the branch name + BranchMaxLength Property = "branch_max_length" + // WorktreeCountIcon shows before the worktree context + WorktreeCountIcon Property = "worktree_count_icon" +) + +func (g *git) renderDeprecatedString(displayStatus bool) string { + if !displayStatus { + return g.getPrettyHEADName() + } + buffer := new(bytes.Buffer) + // remote (if available) + if len(g.repo.UpstreamIcon) != 0 { + fmt.Fprintf(buffer, "%s", g.repo.UpstreamIcon) + } + // branchName + fmt.Fprintf(buffer, "%s", g.repo.HEAD) + if g.props.getBool(DisplayBranchStatus, true) { + buffer.WriteString(g.getBranchStatus()) + } + if g.repo.Staging.Changed { + 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 { + fmt.Fprint(buffer, g.getStatusDetailString(g.repo.Working, WorkingColor, LocalWorkingIcon, " \uF044")) + } + if g.repo.StashCount != 0 { + fmt.Fprintf(buffer, " %s%d", g.props.getString(StashCountIcon, "\uF692 "), g.repo.StashCount) + } + if g.repo.WorktreeCount != 0 { + fmt.Fprintf(buffer, " %s%d", g.props.getString(WorktreeCountIcon, "\uf1bb "), g.repo.WorktreeCount) + } + return buffer.String() +} diff --git a/src/segment_git_test.go b/src/segment_git_test.go index 26a89df5..1024eacd 100644 --- a/src/segment_git_test.go +++ b/src/segment_git_test.go @@ -564,35 +564,35 @@ func bootstrapUpstreamTest(upstream string) *git { func TestGetUpstreamSymbolGitHub(t *testing.T) { g := bootstrapUpstreamTest("github.com/test") - upstreamIcon := g.getUpstreamSymbol() + upstreamIcon := g.getUpstreamIcon() assert.Equal(t, "GH", upstreamIcon) } func TestGetUpstreamSymbolGitLab(t *testing.T) { g := bootstrapUpstreamTest("gitlab.com/test") - upstreamIcon := g.getUpstreamSymbol() + upstreamIcon := g.getUpstreamIcon() assert.Equal(t, "GL", upstreamIcon) } func TestGetUpstreamSymbolBitBucket(t *testing.T) { g := bootstrapUpstreamTest("bitbucket.org/test") - upstreamIcon := g.getUpstreamSymbol() + upstreamIcon := g.getUpstreamIcon() assert.Equal(t, "BB", upstreamIcon) } func TestGetUpstreamSymbolAzureDevOps(t *testing.T) { g := bootstrapUpstreamTest("dev.azure.com/test") - upstreamIcon := g.getUpstreamSymbol() + upstreamIcon := g.getUpstreamIcon() assert.Equal(t, "AD", upstreamIcon) g = bootstrapUpstreamTest("test.visualstudio.com") - upstreamIcon = g.getUpstreamSymbol() + upstreamIcon = g.getUpstreamIcon() assert.Equal(t, "AD", upstreamIcon) } func TestGetUpstreamSymbolGit(t *testing.T) { g := bootstrapUpstreamTest("gitstash.com/test") - upstreamIcon := g.getUpstreamSymbol() + upstreamIcon := g.getUpstreamIcon() assert.Equal(t, "G", upstreamIcon) }