From cce79ab7d8481a06054166049876c8a2cbb3418f Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Tue, 22 May 2018 17:23:54 +0300 Subject: Clean up code --- ui/messages/textbase.go | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) (limited to 'ui/messages/textbase.go') 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):] -- cgit v1.2.3