Generalize tag stripping as StripTags

This commit is contained in:
Trevor Slocum 2020-10-19 09:55:07 -07:00
parent 07ef49c867
commit fdb055d99b
3 changed files with 28 additions and 15 deletions

View File

@ -11,6 +11,7 @@ v1.5.1 (WIP)
- Allow modification of scroll bar render text
- Allow scrolling List horizontally
- Clarify that Table rows must each have the same number of columns
- Generalize tag stripping as StripTags
- Make printWithStyle public and rename as PrintStyle
- Optimize TextView (writing is 90% faster, drawing is 50% faster)
- Remove return values from methods which return their primitive (breaks chaining)

View File

@ -327,21 +327,7 @@ func (t *TextView) GetBytes(stripTags bool) []byte {
}
buffer := bytes.Join(t.buffer, []byte("\n"))
if t.regions {
buffer = regionPattern.ReplaceAll(buffer, nil)
}
if t.dynamicColors {
buffer = colorPattern.ReplaceAllFunc(buffer, func(match []byte) []byte {
if len(match) > 2 {
return nil
}
return match
})
}
if t.regions || t.dynamicColors {
buffer = escapePattern.ReplaceAll(buffer, []byte(`[$1$2]`))
}
return buffer
return StripTags(buffer, t.dynamicColors, t.regions)
}
// GetText returns the current text of this text view. If "stripTags" is set

26
util.go
View File

@ -97,6 +97,32 @@ func init() {
}
}
// StripTags returns the provided text without color and/or region tags.
func StripTags(text []byte, colors bool, regions bool) []byte {
if !colors && !regions {
stripped := make([]byte, len(text))
copy(stripped, text)
return stripped
}
var stripped []byte
src := text
if regions {
stripped = regionPattern.ReplaceAll(text, nil)
src = stripped
}
if colors {
stripped = colorPattern.ReplaceAllFunc(src, func(match []byte) []byte {
if len(match) > 2 {
return nil
}
return match
})
}
return escapePattern.ReplaceAll(stripped, []byte(`[$1$2]`))
}
// ColorHex returns the hexadecimal value of a color as a string, prefixed with #.
// If the color is invalid, a blank string is returned.
func ColorHex(c tcell.Color) string {