1
0
mirror of https://github.com/rivo/tview.git synced 2025-04-28 13:48:53 +08:00

Bugfix in string decomposition: Escape sequences cannot be handled after the fact. Fixes #448

This commit is contained in:
Oliver 2020-05-28 10:36:26 +02:00
parent 8aa2912f24
commit d465381941

35
util.go
View File

@ -198,32 +198,31 @@ func decomposeString(text string, findColors, findRegions bool) (colorIndices []
} }
// Make a (sorted) list of all tags. // Make a (sorted) list of all tags.
var allIndices [][]int allIndices := make([][3]int, 0, len(colorIndices)+len(regionIndices)+len(escapeIndices))
if findColors && findRegions { for indexType, index := range [][][]int{colorIndices, regionIndices, escapeIndices} {
allIndices = colorIndices for _, tag := range index {
allIndices = make([][]int, len(colorIndices)+len(regionIndices)) allIndices = append(allIndices, [3]int{tag[0], tag[1], indexType})
copy(allIndices, colorIndices) }
copy(allIndices[len(colorIndices):], regionIndices)
sort.Slice(allIndices, func(i int, j int) bool {
return allIndices[i][0] < allIndices[j][0]
})
} else if findColors {
allIndices = colorIndices
} else {
allIndices = regionIndices
} }
sort.Slice(allIndices, func(i int, j int) bool {
return allIndices[i][0] < allIndices[j][0]
})
// Remove the tags from the original string. // Remove the tags from the original string.
var from int var from int
buf := make([]byte, 0, len(text)) buf := make([]byte, 0, len(text))
for _, indices := range allIndices { for _, indices := range allIndices {
buf = append(buf, []byte(text[from:indices[0]])...) if indices[2] == 2 { // Escape sequences are not simply removed.
from = indices[1] buf = append(buf, []byte(text[from:indices[1]-2])...)
buf = append(buf, ']')
from = indices[1]
} else {
buf = append(buf, []byte(text[from:indices[0]])...)
from = indices[1]
}
} }
buf = append(buf, text[from:]...) buf = append(buf, text[from:]...)
stripped = string(buf)
// Escape string.
stripped = string(escapePattern.ReplaceAll(buf, []byte("[$1$2]")))
// Get the width of the stripped string. // Get the width of the stripped string.
width = stringWidth(stripped) width = stringWidth(stripped)