diff options
Diffstat (limited to 'ui/messages')
-rw-r--r-- | ui/messages/textbase.go | 26 | ||||
-rw-r--r-- | ui/messages/tstring/string.go | 17 |
2 files changed, 24 insertions, 19 deletions
diff --git a/ui/messages/textbase.go b/ui/messages/textbase.go index c241c0a..f805067 100644 --- a/ui/messages/textbase.go +++ b/ui/messages/textbase.go @@ -44,6 +44,18 @@ var ( spacePattern = regexp.MustCompile(`\s+`) ) +func matchBoundaryPattern(extract tstring.TString) tstring.TString { + matches := boundaryPattern.FindAllStringIndex(extract.String(), -1) + if len(matches) > 0 { + if match := matches[len(matches)-1]; len(match) >= 2 { + if until := match[1]; until < len(extract) { + extract = extract[:until] + } + } + } + return extract +} + // CalculateBuffer generates the internal buffer for this message that consists // of the text of this message split into lines at most as wide as the width // parameter. @@ -63,24 +75,14 @@ func (msg *BaseTextMessage) calculateBufferWithText(text tstring.TString, width } else { newlines = 0 } - // Mostly from tview/textview.go#reindexBuffer() + // Adapted from tview/textview.go#reindexBuffer() for len(str) > 0 { extract := str.Truncate(width) if len(extract) < len(str) { if spaces := spacePattern.FindStringIndex(str[len(extract):].String()); spaces != nil && spaces[0] == 0 { extract = str[:len(extract)+spaces[1]] } - - matches := boundaryPattern.FindAllStringIndex(extract.String(), -1) - if len(matches) > 0 { - match := matches[len(matches)-1] - if len(match) >= 2 { - until := match[1] - if until < len(extract) { - extract = extract[:until] - } - } - } + extract = matchBoundaryPattern(extract) } msg.buffer = append(msg.buffer, extract) str = str[len(extract):] diff --git a/ui/messages/tstring/string.go b/ui/messages/tstring/string.go index a87d16a..4f3ee29 100644 --- a/ui/messages/tstring/string.go +++ b/ui/messages/tstring/string.go @@ -67,19 +67,22 @@ func (str TString) Append(data string) TString { } func (str TString) AppendColor(data string, color tcell.Color) TString { - newStr := make(TString, len(str)+len(data)) - copy(newStr, str) - for i, char := range data { - newStr[i+len(str)] = NewColorCell(char, color) - } - return newStr + return str.AppendCustom(data, func(r rune) Cell { + return NewColorCell(r, color) + }) } func (str TString) AppendStyle(data string, style tcell.Style) TString { + return str.AppendCustom(data, func(r rune) Cell { + return NewStyleCell(r, style) + }) +} + +func (str TString) AppendCustom(data string, cellCreator func(rune) Cell) TString { newStr := make(TString, len(str)+len(data)) copy(newStr, str) for i, char := range data { - newStr[i+len(str)] = NewStyleCell(char, style) + newStr[i+len(str)] = cellCreator(char) } return newStr } |