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
|
from __future__ import annotations
from datetime import datetime
from math import ceil, log10
from typing import Optional, Tuple, Dict, Any, Callable
from poezio import poopt
from poezio.theming import dump_tuple, get_theme
from poezio.ui.funcs import truncate_nick
from poezio.user import User
class BaseMessage:
"""Base class for all ui-related messages"""
__slots__ = ('txt', 'time', 'identifier')
txt: str
identifier: str
time: datetime
def __init__(self, txt: str, identifier: str = '', time: Optional[datetime] = None):
self.txt = txt
self.identifier = identifier
if time is not None:
self.time = time
else:
self.time = datetime.now()
def compute_offset(self, with_timestamps: bool, nick_size: int) -> int:
"""Compute the offset of the message"""
theme = get_theme()
return theme.SHORT_TIME_FORMAT_LENGTH + 1
class EndOfArchive(BaseMessage):
"""Marker added to a buffer when we reach the end of a MAM archive"""
class InfoMessage(BaseMessage):
"""Information message"""
def __init__(self, txt: str, identifier: str = '', time: Optional[datetime] = None):
txt = ('\x19%s}' % dump_tuple(get_theme().COLOR_INFORMATION_TEXT)) + txt
super().__init__(txt=txt, identifier=identifier, time=time)
class UIMessage(BaseMessage):
"""Message displayed through poezio UI"""
__slots__ = ('level', 'color')
level: str
color: Optional[Tuple]
def __init__(self, txt: str, level: str):
BaseMessage.__init__(self, txt=txt)
self.level = level.capitalize()
colors = get_theme().INFO_COLORS
self.color = colors.get(level.lower(), colors.get('default', None))
def compute_offset(self, with_timestamps: bool, nick_size: int) -> int:
"""Compute the x-position at which the message should be printed"""
offset = 0
theme = get_theme()
if with_timestamps:
offset += 1 + theme.SHORT_TIME_FORMAT_LENGTH
level = self.level
if not level: # not a message, nothing to do afterwards
return offset
level = truncate_nick(level, nick_size) or ''
offset += poopt.wcswidth(level)
offset += 2
return offset
class LoggableTrait:
"""Trait for classes of messages that should go through the logger"""
pass
class PersistentInfoMessage(InfoMessage, LoggableTrait):
"""Information message thatt will be logged"""
pass
class MucOwnLeaveMessage(InfoMessage, LoggableTrait):
"""Status message displayed on our room leave/kick/ban"""
class MucOwnJoinMessage(InfoMessage, LoggableTrait):
"""Status message displayed on our room join"""
class XMLLog(BaseMessage):
"""XML Log message"""
__slots__ = ('incoming')
incoming: bool
def __init__(
self,
txt: str,
incoming: bool,
):
BaseMessage.__init__(
self,
txt=txt,
)
self.incoming = incoming
def compute_offset(self, with_timestamps: bool, nick_size: int) -> int:
offset = 0
theme = get_theme()
if with_timestamps:
offset += 1 + theme.SHORT_TIME_FORMAT_LENGTH
if self.incoming:
nick = theme.CHAR_XML_IN
else:
nick = theme.CHAR_XML_OUT
nick = truncate_nick(nick, nick_size) or ''
offset += 1 + len(nick)
return offset
class StatusMessage(BaseMessage):
"""A dynamically formatted status message"""
__slots__ = ('format_string', 'format_args')
format_string: str
format_args: Dict[str, Callable[[], Any]]
def __init__(self, format_string: str, format_args: dict):
BaseMessage.__init__(
self,
txt='',
)
self.format_string = format_string
self.format_args = format_args
self.rebuild()
def rebuild(self):
real_args = {}
for key, func in self.format_args.items():
real_args[key] = func()
self.txt = self.format_string.format(**real_args)
class Message(BaseMessage, LoggableTrait):
__slots__ = ('nick_color', 'nickname', 'user', 'delayed', 'history',
'highlight', 'me', 'old_message', 'revisions',
'jid', 'ack')
nick_color: Optional[Tuple]
nickname: Optional[str]
user: Optional[User]
delayed: bool
history: bool
highlight: bool
me: bool
old_message: Optional[Message]
revisions: int
jid: Optional[str]
ack: int
def __init__(self,
txt: str,
nickname: Optional[str],
time: Optional[datetime] = None,
nick_color: Optional[Tuple] = None,
delayed: bool = False,
history: bool = False,
user: Optional[User] = None,
identifier: Optional[str] = '',
highlight: bool = False,
old_message: Optional[Message] = None,
revisions: int = 0,
jid: Optional[str] = None,
ack: int = 0) -> None:
"""
Create a new Message object with parameters, check for /me messages,
and delayed messages
"""
BaseMessage.__init__(
self,
txt=txt.replace('\t', ' ') + '\x19o',
identifier=identifier or '',
time=time,
)
if txt.startswith('/me '):
me = True
txt = '\x19%s}%s\x19o' % (dump_tuple(get_theme().COLOR_ME_MESSAGE),
txt[4:])
else:
me = False
self.txt = txt
self.delayed = delayed or history
self.history = history
self.nickname = nickname
self.nick_color = nick_color
self.user = user
self.highlight = highlight
self.me = me
self.old_message = old_message
self.revisions = revisions
self.jid = jid
self.ack = ack
def _other_elems(self) -> str:
"Helper for the repr_message function"
acc = []
fields = list(self.__slots__)
fields.remove('old_message')
for field in fields:
acc.append('%s=%s' % (field, repr(getattr(self, field))))
return 'Message(%s, %s' % (', '.join(acc), 'old_message=')
def __repr__(self) -> str:
"""
repr() for the Message class, for debug purposes, since the default
repr() is recursive, so it can stack overflow given too many revisions
of a message
"""
init = self._other_elems()
acc = [init]
next_message = self.old_message
rev = 1
while next_message is not None:
acc.append(next_message._other_elems())
next_message = next_message.old_message
rev += 1
acc.append('None')
while rev:
acc.append(')')
rev -= 1
return ''.join(acc)
def compute_offset(self, with_timestamps: bool, nick_size: int) -> int:
"""Compute the x-position at which the message should be printed"""
offset = 0
theme = get_theme()
if with_timestamps:
if self.history:
offset += 1 + theme.LONG_TIME_FORMAT_LENGTH
else:
offset += 1 + theme.SHORT_TIME_FORMAT_LENGTH
if not self.nickname: # not a message, nothing to do afterwards
return offset
nick = truncate_nick(self.nickname, nick_size) or ''
offset += poopt.wcswidth(nick)
if self.ack:
theme = get_theme()
if self.ack > 0:
offset += poopt.wcswidth(theme.CHAR_ACK_RECEIVED) + 1
else:
offset += poopt.wcswidth(theme.CHAR_NACK) + 1
if self.me:
offset += 3
else:
offset += 2
if self.revisions:
offset += ceil(log10(self.revisions + 1))
return offset
|