| 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 html |
| 18 | |
| 19 | import ( |
| 20 | "fmt" |
| 21 | "regexp" |
| 22 | |
| 23 | "github.com/mattn/go-runewidth" |
| 24 | |
| 25 | "go.mau.fi/mauview" |
| 26 | |
| 27 | "maunium.net/go/gomuks/ui/widget" |
| 28 | ) |
| 29 | |
| 30 | type TextEntity struct { |
| 31 | *BaseEntity |
| 32 | // Text in this entity. |
| 33 | Text string |
| 34 | |
| 35 | buffer []string |
| 36 | } |
| 37 | |
| 38 | // NewTextEntity creates a new text-only Entity. |
| 39 | func NewTextEntity(text string) *TextEntity { |
| 40 | return &TextEntity{ |
| 41 | BaseEntity: &BaseEntity{ |
| 42 | Tag: "text", |
| 43 | }, |
| 44 | Text: text, |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | func (te *TextEntity) IsEmpty() bool { |
| 49 | return len(te.Text) == 0 |
| 50 | } |
| 51 | |
| 52 | func (te *TextEntity) AdjustStyle(fn AdjustStyleFunc, reason AdjustStyleReason) Entity { |
| 53 | te.BaseEntity = te.BaseEntity.AdjustStyle(fn, reason).(*BaseEntity) |
| 54 | return te |
| 55 | } |
| 56 | |
| 57 | func (te *TextEntity) Clone() Entity { |
| 58 | return &TextEntity{ |
| 59 | BaseEntity: te.BaseEntity.Clone().(*BaseEntity), |
| 60 | Text: te.Text, |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | func (te *TextEntity) PlainText() string { |
| 65 | return te.Text |
| 66 | } |
| 67 | |
| 68 | func (te *TextEntity) String() string { |
| 69 | return fmt.Sprintf("&html.TextEntity{Text=%s, Base=%s},\n", te.Text, te.BaseEntity) |
| 70 | } |
| 71 | |
| 72 | func (te *TextEntity) Draw(screen mauview.Screen, ctx DrawContext) { |
| 73 | width, _ := screen.Size() |
| 74 | x := te.startX |
| 75 | for y, line := range te.buffer { |
| 76 | widget.WriteLine(screen, mauview.AlignLeft, line, x, y, width, te.Style) |
| 77 | x = 0 |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | func (te *TextEntity) CalculateBuffer(width, startX int, ctx DrawContext) int { |
| 82 | te.BaseEntity.CalculateBuffer(width, startX, ctx) |
| 83 | if len(te.Text) == 0 { |
| 84 | return te.startX |
| 85 | } |
| 86 | te.height = 0 |
| 87 | te.prevWidth = width |
| 88 | if te.buffer == nil { |
| 89 | te.buffer = []string{} |
| 90 | } |
| 91 | bufPtr := 0 |
| 92 | text := te.Text |
| 93 | textStartX := te.startX |
| 94 | for { |
| 95 | // TODO add option no wrap and character wrap options |
| 96 | extract := runewidth.Truncate(text, width-textStartX, "") |
| 97 | extract, wordWrapped := trim(extract, text, ctx.BareMessages) |
| 98 | if !wordWrapped && textStartX > 0 { |
| 99 | if bufPtr < len(te.buffer) { |
| 100 | te.buffer[bufPtr] = "" |
| 101 | } else { |
| 102 | te.buffer = append(te.buffer, "") |
| 103 | } |
| 104 | bufPtr++ |
| 105 | textStartX = 0 |
| 106 | continue |
| 107 | } |
| 108 | if bufPtr < len(te.buffer) { |
| 109 | te.buffer[bufPtr] = extract |
| 110 | } else { |
| 111 | te.buffer = append(te.buffer, extract) |
| 112 | } |
| 113 | bufPtr++ |
| 114 | text = text[len(extract):] |
| 115 | if len(text) == 0 { |
| 116 | te.buffer = te.buffer[:bufPtr] |
| 117 | te.height += len(te.buffer) |
| 118 | // This entity is over, return the startX for the next entity |
| 119 | if te.Block { |
| 120 | // ...except if it's a block entity |
| 121 | return 0 |
| 122 | } |
| 123 | return textStartX + runewidth.StringWidth(extract) |
| 124 | } |
| 125 | textStartX = 0 |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | var ( |
| 130 | boundaryPattern = regexp.MustCompile(`([[:punct:]]\s*|\s+)`) |
| 131 | bareBoundaryPattern = regexp.MustCompile(`(\s+)`) |
| 132 | spacePattern = regexp.MustCompile(`\s+`) |
| 133 | ) |
| 134 | |
| 135 | func trim(extract, full string, bare bool) (string, bool) { |
| 136 | if len(extract) == len(full) { |
| 137 | return extract, true |
| 138 | } |
| 139 | if spaces := spacePattern.FindStringIndex(full[len(extract):]); spaces != nil && spaces[0] == 0 { |
| 140 | extract = full[:len(extract)+spaces[1]] |
| 141 | } |
| 142 | regex := boundaryPattern |
| 143 | if bare { |
| 144 | regex = bareBoundaryPattern |
| 145 | } |
| 146 | matches := regex.FindAllStringIndex(extract, -1) |
| 147 | if len(matches) > 0 { |
| 148 | if match := matches[len(matches)-1]; len(match) >= 2 { |
| 149 | if until := match[1]; until < len(extract) { |
| 150 | extract = extract[:until] |
| 151 | return extract, true |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | return extract, len(extract) > 0 && extract[len(extract)-1] == ' ' |
| 156 | } |