From bcdfb8180bf30b0fa35e35181288d7da86808963 Mon Sep 17 00:00:00 2001 From: Ted Ballou Date: Thu, 18 Nov 2021 13:58:57 -0500 Subject: [PATCH] feat(git): add symbol to the branch name when truncating --- docs/docs/segment-git.mdx | 1 + src/segment_git.go | 5 ++++- src/segment_git_test.go | 28 ++++++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/docs/docs/segment-git.mdx b/docs/docs/segment-git.mdx index 8a5b5b30..79cded01 100644 --- a/docs/docs/segment-git.mdx +++ b/docs/docs/segment-git.mdx @@ -76,6 +76,7 @@ You can set the following properties to `true` to enable fetching additional inf - branch_behind_icon: `string` - the icon to display when the local branch is behind its remote - defaults to `\u2193` - branch_gone_icon: `string` - the icon to display when there's no remote branch - defaults to `\u2262` - branch_max_length: `int` - the max length for the displayed branch name where `0` implies full length - defaults to `0` +- truncate_symbol: `string` - the icon to display when a branch name is truncated - defaults to empty #### HEAD diff --git a/src/segment_git.go b/src/segment_git.go index b312c9dc..7fc6d460 100644 --- a/src/segment_git.go +++ b/src/segment_git.go @@ -95,6 +95,8 @@ const ( // BranchMaxLength truncates the length of the branch name BranchMaxLength Property = "branch_max_length" + // TruncateSymbol appends the set symbol to a truncated branch name + TruncateSymbol Property = "truncate_symbol" // BranchIcon the icon to use as branch indicator BranchIcon Property = "branch_icon" // BranchIdenticalIcon the icon to display when the remote and local branch are identical @@ -384,7 +386,8 @@ func (g *git) truncateBranch(branch string) string { if maxLength == 0 || len(branch) < maxLength { return branch } - return branch[0:maxLength] + symbol := g.props.getString(TruncateSymbol, "") + return branch[0:maxLength] + symbol } func (g *git) hasGitFile(file string) bool { diff --git a/src/segment_git_test.go b/src/segment_git_test.go index 4fde6b30..e941247d 100644 --- a/src/segment_git_test.go +++ b/src/segment_git_test.go @@ -667,6 +667,34 @@ func TestTruncateBranch(t *testing.T) { } } +func TestTruncateBranchWithSymbol(t *testing.T) { + cases := []struct { + Case string + Expected string + Branch string + MaxLength interface{} + TruncateSymbol interface{} + }{ + {Case: "No limit", Expected: "all-your-base-are-belong-to-us", Branch: "all-your-base-are-belong-to-us", TruncateSymbol: "..."}, + {Case: "No limit - larger", Expected: "all-your-base...", Branch: "all-your-base-are-belong-to-us", MaxLength: 13.0, TruncateSymbol: "..."}, + {Case: "No limit - smaller", Expected: "all-your-base", Branch: "all-your-base", MaxLength: 16.0, TruncateSymbol: "..."}, + {Case: "Invalid setting", Expected: "all-your-base", Branch: "all-your-base", MaxLength: "burp", TruncateSymbol: "..."}, + {Case: "Lower than limit", Expected: "all-your-base", Branch: "all-your-base", MaxLength: 20.0, TruncateSymbol: "..."}, + } + + for _, tc := range cases { + g := &git{ + props: &properties{ + values: map[Property]interface{}{ + BranchMaxLength: tc.MaxLength, + TruncateSymbol: tc.TruncateSymbol, + }, + }, + } + assert.Equal(t, tc.Expected, g.truncateBranch(tc.Branch), tc.Case) + } +} + func TestGetGitCommand(t *testing.T) { cases := []struct { Case string