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-04-10 15:07:16 +02:00
|
|
|
"time"
|
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 {
|
|
|
|
BaseTextMessage
|
|
|
|
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.
|
|
|
|
func NewTextMessage(id, sender, msgtype, text string, timestamp time.Time) UIMessage {
|
2018-04-13 20:25:45 +02:00
|
|
|
return &TextMessage{
|
|
|
|
BaseTextMessage: newBaseTextMessage(id, sender, msgtype, timestamp),
|
2018-04-09 22:45:54 +02:00
|
|
|
MsgText: text,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-04-13 20:25:45 +02:00
|
|
|
// CopyFrom replaces the content of this message object with the content of the given object.
|
|
|
|
func (msg *TextMessage) CopyFrom(from ifc.MessageMeta) {
|
|
|
|
msg.BaseTextMessage.CopyFrom(from)
|
2018-04-09 22:45:54 +02:00
|
|
|
|
2018-04-13 20:25:45 +02:00
|
|
|
fromTextMsg, ok := from.(*TextMessage)
|
|
|
|
if ok {
|
|
|
|
msg.MsgText = fromTextMsg.MsgText
|
2018-04-09 22:45:54 +02:00
|
|
|
}
|
|
|
|
|
2018-04-13 20:25:45 +02:00
|
|
|
msg.cache = nil
|
|
|
|
msg.RecalculateBuffer()
|
2018-04-09 22:45:54 +02:00
|
|
|
}
|
2018-04-13 20:25:45 +02:00
|
|
|
func (msg *TextMessage) SetType(msgtype string) {
|
|
|
|
msg.BaseTextMessage.SetType(msgtype)
|
|
|
|
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) {
|
|
|
|
msg.BaseTextMessage.SetState(state)
|
|
|
|
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) {
|
|
|
|
msg.BaseTextMessage.SetIsHighlight(isHighlight)
|
|
|
|
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) {
|
|
|
|
msg.BaseTextMessage.SetIsService(isService)
|
|
|
|
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-04-13 20:25:45 +02:00
|
|
|
func (msg *TextMessage) CalculateBuffer(width int) {
|
|
|
|
msg.BaseTextMessage.calculateBufferWithText(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() {
|
|
|
|
msg.CalculateBuffer(msg.prevBufferWidth)
|
2018-04-09 22:45:54 +02:00
|
|
|
}
|