1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-04-25 12:24:47 +02:00

Fix truncation of branch names containing non-ASCII characters (#3685)

Fix truncating long branch names containing non-ASCII characters.
This commit is contained in:
Stefan Haller 2024-06-23 12:28:05 +02:00 committed by GitHub
commit 5e9fe2be80
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 34 additions and 9 deletions

View File

@ -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 {

View File

@ -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,

View File

@ -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 {

View File

@ -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,