From e55b6656e394167fccc134915a522eacabd1f1b9 Mon Sep 17 00:00:00 2001 From: Ryan Gaus Date: Fri, 26 May 2017 20:59:29 -0400 Subject: [PATCH] code: ensure that when wrapping a message across lines that words need to wrap. Fixes #9 --- gateway/printable_message.go | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/gateway/printable_message.go b/gateway/printable_message.go index 3bce5dd..4b22559 100644 --- a/gateway/printable_message.go +++ b/gateway/printable_message.go @@ -1,7 +1,7 @@ package gateway import ( - // "log" + "log" "strings" ) @@ -92,11 +92,15 @@ func (p *PrintableMessage) Lines(width int) [][]PrintableMessagePart { // "window width" // (done, since the line length < window width) - wordsInLastMessagePart := strings.Split(part.Content, " ") - for len(strings.Join(wordsInLastMessagePart, " ")) > maximumLengthOfLastMessagePart { - // Remove one word from the last message part - extraWords = append([]string{wordsInLastMessagePart[len(wordsInLastMessagePart) - 1]}, extraWords...) - wordsInLastMessagePart = wordsInLastMessagePart[:len(wordsInLastMessagePart)-1] + if maximumLengthOfLastMessagePart > 0 { // Ensure that we should remove words. Fixes #9. + wordsInLastMessagePart := strings.Split(part.Content, " ") + for len(strings.Join(wordsInLastMessagePart, " ")) > maximumLengthOfLastMessagePart { + // Remove one word from the last message part + log.Printf("len(wordsInLastMessagePart) = %d", len(wordsInLastMessagePart)) + log.Printf("wordsInLastMessagePart[-1] = %+v", wordsInLastMessagePart[len(wordsInLastMessagePart)-1]) + extraWords = append([]string{wordsInLastMessagePart[len(wordsInLastMessagePart) - 1]}, extraWords...) + wordsInLastMessagePart = wordsInLastMessagePart[:len(wordsInLastMessagePart)-1] + } } // If the last message part in a line has content to append, then add it. However, if