summaryrefslogtreecommitdiff
path: root/poezio/ui
diff options
context:
space:
mode:
authormathieui <mathieui@mathieui.net>2019-09-29 18:12:48 +0200
committermathieui <mathieui@mathieui.net>2020-05-09 19:46:17 +0200
commita5e92800477f6195a6b2e8c6e64a56859117d5b8 (patch)
treef53550024091a12deeee88c0d9897d0aeba9f4bf /poezio/ui
parentfefbb0b08887b761ad7a1cf95cf6b9f5f30013a6 (diff)
downloadpoezio-a5e92800477f6195a6b2e8c6e64a56859117d5b8.tar.gz
poezio-a5e92800477f6195a6b2e8c6e64a56859117d5b8.tar.bz2
poezio-a5e92800477f6195a6b2e8c6e64a56859117d5b8.tar.xz
poezio-a5e92800477f6195a6b2e8c6e64a56859117d5b8.zip
Pass a message to add_message instead of messed up kwargs everywhere
Changes LOTS of things
Diffstat (limited to 'poezio/ui')
-rw-r--r--poezio/ui/render.py37
-rw-r--r--poezio/ui/types.py25
2 files changed, 50 insertions, 12 deletions
diff --git a/poezio/ui/render.py b/poezio/ui/render.py
index b695e8f3..3f94ecd8 100644
--- a/poezio/ui/render.py
+++ b/poezio/ui/render.py
@@ -3,10 +3,14 @@ import curses
from datetime import datetime
from functools import singledispatch
-from typing import List, Optional, Tuple
+from typing import List, Tuple
from math import ceil, log10
from poezio import poopt
+from poezio.theming import (
+ get_theme,
+)
+from poezio.windows import Win
from poezio.ui.consts import (
FORMAT_CHAR,
LONG_FORMAT,
@@ -16,12 +20,10 @@ from poezio.ui.funcs import (
truncate_nick,
parse_attrs,
)
-from poezio.theming import (
- get_theme,
-)
from poezio.ui.types import (
BaseMessage,
Message,
+ StatusMessage,
XMLLog,
)
@@ -86,6 +88,17 @@ def build_message(msg: Message, width: int, timestamp: bool, nick_size: int = 10
return []
offset = msg.compute_offset(timestamp, nick_size)
lines = poopt.cut_text(txt, width - offset - 1)
+ generated_lines = generate_lines(lines, msg, default_color='')
+ if msg.top:
+ generated_lines.reverse()
+ return generated_lines
+
+
+@build_lines.register(StatusMessage)
+def build_status(msg: StatusMessage, width: int, timestamp: bool, nick_size: int = 10) -> List[Line]:
+ msg.rebuild()
+ offset = msg.compute_offset(timestamp, nick_size)
+ lines = poopt.cut_text(msg.txt, width - offset - 1)
return generate_lines(lines, msg, default_color='')
@@ -97,7 +110,7 @@ def build_xmllog(msg: XMLLog, width: int, timestamp: bool, nick_size: int = 10)
@singledispatch
-def write_pre(msg: BaseMessage, win, with_timestamps: bool, nick_size: int) -> int:
+def write_pre(msg: BaseMessage, win: Win, with_timestamps: bool, nick_size: int) -> int:
"""Write the part before text (only the timestamp)"""
if with_timestamps:
return PreMessageHelpers.write_time(win, False, msg.time)
@@ -105,7 +118,7 @@ def write_pre(msg: BaseMessage, win, with_timestamps: bool, nick_size: int) -> i
@write_pre.register(Message)
-def write_pre_message(msg: Message, win, with_timestamps: bool, nick_size: int) -> int:
+def write_pre_message(msg: Message, win: Win, with_timestamps: bool, nick_size: int) -> int:
"""Write the part before the body:
- timestamp (short or long)
- ack/nack
@@ -149,7 +162,7 @@ def write_pre_message(msg: Message, win, with_timestamps: bool, nick_size: int)
@write_pre.register(XMLLog)
-def write_pre_xmllog(msg: XMLLog, win, with_timestamps: bool, nick_size: int) -> int:
+def write_pre_xmllog(msg: XMLLog, win: Win, with_timestamps: bool, nick_size: int) -> int:
"""Write the part before the stanza (timestamp + IN/OUT)"""
offset = 0
if with_timestamps:
@@ -170,7 +183,7 @@ def write_pre_xmllog(msg: XMLLog, win, with_timestamps: bool, nick_size: int) ->
class PreMessageHelpers:
@staticmethod
- def write_revisions(buffer, msg: Message) -> int:
+ def write_revisions(buffer: Win, msg: Message) -> int:
if msg.revisions:
color = get_theme().COLOR_REVISIONS_MESSAGE
with buffer.colored_text(color=color):
@@ -179,7 +192,7 @@ class PreMessageHelpers:
return 0
@staticmethod
- def write_ack(buffer) -> int:
+ def write_ack(buffer: Win) -> int:
theme = get_theme()
color = theme.COLOR_CHAR_ACK
with buffer.colored_text(color=color):
@@ -188,7 +201,7 @@ class PreMessageHelpers:
return poopt.wcswidth(theme.CHAR_ACK_RECEIVED) + 1
@staticmethod
- def write_nack(buffer) -> int:
+ def write_nack(buffer: Win) -> int:
theme = get_theme()
color = theme.COLOR_CHAR_NACK
with buffer.colored_text(color=color):
@@ -197,7 +210,7 @@ class PreMessageHelpers:
return poopt.wcswidth(theme.CHAR_NACK) + 1
@staticmethod
- def write_nickname(buffer, nickname: str, color, highlight=False) -> None:
+ def write_nickname(buffer: Win, nickname: str, color, highlight=False) -> None:
"""
Write the nickname, using the user's color
and return the number of written characters
@@ -215,7 +228,7 @@ class PreMessageHelpers:
buffer.addstr(nickname)
@staticmethod
- def write_time(buffer, history: bool, time: datetime) -> int:
+ def write_time(buffer: Win, history: bool, time: datetime) -> int:
"""
Write the date on the yth line of the window
"""
diff --git a/poezio/ui/types.py b/poezio/ui/types.py
index 6c744ac3..9ecdc185 100644
--- a/poezio/ui/types.py
+++ b/poezio/ui/types.py
@@ -27,6 +27,12 @@ class BaseMessage:
return SHORT_FORMAT_LENGTH + 1
+class InfoMessage(BaseMessage):
+ 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 XMLLog(BaseMessage):
"""XML Log message"""
__slots__ = ('txt', 'time', 'identifier', 'incoming')
@@ -59,6 +65,25 @@ class XMLLog(BaseMessage):
return offset
+class StatusMessage(BaseMessage):
+ __slots__ = ('txt', 'time', 'identifier', 'format_string', 'format_args')
+
+ 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):
__slots__ = ('txt', 'nick_color', 'time', 'nickname', 'user', 'history',
'identifier', 'top', 'highlight', 'me', 'old_message', 'revisions',