From 65a28c4c3b349b1ac7d53a834ca9cf1dffa85a5e Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Mon, 11 Nov 2024 20:04:47 +0100 Subject: [PATCH] Convert tabs to spaces in WrapViewLinesToWidth We haven't needed this before since we were only using the function for text in confirmations and menus, which is unlikely to contain tabs. We are going to use it for patches in the staging view though, which often do. --- pkg/utils/lines.go | 9 +++++++++ pkg/utils/lines_test.go | 9 +++++++++ 2 files changed, 18 insertions(+) diff --git a/pkg/utils/lines.go b/pkg/utils/lines.go index 740d4a14c..ec27d0955 100644 --- a/pkg/utils/lines.go +++ b/pkg/utils/lines.go @@ -115,6 +115,15 @@ func WrapViewLinesToWidth(wrap bool, text string, width int) []string { wrappedLines := make([]string, 0, len(lines)) for _, line := range lines { + // convert tabs to spaces + for i := 0; i < len(line); i++ { + if line[i] == '\t' { + numSpaces := 4 - (i % 4) + line = line[:i] + " "[:numSpaces] + line[i+1:] + i += numSpaces - 1 + } + } + n := 0 offset := 0 lastWhitespaceIndex := -1 diff --git a/pkg/utils/lines_test.go b/pkg/utils/lines_test.go index eb319d619..85fbbcc3d 100644 --- a/pkg/utils/lines_test.go +++ b/pkg/utils/lines_test.go @@ -334,6 +334,15 @@ func TestWrapViewLinesToWidth(t *testing.T) { "drifting blah blah", }, }, + { + name: "Tabs", + wrap: true, + text: "\ta\tbb\tccc\tdddd\teeeee", + width: 50, + expectedWrappedLines: []string{ + " a bb ccc dddd eeeee", + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {