mirror of
https://github.com/rivo/tview.git
synced 2025-04-24 13:48:56 +08:00
Bugfix in string decomposition: Escape sequences cannot be handled after the fact. Fixes #448
This commit is contained in:
parent
8aa2912f24
commit
d465381941
35
util.go
35
util.go
@ -198,32 +198,31 @@ func decomposeString(text string, findColors, findRegions bool) (colorIndices []
|
||||
}
|
||||
|
||||
// Make a (sorted) list of all tags.
|
||||
var allIndices [][]int
|
||||
if findColors && findRegions {
|
||||
allIndices = colorIndices
|
||||
allIndices = make([][]int, len(colorIndices)+len(regionIndices))
|
||||
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
|
||||
allIndices := make([][3]int, 0, len(colorIndices)+len(regionIndices)+len(escapeIndices))
|
||||
for indexType, index := range [][][]int{colorIndices, regionIndices, escapeIndices} {
|
||||
for _, tag := range index {
|
||||
allIndices = append(allIndices, [3]int{tag[0], tag[1], indexType})
|
||||
}
|
||||
}
|
||||
sort.Slice(allIndices, func(i int, j int) bool {
|
||||
return allIndices[i][0] < allIndices[j][0]
|
||||
})
|
||||
|
||||
// Remove the tags from the original string.
|
||||
var from int
|
||||
buf := make([]byte, 0, len(text))
|
||||
for _, indices := range allIndices {
|
||||
buf = append(buf, []byte(text[from:indices[0]])...)
|
||||
from = indices[1]
|
||||
if indices[2] == 2 { // Escape sequences are not simply removed.
|
||||
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:]...)
|
||||
|
||||
// Escape string.
|
||||
stripped = string(escapePattern.ReplaceAll(buf, []byte("[$1$2]")))
|
||||
stripped = string(buf)
|
||||
|
||||
// Get the width of the stripped string.
|
||||
width = stringWidth(stripped)
|
||||
|
Loading…
x
Reference in New Issue
Block a user