1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
|
// gomuks - A terminal Matrix client written in Go.
// Copyright (C) 2019 Tulir Asokan
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package messages
import (
"fmt"
"strings"
"time"
"github.com/mattn/go-runewidth"
"maunium.net/go/gomuks/config"
"maunium.net/go/gomuks/ui/widget"
"maunium.net/go/mautrix"
"maunium.net/go/mauview"
"maunium.net/go/tcell"
)
type HTMLMessage struct {
BaseMessage
Root *HTMLEntity
}
func NewHTMLMessage(id, sender, displayname string, msgtype mautrix.MessageType, root *HTMLEntity, timestamp time.Time) UIMessage {
return &HTMLMessage{
BaseMessage: newBaseMessage(id, sender, displayname, msgtype, timestamp),
Root: root,
}
}
func (hw *HTMLMessage) Draw(screen mauview.Screen) {
hw.Root.Draw(screen)
}
func (hw *HTMLMessage) OnKeyEvent(event mauview.KeyEvent) bool {
return false
}
func (hw *HTMLMessage) OnMouseEvent(event mauview.MouseEvent) bool {
return false
}
func (hw *HTMLMessage) OnPasteEvent(event mauview.PasteEvent) bool {
return false
}
func (hw *HTMLMessage) CalculateBuffer(preferences config.UserPreferences, width int) {
if width <= 0 {
panic("Negative width in CalculateBuffer")
}
// TODO account for bare messages in initial startX
startX := 0
hw.Root.calculateBuffer(width, startX, preferences.BareMessageView)
}
func (hw *HTMLMessage) Height() int {
return hw.Root.height
}
func (hw *HTMLMessage) PlainText() string {
// FIXME
return "Plaintext unavailable"
}
func (hw *HTMLMessage) NotificationContent() string {
// FIXME
return "Notification content unavailable"
}
type HTMLEntity struct {
// Permanent variables
Tag string
Text string
Style tcell.Style
Children []*HTMLEntity
Block bool
Indent int
// Non-permanent variables (calculated buffer data)
buffer []string
prevWidth int
startX int
height int
}
func (he *HTMLEntity) AdjustStyle(fn func(tcell.Style) tcell.Style) *HTMLEntity {
for _, child := range he.Children {
child.AdjustStyle(fn)
}
he.Style = fn(he.Style)
return he
}
func (he *HTMLEntity) Draw(screen mauview.Screen) {
width, _ := screen.Size()
if len(he.buffer) > 0 {
x := he.startX
for y, line := range he.buffer {
widget.WriteLine(screen, mauview.AlignLeft, line, x, y, width, he.Style)
x = 0
}
}
if len(he.Children) > 0 {
proxyScreen := &mauview.ProxyScreen{Parent: screen, OffsetX: he.Indent, Width: width - he.Indent}
for i, entity := range he.Children {
if i != 0 && entity.startX == 0 {
proxyScreen.OffsetY++
}
proxyScreen.Height = entity.height
entity.Draw(proxyScreen)
proxyScreen.OffsetY += entity.height - 1
}
}
}
func (he *HTMLEntity) String() string {
var buf strings.Builder
buf.WriteString("&HTMLEntity{\n")
_, _ = fmt.Fprintf(&buf, ` Tag="%s", Style=%d, Block=%t, Indent=%d, startX=%d, height=%d,\n`,
he.Tag, he.Style, he.Block, he.Indent, he.startX, he.height)
_, _ = fmt.Fprintf(&buf, ` Buffer=["%s"]`, strings.Join(he.buffer, "\", \""))
if len(he.Text) > 0 {
buf.WriteString(",\n")
_, _ = fmt.Fprintf(&buf, ` Text="%s"`, he.Text)
}
if len(he.Children) > 0 {
buf.WriteString(",\n")
buf.WriteString(" Children={")
for _, child := range he.Children {
buf.WriteString("\n ")
buf.WriteString(strings.Join(strings.Split(strings.TrimRight(child.String(), "\n"), "\n"), "\n "))
}
buf.WriteString("\n },")
}
buf.WriteString("\n},\n")
return buf.String()
}
func (he *HTMLEntity) calculateBuffer(width, startX int, bare bool) int {
he.startX = startX
if he.Block {
he.startX = 0
}
he.height = 0
if len(he.Children) > 0 {
childStartX := he.startX
for _, entity := range he.Children {
if entity.Block || childStartX == 0 || he.height == 0 {
he.height++
}
childStartX = entity.calculateBuffer(width-he.Indent, childStartX, bare)
he.height += entity.height - 1
}
if len(he.Text) == 0 && !he.Block {
return childStartX
}
}
if len(he.Text) > 0 {
he.prevWidth = width
if he.buffer == nil {
he.buffer = []string{}
}
bufPtr := 0
text := he.Text
textStartX := he.startX
for {
extract := runewidth.Truncate(text, width-textStartX, "")
extract, wordWrapped := trim(extract, text, bare)
if !wordWrapped && textStartX > 0 {
if bufPtr < len(he.buffer) {
he.buffer[bufPtr] = ""
} else {
he.buffer = append(he.buffer, "")
}
bufPtr++
textStartX = 0
continue
}
if bufPtr < len(he.buffer) {
he.buffer[bufPtr] = extract
} else {
he.buffer = append(he.buffer, extract)
}
bufPtr++
text = text[len(extract):]
if len(text) == 0 {
he.buffer = he.buffer[:bufPtr]
he.height += len(he.buffer)
// This entity is over, return the startX for the next entity
if he.Block {
// ...except if it's a block entity
return 0
}
return textStartX + runewidth.StringWidth(extract)
}
textStartX = 0
}
}
return 0
}
func trim(extract, full string, bare bool) (string, bool) {
if len(extract) == len(full) {
return extract, true
}
if spaces := spacePattern.FindStringIndex(full[len(extract):]); spaces != nil && spaces[0] == 0 {
extract = full[:len(extract)+spaces[1]]
}
regex := boundaryPattern
if bare {
regex = bareBoundaryPattern
}
matches := regex.FindAllStringIndex(extract, -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, true
}
}
}
return extract, len(extract) > 0 && extract[len(extract)-1] == ' '
}
|