Skip to content

Commit 5e9fe2b

Browse files
authored
Fix truncation of branch names containing non-ASCII characters (#3685)
Fix truncating long branch names containing non-ASCII characters.
2 parents a171ec4 + d406ec0 commit 5e9fe2b

File tree

4 files changed

+34
-9
lines changed

4 files changed

+34
-9
lines changed

pkg/gui/presentation/branches.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@ func getBranchDisplayStrings(
7979
}
8080

8181
// Don't bother shortening branch names that are already 3 characters or less
82-
if len(displayName) > max(availableWidth, 3) {
82+
if runewidth.StringWidth(displayName) > max(availableWidth, 3) {
8383
// Never shorten the branch name to less then 3 characters
8484
len := max(availableWidth, 4)
85-
displayName = displayName[:len-1] + "…"
85+
displayName = runewidth.Truncate(displayName, len, "…")
8686
}
8787
coloredName := nameTextStyle.Sprint(displayName)
8888
if checkedOutByWorkTree {

pkg/gui/presentation/branches_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ func Test_getBranchDisplayStrings(t *testing.T) {
4343
showDivergenceCfg: "none",
4444
expected: []string{"1m", "branch_name"},
4545
},
46+
{
47+
branch: &models.Branch{Name: "🍉_special_char", Recency: "1m"},
48+
itemOperation: types.ItemOperationNone,
49+
fullDescription: false,
50+
viewWidth: 19,
51+
useIcons: false,
52+
checkedOutByWorktree: false,
53+
showDivergenceCfg: "none",
54+
expected: []string{"1m", "🍉_special_char"},
55+
},
4656
{
4757
branch: &models.Branch{Name: "branch_name", Recency: "1m"},
4858
itemOperation: types.ItemOperationNone,
@@ -184,6 +194,16 @@ func Test_getBranchDisplayStrings(t *testing.T) {
184194
showDivergenceCfg: "none",
185195
expected: []string{"1m", "branch_na…"},
186196
},
197+
{
198+
branch: &models.Branch{Name: "🍉_special_char", Recency: "1m"},
199+
itemOperation: types.ItemOperationNone,
200+
fullDescription: false,
201+
viewWidth: 18,
202+
useIcons: false,
203+
checkedOutByWorktree: false,
204+
showDivergenceCfg: "none",
205+
expected: []string{"1m", "🍉_special_ch…"},
206+
},
187207
{
188208
branch: &models.Branch{Name: "branch_name", Recency: "1m"},
189209
itemOperation: types.ItemOperationNone,

pkg/utils/formatting.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,10 @@ func MaxFn[T any](items []T, fn func(T) int) int {
161161

162162
// TruncateWithEllipsis returns a string, truncated to a certain length, with an ellipsis
163163
func TruncateWithEllipsis(str string, limit int) string {
164-
if runewidth.StringWidth(str) > limit && limit <= 3 {
164+
if runewidth.StringWidth(str) > limit && limit <= 2 {
165165
return strings.Repeat(".", limit)
166166
}
167-
return runewidth.Truncate(str, limit, "...")
167+
return runewidth.Truncate(str, limit, "")
168168
}
169169

170170
func SafeTruncate(str string, limit int) string {

pkg/utils/formatting_test.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,22 +107,22 @@ func TestTruncateWithEllipsis(t *testing.T) {
107107
{
108108
"hello world !",
109109
3,
110-
"...",
110+
"he…",
111111
},
112112
{
113113
"hello world !",
114114
4,
115-
"h...",
115+
"hel…",
116116
},
117117
{
118118
"hello world !",
119119
5,
120-
"he...",
120+
"hell…",
121121
},
122122
{
123123
"hello world !",
124124
12,
125-
"hello wor...",
125+
"hello world…",
126126
},
127127
{
128128
"hello world !",
@@ -137,13 +137,18 @@ func TestTruncateWithEllipsis(t *testing.T) {
137137
{
138138
"大大大大",
139139
5,
140-
"大...",
140+
"大大…",
141141
},
142142
{
143143
"大大大大",
144144
2,
145145
"..",
146146
},
147+
{
148+
"大大大大",
149+
1,
150+
".",
151+
},
147152
{
148153
"大大大大",
149154
0,

0 commit comments

Comments
 (0)