aboutsummaryrefslogtreecommitdiff
path: root/ui/messages/textmessage.go
blob: 8ad3168e49b9d3e858dba80338c5a2b73692135e (plain)
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// gomuks - A terminal Matrix client written in Go.
// Copyright (C) 2018 Tulir Asokan
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

package messages

import (
	"encoding/gob"
	"fmt"
	"regexp"
	"time"

	"github.com/gdamore/tcell"
	"maunium.net/go/gomuks/interface"
	"maunium.net/go/gomuks/ui/widget"
)

func init() {
	gob.Register(&UITextMessage{})
	gob.Register(&UIExpandedTextMessage{})
}

type UIExpandedTextMessage struct {
	UITextMessage
	MsgUIStringText UIString
}

// NewExpandedTextMessage creates a new UIExpandedTextMessage object with the provided values and the default state.
func NewExpandedTextMessage(id, sender, msgtype string, text UIString, timestamp time.Time) UIMessage {
	return &UIExpandedTextMessage{
		UITextMessage{
			MsgSender:       sender,
			MsgTimestamp:    timestamp,
			MsgSenderColor:  widget.GetHashColor(sender),
			MsgType:         msgtype,
			MsgText:         text.String(),
			MsgID:           id,
			prevBufferWidth: 0,
			MsgState:        ifc.MessageStateDefault,
			MsgIsHighlight:  false,
			MsgIsService:    false,
		},
		text,
	}
}

func (msg *UIExpandedTextMessage) GetUIStringText() UIString {
	return msg.MsgUIStringText
}

// CopyFrom replaces the content of this message object with the content of the given object.
func (msg *UIExpandedTextMessage) CopyFrom(from ifc.MessageMeta) {
	msg.MsgSender = from.Sender()
	msg.MsgSenderColor = from.SenderColor()

	fromMsg, ok := from.(UIMessage)
	if ok {
		msg.MsgSender = fromMsg.RealSender()
		msg.MsgID = fromMsg.ID()
		msg.MsgType = fromMsg.Type()
		msg.MsgTimestamp = fromMsg.Timestamp()
		msg.MsgState = fromMsg.State()
		msg.MsgIsService = fromMsg.IsService()
		msg.MsgIsHighlight = fromMsg.IsHighlight()
		msg.buffer = nil

		fromExpandedMsg, ok := from.(*UIExpandedTextMessage)
		if ok {
			msg.MsgUIStringText = fromExpandedMsg.MsgUIStringText
		} else {
			msg.MsgUIStringText = NewColorUIString(fromMsg.Text(), from.TextColor())
		}

		msg.RecalculateBuffer()
	}
}

type UITextMessage struct {
	MsgID           string
	MsgType         string
	MsgSender       string
	MsgSenderColor  tcell.Color
	MsgTimestamp    time.Time
	MsgText         string
	MsgState        ifc.MessageState
	MsgIsHighlight  bool
	MsgIsService    bool
	buffer          []UIString
	prevBufferWidth int
}

// NewTextMessage creates a new UITextMessage object with the provided values and the default state.
func NewTextMessage(id, sender, msgtype, text string, timestamp time.Time) UIMessage {
	return &UITextMessage{
		MsgSender:       sender,
		MsgTimestamp:    timestamp,
		MsgSenderColor:  widget.GetHashColor(sender),
		MsgType:         msgtype,
		MsgText:         text,
		MsgID:           id,
		prevBufferWidth: 0,
		MsgState:        ifc.MessageStateDefault,
		MsgIsHighlight:  false,
		MsgIsService:    false,
	}
}

// CopyFrom replaces the content of this message object with the content of the given object.
func (msg *UITextMessage) CopyFrom(from ifc.MessageMeta) {
	msg.MsgSender = from.Sender()
	msg.MsgSenderColor = from.SenderColor()

	fromMsg, ok := from.(UIMessage)
	if ok {
		msg.MsgSender = fromMsg.RealSender()
		msg.MsgID = fromMsg.ID()
		msg.MsgType = fromMsg.Type()
		msg.MsgTimestamp = fromMsg.Timestamp()
		msg.MsgText = fromMsg.Text()
		msg.MsgState = fromMsg.State()
		msg.MsgIsService = fromMsg.IsService()
		msg.MsgIsHighlight = fromMsg.IsHighlight()
		msg.buffer = nil

		msg.RecalculateBuffer()
	}
}

// Sender gets the string that should be displayed as the sender of this message.
//
// If the message is being sent, the sender is "Sending...".
// If sending has failed, the sender is "Error".
// If the message is an emote, the sender is blank.
// In any other case, the sender is the display name of the user who sent the message.
func (msg *UITextMessage) Sender() string {
	switch msg.MsgState {
	case ifc.MessageStateSending:
		return "Sending..."
	case ifc.MessageStateFailed:
		return "Error"
	}
	switch msg.MsgType {
	case "m.emote":
		// Emotes don't show a separate sender, it's included in the buffer.
		return ""
	default:
		return msg.MsgSender
	}
}

func (msg *UITextMessage) RealSender() string {
	return msg.MsgSender
}

func (msg *UITextMessage) getStateSpecificColor() tcell.Color {
	switch msg.MsgState {
	case ifc.MessageStateSending:
		return tcell.ColorGray
	case ifc.MessageStateFailed:
		return tcell.ColorRed
	case ifc.MessageStateDefault:
		fallthrough
	default:
		return tcell.ColorDefault
	}
}

// SenderColor returns the color the name of the sender should be shown in.
//
// If the message is being sent, the color is gray.
// If sending has failed, the color is red.
//
// In any other case, the color is whatever is specified in the Message struct.
// Usually that means it is the hash-based color of the sender (see ui/widget/color.go)
func (msg *UITextMessage) SenderColor() tcell.Color {
	stateColor := msg.getStateSpecificColor()
	switch {
	case stateColor != tcell.ColorDefault:
		return stateColor
	case msg.MsgIsService:
		return tcell.ColorGray
	default:
		return msg.MsgSenderColor
	}
}

// TextColor returns the color the actual content of the message should be shown in.
func (msg *UITextMessage) TextColor() tcell.Color {
	stateColor := msg.getStateSpecificColor()
	switch {
	case stateColor != tcell.ColorDefault:
		return stateColor
	case msg.MsgIsService:
		return tcell.ColorGray
	case msg.MsgIsHighlight:
		return tcell.ColorYellow
	case msg.MsgType == "m.room.member":
		return tcell.ColorGreen
	default:
		return tcell.ColorDefault
	}
}

// TimestampColor returns the color the timestamp should be shown in.
//
// As with SenderColor(), messages being sent and messages that failed to be sent are
// gray and red respectively.
//
// However, other messages are the default color instead of a color stored in the struct.
func (msg *UITextMessage) TimestampColor() tcell.Color {
	return msg.getStateSpecificColor()
}

// RecalculateBuffer calculates the buffer again with the previously provided width.
func (msg *UITextMessage) RecalculateBuffer() {
	msg.CalculateBuffer(msg.prevBufferWidth)
}

// Buffer returns the computed text buffer.
//
// The buffer contains the text of the message split into lines with a maximum
// width of whatever was provided to CalculateBuffer().
//
// N.B. This will NOT automatically calculate the buffer if it hasn't been
//      calculated already, as that requires the target width.
func (msg *UITextMessage) Buffer() []UIString {
	return msg.buffer
}

// Height returns the number of rows in the computed buffer (see Buffer()).
func (msg *UITextMessage) Height() int {
	return len(msg.buffer)
}

// Timestamp returns the full timestamp when the message was sent.
func (msg *UITextMessage) Timestamp() time.Time {
	return msg.MsgTimestamp
}

// FormatTime returns the formatted time when the message was sent.
func (msg *UITextMessage) FormatTime() string {
	return msg.MsgTimestamp.Format(TimeFormat)
}

// FormatDate returns the formatted date when the message was sent.
func (msg *UITextMessage) FormatDate() string {
	return msg.MsgTimestamp.Format(DateFormat)
}

func (msg *UITextMessage) ID() string {
	return msg.MsgID
}

func (msg *UITextMessage) SetID(id string) {
	msg.MsgID = id
}

func (msg *UITextMessage) Type() string {
	return msg.MsgType
}

func (msg *UITextMessage) SetType(msgtype string) {
	msg.MsgType = msgtype
}

func (msg *UITextMessage) Text() string {
	return msg.MsgText
}

func (msg *UITextMessage) SetText(text string) {
	msg.MsgText = text
}

func (msg *UITextMessage) State() ifc.MessageState {
	return msg.MsgState
}

func (msg *UITextMessage) SetState(state ifc.MessageState) {
	msg.MsgState = state
}

func (msg *UITextMessage) IsHighlight() bool {
	return msg.MsgIsHighlight
}

func (msg *UITextMessage) SetIsHighlight(isHighlight bool) {
	msg.MsgIsHighlight = isHighlight
}

func (msg *UITextMessage) IsService() bool {
	return msg.MsgIsService
}

func (msg *UITextMessage) SetIsService(isService bool) {
	msg.MsgIsService = isService
}

func (msg *UITextMessage) GetUIStringText() UIString {
	return NewColorUIString(msg.Text(), msg.TextColor())
}

// Regular expressions used to split lines when calculating the buffer.
//
// From tview/textview.go
var (
	boundaryPattern = regexp.MustCompile("([[:punct:]]\\s*|\\s+)")
	spacePattern    = regexp.MustCompile(`\s+`)
)

// 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.
func (msg *UITextMessage) CalculateBuffer(width int) {
	if width < 2 {
		return
	}

	msg.buffer = []UIString{}
	text := msg.GetUIStringText()
	if msg.MsgType == "m.emote" {
		text = NewColorUIString(fmt.Sprintf("* %s %s", msg.MsgSender, text.String()), msg.TextColor())
		text.Colorize(2, len(msg.MsgSender), msg.SenderColor())
	}

	forcedLinebreaks := text.Split('\n')
	newlines := 0
	for _, str := range forcedLinebreaks {
		if len(str) == 0 && newlines < 1 {
			msg.buffer = append(msg.buffer, UIString{})
			newlines++
		} else {
			newlines = 0
		}
		// Mostly 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 {
					extract = extract[:matches[len(matches)-1][1]]
				}
			}
			msg.buffer = append(msg.buffer, extract)
			str = str[len(extract):]
		}
	}
	msg.prevBufferWidth = width
}