From 36e57d16bd9f3167c09def1879aac15e884a5752 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Sat, 13 Jan 2024 14:10:34 +0100 Subject: [PATCH] Don't try to shorten branch names that are already 3 characters or less This fixes a potential crash when the available width is very small and the branch name is one to three characters long. --- pkg/gui/presentation/branches.go | 3 ++- pkg/gui/presentation/branches_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/pkg/gui/presentation/branches.go b/pkg/gui/presentation/branches.go index a2bb97ed2..bb1f0bdc3 100644 --- a/pkg/gui/presentation/branches.go +++ b/pkg/gui/presentation/branches.go @@ -78,7 +78,8 @@ func getBranchDisplayStrings( nameTextStyle = theme.DiffTerminalColor } - if len(displayName) > availableWidth { + // Don't bother shortening branch names that are already 3 characters or less + if len(displayName) > utils.Max(availableWidth, 3) { // Never shorten the branch name to less then 3 characters len := utils.Max(availableWidth, 4) displayName = displayName[:len-1] + "…" diff --git a/pkg/gui/presentation/branches_test.go b/pkg/gui/presentation/branches_test.go index 2c8374feb..2414572ca 100644 --- a/pkg/gui/presentation/branches_test.go +++ b/pkg/gui/presentation/branches_test.go @@ -176,6 +176,33 @@ func Test_getBranchDisplayStrings(t *testing.T) { checkedOutByWorktree: false, expected: []string{"1m", "branc… Pushing |"}, }, + { + branch: &models.Branch{Name: "abc", Recency: "1m"}, + itemOperation: types.ItemOperationPushing, + fullDescription: false, + viewWidth: -1, + useIcons: false, + checkedOutByWorktree: false, + expected: []string{"1m", "abc Pushing |"}, + }, + { + branch: &models.Branch{Name: "ab", Recency: "1m"}, + itemOperation: types.ItemOperationPushing, + fullDescription: false, + viewWidth: -1, + useIcons: false, + checkedOutByWorktree: false, + expected: []string{"1m", "ab Pushing |"}, + }, + { + branch: &models.Branch{Name: "a", Recency: "1m"}, + itemOperation: types.ItemOperationPushing, + fullDescription: false, + viewWidth: -1, + useIcons: false, + checkedOutByWorktree: false, + expected: []string{"1m", "a Pushing |"}, + }, { branch: &models.Branch{ Name: "branch_name",