diff --git a/CHANGELOG b/CHANGELOG index dd281fd..5ea7307 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -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) diff --git a/textview.go b/textview.go index 684be27..e5e1c68 100644 --- a/textview.go +++ b/textview.go @@ -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 diff --git a/util.go b/util.go index 43bd29a..01ca863 100644 --- a/util.go +++ b/util.go @@ -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 {