diff --git a/inputfield.go b/inputfield.go index d497bf9..82e54c8 100644 --- a/inputfield.go +++ b/inputfield.go @@ -573,6 +573,7 @@ func (i *InputField) InputHandler() func(event *tcell.EventKey, setFocus func(p } i.autocompleteList.SetCurrentItem(newEntry) currentText, _ = i.autocompleteList.GetItemText(newEntry) // Don't trigger changed function twice. + currentText = stripTags(currentText) i.SetText(currentText) } else { finish(key) @@ -585,6 +586,7 @@ func (i *InputField) InputHandler() func(event *tcell.EventKey, setFocus func(p } i.autocompleteList.SetCurrentItem(newEntry) currentText, _ = i.autocompleteList.GetItemText(newEntry) // Don't trigger changed function twice. + currentText = stripTags(currentText) i.SetText(currentText) } else { finish(key) diff --git a/textview.go b/textview.go index aadac20..585cbd9 100644 --- a/textview.go +++ b/textview.go @@ -269,13 +269,13 @@ func (t *TextView) SetText(text string) *TextView { return t } -// GetText returns the current text of this text view. If "stripTags" is set +// GetText returns the current text of this text view. If "stripAllTags" is set // to true, any region/color tags are stripped from the text. -func (t *TextView) GetText(stripTags bool) string { +func (t *TextView) GetText(stripAllTags bool) string { // Get the buffer. buffer := make([]string, len(t.buffer), len(t.buffer)+1) copy(buffer, t.buffer) - if !stripTags { + if !stripAllTags { buffer = append(buffer, string(t.recentBytes)) } @@ -283,19 +283,14 @@ func (t *TextView) GetText(stripTags bool) string { text := strings.Join(buffer, "\n") // Strip from tags if required. - if stripTags { + if stripAllTags { if t.regions { text = regionPattern.ReplaceAllString(text, "") } if t.dynamicColors { - text = colorPattern.ReplaceAllStringFunc(text, func(match string) string { - if len(match) > 2 { - return "" - } - return match - }) + text = stripTags(text) } - if t.regions || t.dynamicColors { + if t.regions && !t.dynamicColors { text = escapePattern.ReplaceAllString(text, `[$1$2]`) } } diff --git a/util.go b/util.go index 3bd0d3d..5ddad6b 100644 --- a/util.go +++ b/util.go @@ -628,3 +628,15 @@ func iterateStringReverse(text string, callback func(main rune, comb []rune, tex return false } + +// stripTags strips colour tags from the given string. (Region tags are not +// stripped.) +func stripTags(text string) string { + stripped := colorPattern.ReplaceAllStringFunc(text, func(match string) string { + if len(match) > 2 { + return "" + } + return match + }) + return escapePattern.ReplaceAllString(stripped, `[$1$2]`) +}