From 93af0016f777fb477263d44e4f7530a7b59bc53b Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Sat, 15 Jun 2024 14:30:27 +0200 Subject: [PATCH 1/3] Use actual ellipsis character instead of ... to truncate strings Space is scarce in lazygit's UI, and using ... wastes a lot of it. --- pkg/utils/formatting.go | 4 ++-- pkg/utils/formatting_test.go | 15 ++++++++++----- 2 files changed, 12 insertions(+), 7 deletions(-) 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, From 7ec784e2a053b23809cabcad2750db6f2cac1698 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Sat, 22 Jun 2024 15:25:43 +0200 Subject: [PATCH 2/3] Add test demonstrating wrong truncation of branch names containing non-ASCII characters --- pkg/gui/presentation/branches_test.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/pkg/gui/presentation/branches_test.go b/pkg/gui/presentation/branches_test.go index ba79f16ce..14012cda2 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_c…"}, // truncated, but shouldn't + }, { 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_…"}, // truncated two runes too much + }, { branch: &models.Branch{Name: "branch_name", Recency: "1m"}, itemOperation: types.ItemOperationNone, From d406ec06af1f671e3370d120deb439af3cad7fa4 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Sat, 22 Jun 2024 13:34:41 +0200 Subject: [PATCH 3/3] Fix truncation of long branch names containing non-ASCII characters --- pkg/gui/presentation/branches.go | 4 ++-- pkg/gui/presentation/branches_test.go | 4 ++-- 2 files changed, 4 insertions(+), 4 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 14012cda2..71dc89c8a 100644 --- a/pkg/gui/presentation/branches_test.go +++ b/pkg/gui/presentation/branches_test.go @@ -51,7 +51,7 @@ func Test_getBranchDisplayStrings(t *testing.T) { useIcons: false, checkedOutByWorktree: false, showDivergenceCfg: "none", - expected: []string{"1m", "🍉_special_c…"}, // truncated, but shouldn't + expected: []string{"1m", "🍉_special_char"}, }, { branch: &models.Branch{Name: "branch_name", Recency: "1m"}, @@ -202,7 +202,7 @@ func Test_getBranchDisplayStrings(t *testing.T) { useIcons: false, checkedOutByWorktree: false, showDivergenceCfg: "none", - expected: []string{"1m", "🍉_special_…"}, // truncated two runes too much + expected: []string{"1m", "🍉_special_ch…"}, }, { branch: &models.Branch{Name: "branch_name", Recency: "1m"},