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.
This commit is contained in:
Stefan Haller 2024-11-11 20:04:47 +01:00
parent 1f2cb35cc9
commit 65a28c4c3b
2 changed files with 18 additions and 0 deletions

View file

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

View file

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