2018-04-09 22:45:54 +02:00
|
|
|
// 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"
|
2018-09-05 09:55:48 +02:00
|
|
|
"maunium.net/go/gomatrix"
|
2018-04-10 15:07:16 +02:00
|
|
|
"time"
|
2018-04-09 22:45:54 +02:00
|
|
|
|
2018-06-01 23:44:21 +02:00
|
|
|
"maunium.net/go/gomuks/config"
|
2018-04-09 22:45:54 +02:00
|
|
|
"maunium.net/go/gomuks/interface"
|
2018-04-11 18:20:40 +02:00
|
|
|
"maunium.net/go/gomuks/ui/messages/tstring"
|
2018-04-09 22:45:54 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2018-04-13 20:25:45 +02:00
|
|
|
gob.Register(&TextMessage{})
|
2018-04-09 22:45:54 +02:00
|
|
|
}
|
|
|
|
|
2018-04-13 20:25:45 +02:00
|
|
|
type TextMessage struct {
|
2018-05-22 21:06:48 +02:00
|
|
|
BaseMessage
|
2018-04-13 20:25:45 +02:00
|
|
|
cache tstring.TString
|
|
|
|
MsgText string
|
2018-04-09 22:45:54 +02:00
|
|
|
}
|
|
|
|
|
2018-04-10 18:31:28 +02:00
|
|
|
// NewTextMessage creates a new UITextMessage object with the provided values and the default state.
|
2018-09-05 09:55:48 +02:00
|
|
|
func NewTextMessage(id, sender, displayname string, msgtype gomatrix.MessageType, text string, timestamp time.Time) UIMessage {
|
2018-04-13 20:25:45 +02:00
|
|
|
return &TextMessage{
|
2018-05-22 21:06:48 +02:00
|
|
|
BaseMessage: newBaseMessage(id, sender, displayname, msgtype, timestamp),
|
2018-06-01 23:44:21 +02:00
|
|
|
MsgText: text,
|
2018-04-09 22:45:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-13 20:25:45 +02:00
|
|
|
func (msg *TextMessage) getCache() tstring.TString {
|
|
|
|
if msg.cache == nil {
|
|
|
|
switch msg.MsgType {
|
|
|
|
case "m.emote":
|
|
|
|
msg.cache = tstring.NewColorTString(fmt.Sprintf("* %s %s", msg.MsgSender, msg.MsgText), msg.TextColor())
|
|
|
|
msg.cache.Colorize(0, len(msg.MsgSender)+2, msg.SenderColor())
|
|
|
|
default:
|
|
|
|
msg.cache = tstring.NewColorTString(msg.MsgText, msg.TextColor())
|
|
|
|
}
|
2018-04-09 22:45:54 +02:00
|
|
|
}
|
2018-04-13 20:25:45 +02:00
|
|
|
return msg.cache
|
2018-04-09 22:45:54 +02:00
|
|
|
}
|
|
|
|
|
2018-09-05 09:55:48 +02:00
|
|
|
func (msg *TextMessage) SetType(msgtype gomatrix.MessageType) {
|
2018-05-22 21:06:48 +02:00
|
|
|
msg.BaseMessage.SetType(msgtype)
|
2018-04-13 20:25:45 +02:00
|
|
|
msg.cache = nil
|
2018-04-09 22:45:54 +02:00
|
|
|
}
|
|
|
|
|
2018-04-13 20:25:45 +02:00
|
|
|
func (msg *TextMessage) SetState(state ifc.MessageState) {
|
2018-05-22 21:06:48 +02:00
|
|
|
msg.BaseMessage.SetState(state)
|
2018-04-13 20:25:45 +02:00
|
|
|
msg.cache = nil
|
2018-04-09 22:45:54 +02:00
|
|
|
}
|
|
|
|
|
2018-04-13 20:25:45 +02:00
|
|
|
func (msg *TextMessage) SetIsHighlight(isHighlight bool) {
|
2018-05-22 21:06:48 +02:00
|
|
|
msg.BaseMessage.SetIsHighlight(isHighlight)
|
2018-04-13 20:25:45 +02:00
|
|
|
msg.cache = nil
|
2018-04-09 22:45:54 +02:00
|
|
|
}
|
|
|
|
|
2018-04-13 20:25:45 +02:00
|
|
|
func (msg *TextMessage) SetIsService(isService bool) {
|
2018-05-22 21:06:48 +02:00
|
|
|
msg.BaseMessage.SetIsService(isService)
|
2018-04-13 20:25:45 +02:00
|
|
|
msg.cache = nil
|
2018-04-09 22:45:54 +02:00
|
|
|
}
|
|
|
|
|
2018-04-13 20:25:45 +02:00
|
|
|
func (msg *TextMessage) NotificationContent() string {
|
2018-04-09 22:45:54 +02:00
|
|
|
return msg.MsgText
|
|
|
|
}
|
|
|
|
|
2018-05-22 21:06:48 +02:00
|
|
|
func (msg *TextMessage) PlainText() string {
|
|
|
|
return msg.MsgText
|
|
|
|
}
|
|
|
|
|
2018-06-01 23:43:56 +02:00
|
|
|
func (msg *TextMessage) CalculateBuffer(prefs config.UserPreferences, width int) {
|
|
|
|
msg.calculateBufferWithText(prefs, msg.getCache(), width)
|
2018-04-09 22:45:54 +02:00
|
|
|
}
|
|
|
|
|
2018-04-13 20:25:45 +02:00
|
|
|
// RecalculateBuffer calculates the buffer again with the previously provided width.
|
|
|
|
func (msg *TextMessage) RecalculateBuffer() {
|
2018-06-01 23:43:56 +02:00
|
|
|
msg.CalculateBuffer(msg.prevPrefs, msg.prevBufferWidth)
|
2018-04-09 22:45:54 +02:00
|
|
|
}
|