Browse Source

Generalize tag stripping as StripTags

tablepad
Trevor Slocum 2 years ago
parent
commit
fdb055d99b
  1. 1
      CHANGELOG
  2. 16
      textview.go
  3. 26
      util.go

1
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)

16
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

26
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 {

Loading…
Cancel
Save