| 1 | // gomuks - A terminal Matrix client written in Go. |
| 2 | // Copyright (C) 2020 Tulir Asokan |
| 3 | // |
| 4 | // This program is free software: you can redistribute it and/or modify |
| 5 | // it under the terms of the GNU Affero General Public License as published by |
| 6 | // the Free Software Foundation, either version 3 of the License, or |
| 7 | // (at your option) any later version. |
| 8 | // |
| 9 | // This program is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU Affero General Public License for more details. |
| 13 | // |
| 14 | // You should have received a copy of the GNU Affero General Public License |
| 15 | // along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | |
| 17 | package messages |
| 18 | |
| 19 | import ( |
| 20 | "fmt" |
| 21 | "regexp" |
| 22 | |
| 23 | "maunium.net/go/gomuks/config" |
| 24 | "maunium.net/go/gomuks/ui/messages/tstring" |
| 25 | ) |
| 26 | |
| 27 | // Regular expressions used to split lines when calculating the buffer. |
| 28 | // |
| 29 | // From tview/textview.go |
| 30 | var ( |
| 31 | boundaryPattern = regexp.MustCompile(`([[:punct:]]\s*|\s+)`) |
| 32 | bareBoundaryPattern = regexp.MustCompile(`(\s+)`) |
| 33 | spacePattern = regexp.MustCompile(`\s+`) |
| 34 | ) |
| 35 | |
| 36 | func matchBoundaryPattern(bare bool, extract tstring.TString) tstring.TString { |
| 37 | regex := boundaryPattern |
| 38 | if bare { |
| 39 | regex = bareBoundaryPattern |
| 40 | } |
| 41 | matches := regex.FindAllStringIndex(extract.String(), -1) |
| 42 | if len(matches) > 0 { |
| 43 | if match := matches[len(matches)-1]; len(match) >= 2 { |
| 44 | if until := match[1]; until < len(extract) { |
| 45 | extract = extract[:until] |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | return extract |
| 50 | } |
| 51 | |
| 52 | // CalculateBuffer generates the internal buffer for this message that consists |
| 53 | // of the text of this message split into lines at most as wide as the width |
| 54 | // parameter. |
| 55 | func calculateBufferWithText(prefs config.UserPreferences, text tstring.TString, width int, msg *UIMessage) []tstring.TString { |
| 56 | if width < 2 { |
| 57 | return nil |
| 58 | } |
| 59 | |
| 60 | var buffer []tstring.TString |
| 61 | |
| 62 | if prefs.BareMessageView { |
| 63 | newText := tstring.NewTString(msg.FormatTime()) |
| 64 | if len(msg.Sender()) > 0 { |
| 65 | newText = newText.AppendTString(tstring.NewColorTString(fmt.Sprintf(" <%s> ", msg.Sender()), msg.SenderColor())) |
| 66 | } else { |
| 67 | newText = newText.Append(" ") |
| 68 | } |
| 69 | newText = newText.AppendTString(text) |
| 70 | text = newText |
| 71 | } |
| 72 | |
| 73 | forcedLinebreaks := text.Split('\n') |
| 74 | newlines := 0 |
| 75 | for _, str := range forcedLinebreaks { |
| 76 | if len(str) == 0 && newlines < 1 { |
| 77 | buffer = append(buffer, tstring.TString{}) |
| 78 | newlines++ |
| 79 | } else { |
| 80 | newlines = 0 |
| 81 | } |
| 82 | // Adapted from tview/textview.go#reindexBuffer() |
| 83 | for len(str) > 0 { |
| 84 | extract := str.Truncate(width) |
| 85 | if len(extract) < len(str) { |
| 86 | if spaces := spacePattern.FindStringIndex(str[len(extract):].String()); spaces != nil && spaces[0] == 0 { |
| 87 | extract = str[:len(extract)+spaces[1]] |
| 88 | } |
| 89 | extract = matchBoundaryPattern(prefs.BareMessageView, extract) |
| 90 | } |
| 91 | buffer = append(buffer, extract) |
| 92 | str = str[len(extract):] |
| 93 | } |
| 94 | } |
| 95 | return buffer |
| 96 | } |