feat(git): add symbol to the branch name when truncating

This commit is contained in:
Ted Ballou 2021-11-18 13:58:57 -05:00 committed by GitHub
parent f58567e015
commit bcdfb8180b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 1 deletions

View file

@ -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_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_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` - 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 #### HEAD

View file

@ -95,6 +95,8 @@ const (
// BranchMaxLength truncates the length of the branch name // BranchMaxLength truncates the length of the branch name
BranchMaxLength Property = "branch_max_length" 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 the icon to use as branch indicator
BranchIcon Property = "branch_icon" BranchIcon Property = "branch_icon"
// BranchIdenticalIcon the icon to display when the remote and local branch are identical // 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 { if maxLength == 0 || len(branch) < maxLength {
return branch return branch
} }
return branch[0:maxLength] symbol := g.props.getString(TruncateSymbol, "")
return branch[0:maxLength] + symbol
} }
func (g *git) hasGitFile(file string) bool { func (g *git) hasGitFile(file string) bool {

View file

@ -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) { func TestGetGitCommand(t *testing.T) {
cases := []struct { cases := []struct {
Case string Case string