Skip to content

Commit 65a28c4

Browse files
committed
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.
1 parent 1f2cb35 commit 65a28c4

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

pkg/utils/lines.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,15 @@ func WrapViewLinesToWidth(wrap bool, text string, width int) []string {
115115
wrappedLines := make([]string, 0, len(lines))
116116

117117
for _, line := range lines {
118+
// convert tabs to spaces
119+
for i := 0; i < len(line); i++ {
120+
if line[i] == '\t' {
121+
numSpaces := 4 - (i % 4)
122+
line = line[:i] + " "[:numSpaces] + line[i+1:]
123+
i += numSpaces - 1
124+
}
125+
}
126+
118127
n := 0
119128
offset := 0
120129
lastWhitespaceIndex := -1

pkg/utils/lines_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,15 @@ func TestWrapViewLinesToWidth(t *testing.T) {
334334
"drifting blah blah",
335335
},
336336
},
337+
{
338+
name: "Tabs",
339+
wrap: true,
340+
text: "\ta\tbb\tccc\tdddd\teeeee",
341+
width: 50,
342+
expectedWrappedLines: []string{
343+
" a bb ccc dddd eeeee",
344+
},
345+
},
337346
}
338347
for _, tt := range tests {
339348
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)