diff options
author | Stefan Haller <stefan@haller-berlin.de> | 2024-06-23 12:28:05 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-06-23 12:28:05 +0200 |
commit | 5e9fe2be80a067e91a5bc010a37bfeddc09cacdc (patch) | |
tree | 503b62c83824c49bf3a0c7eb0d1a72919664ca1a | |
parent | a171ec42943c048e4210bd6beac64aa138ea7c56 (diff) | |
parent | d406ec06af1f671e3370d120deb439af3cad7fa4 (diff) |
Fix truncation of branch names containing non-ASCII characters (#3685)
Fix truncating long branch names containing non-ASCII characters.
-rw-r--r-- | pkg/gui/presentation/branches.go | 4 | ||||
-rw-r--r-- | pkg/gui/presentation/branches_test.go | 20 | ||||
-rw-r--r-- | pkg/utils/formatting.go | 4 | ||||
-rw-r--r-- | pkg/utils/formatting_test.go | 15 |
4 files changed, 34 insertions, 9 deletions
diff --git a/pkg/gui/presentation/branches.go b/pkg/gui/presentation/branches.go index aab51fe61..b4c4a75c7 100644 --- a/pkg/gui/presentation/branches.go +++ b/pkg/gui/presentation/branches.go @@ -79,10 +79,10 @@ func getBranchDisplayStrings( } // Don't bother shortening branch names that are already 3 characters or less - if len(displayName) > max(availableWidth, 3) { + if runewidth.StringWidth(displayName) > max(availableWidth, 3) { // Never shorten the branch name to less then 3 characters len := max(availableWidth, 4) - displayName = displayName[:len-1] + "…" + displayName = runewidth.Truncate(displayName, len, "…") } coloredName := nameTextStyle.Sprint(displayName) if checkedOutByWorkTree { diff --git a/pkg/gui/presentation/branches_test.go b/pkg/gui/presentation/branches_test.go index ba79f16ce..71dc89c8a 100644 --- a/pkg/gui/presentation/branches_test.go +++ b/pkg/gui/presentation/branches_test.go @@ -44,6 +44,16 @@ func Test_getBranchDisplayStrings(t *testing.T) { expected: []string{"1m", "branch_name"}, }, { + branch: &models.Branch{Name: "🍉_special_char", Recency: "1m"}, + itemOperation: types.ItemOperationNone, + fullDescription: false, + viewWidth: 19, + useIcons: false, + checkedOutByWorktree: false, + showDivergenceCfg: "none", + expected: []string{"1m", "🍉_special_char"}, + }, + { branch: &models.Branch{Name: "branch_name", Recency: "1m"}, itemOperation: types.ItemOperationNone, fullDescription: false, @@ -185,6 +195,16 @@ func Test_getBranchDisplayStrings(t *testing.T) { expected: []string{"1m", "branch_na…"}, }, { + branch: &models.Branch{Name: "🍉_special_char", Recency: "1m"}, + itemOperation: types.ItemOperationNone, + fullDescription: false, + viewWidth: 18, + useIcons: false, + checkedOutByWorktree: false, + showDivergenceCfg: "none", + expected: []string{"1m", "🍉_special_ch…"}, + }, + { branch: &models.Branch{Name: "branch_name", Recency: "1m"}, itemOperation: types.ItemOperationNone, fullDescription: false, diff --git a/pkg/utils/formatting.go b/pkg/utils/formatting.go index 139946ddb..a6bbc5670 100644 --- a/pkg/utils/formatting.go +++ b/pkg/utils/formatting.go @@ -161,10 +161,10 @@ func MaxFn[T any](items []T, fn func(T) int) int { // TruncateWithEllipsis returns a string, truncated to a certain length, with an ellipsis func TruncateWithEllipsis(str string, limit int) string { - if runewidth.StringWidth(str) > limit && limit <= 3 { + if runewidth.StringWidth(str) > limit && limit <= 2 { return strings.Repeat(".", limit) } - return runewidth.Truncate(str, limit, "...") + return runewidth.Truncate(str, limit, "…") } func SafeTruncate(str string, limit int) string { diff --git a/pkg/utils/formatting_test.go b/pkg/utils/formatting_test.go index 3858fd2ec..5b56a9b33 100644 --- a/pkg/utils/formatting_test.go +++ b/pkg/utils/formatting_test.go @@ -107,22 +107,22 @@ func TestTruncateWithEllipsis(t *testing.T) { { "hello world !", 3, - "...", + "he…", }, { "hello world !", 4, - "h...", + "hel…", }, { "hello world !", 5, - "he...", + "hell…", }, { "hello world !", 12, - "hello wor...", + "hello world…", }, { "hello world !", @@ -137,7 +137,7 @@ func TestTruncateWithEllipsis(t *testing.T) { { "大大大大", 5, - "大...", + "大大…", }, { "大大大大", @@ -146,6 +146,11 @@ func TestTruncateWithEllipsis(t *testing.T) { }, { "大大大大", + 1, + ".", + }, + { + "大大大大", 0, "", }, |