| 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 messages |
| 18 |
|
| 19 |
import ( |
| 20 |
"fmt" |
| 21 |
"time" |
| 22 |
|
| 23 |
"go.mau.fi/mauview" |
| •24 |
"github.com/gdamore/tcell/v2" |
| 25 |
|
| 26 |
"maunium.net/go/gomuks/matrix/muksevt" |
| 27 |
|
| 28 |
"maunium.net/go/gomuks/config" |
| 29 |
"maunium.net/go/gomuks/ui/messages/tstring" |
| 30 |
) |
| 31 |
|
| 32 |
type ExpandedTextMessage struct { |
| 33 |
Text tstring.TString |
| 34 |
buffer []tstring.TString |
| 35 |
} |
| 36 |
|
| 37 |
// NewExpandedTextMessage creates a new ExpandedTextMessage object with the provided values and the default state. |
| 38 |
func NewExpandedTextMessage(evt *muksevt.Event, displayname string, text tstring.TString) *UIMessage { |
| 39 |
return newUIMessage(evt, displayname, &ExpandedTextMessage{ |
| 40 |
Text: text, |
| 41 |
}) |
| 42 |
} |
| 43 |
|
| 44 |
func NewServiceMessage(text string) *UIMessage { |
| 45 |
return &UIMessage{ |
| 46 |
SenderID: "*", |
| 47 |
SenderName: "*", |
| 48 |
Timestamp: time.Now(), |
| 49 |
IsService: true, |
| 50 |
Renderer: &ExpandedTextMessage{ |
| 51 |
Text: tstring.NewTString(text), |
| 52 |
}, |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
func NewDateChangeMessage(text string) *UIMessage { |
| 57 |
midnight := time.Now() |
| 58 |
midnight = time.Date(midnight.Year(), midnight.Month(), midnight.Day(), |
| 59 |
0, 0, 0, 0, |
| 60 |
midnight.Location()) |
| 61 |
return &UIMessage{ |
| 62 |
SenderID: "*", |
| 63 |
SenderName: "*", |
| 64 |
Timestamp: midnight, |
| 65 |
IsService: true, |
| 66 |
Renderer: &ExpandedTextMessage{ |
| 67 |
Text: tstring.NewColorTString(text, tcell.ColorGreen), |
| 68 |
}, |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
func (msg *ExpandedTextMessage) Clone() MessageRenderer { |
| 73 |
return &ExpandedTextMessage{ |
| 74 |
Text: msg.Text.Clone(), |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
func (msg *ExpandedTextMessage) NotificationContent() string { |
| 79 |
return msg.Text.String() |
| 80 |
} |
| 81 |
|
| 82 |
func (msg *ExpandedTextMessage) PlainText() string { |
| 83 |
return msg.Text.String() |
| 84 |
} |
| 85 |
|
| 86 |
func (msg *ExpandedTextMessage) String() string { |
| 87 |
return fmt.Sprintf(`&messages.ExpandedTextMessage{Text="%s"}`, msg.Text.String()) |
| 88 |
} |
| 89 |
|
| 90 |
func (msg *ExpandedTextMessage) CalculateBuffer(prefs config.UserPreferences, width int, uiMsg *UIMessage) { |
| 91 |
msg.buffer = calculateBufferWithText(prefs, msg.Text, width, uiMsg) |
| 92 |
} |
| 93 |
|
| 94 |
func (msg *ExpandedTextMessage) Height() int { |
| 95 |
return len(msg.buffer) |
| 96 |
} |
| 97 |
|
| 98 |
func (msg *ExpandedTextMessage) Draw(screen mauview.Screen, _ *UIMessage) { |
| 99 |
for y, line := range msg.buffer { |
| 100 |
line.Draw(screen, 0, y) |
| 101 |
} |
| 102 |
} |