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 @@ -43,6 +43,16 @@ func Test_getBranchDisplayStrings(t *testing.T) { showDivergenceCfg: "none", 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, @@ -184,6 +194,16 @@ func Test_getBranchDisplayStrings(t *testing.T) { showDivergenceCfg: "none", 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, 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,13 +137,18 @@ func TestTruncateWithEllipsis(t *testing.T) { { "大大大大", 5, - "大...", + "大大…", }, { "大大大大", 2, "..", }, + { + "大大大大", + 1, + ".", + }, { "大大大大", 0,