internal: add a different class for UI messages

This commit is contained in:
mathieui 2021-04-15 18:44:22 +02:00
parent 56bab71192
commit a17e5a456b
4 changed files with 57 additions and 10 deletions

View file

@ -75,6 +75,7 @@ from poezio.core.structs import (
from poezio.ui.types import (
Message,
PersistentInfoMessage,
UIMessage,
)
log = logging.getLogger(__name__)
@ -1381,13 +1382,10 @@ class Core:
'Did not show the message:\n\t%s> %s \n\tdue to filter_info_messages configuration',
typ, msg)
return False
colors = get_theme().INFO_COLORS
color = colors.get(typ.lower(), colors.get('default', None))
nb_lines = self.information_buffer.add_message(
Message(
UIMessage(
txt=msg,
nickname=typ,
nick_color=color
level=typ,
)
)
popup_on = config.getlist('information_buffer_popup_on')
@ -1763,11 +1761,9 @@ class Core:
return
error_message = get_error_message(error)
tab.add_message(
Message(
UIMessage(
error_message,
highlight=True,
nickname='Error',
nick_color=get_theme().COLOR_ERROR_MSG,
level='Error',
),
)
code = error['error']['code']

View file

@ -368,7 +368,7 @@ class Theme:
# Info messages color (the part before the ">")
INFO_COLORS = {
'info': (5, -1),
'error': (16, 1),
'error': (9, 7, 'b'),
'warning': (1, -1),
'roster': (2, -1),
'help': (10, -1),

View file

@ -30,6 +30,7 @@ from poezio.ui.types import (
BaseMessage,
Message,
StatusMessage,
UIMessage,
XMLLog,
)
@ -125,6 +126,29 @@ def write_pre(msg: BaseMessage, win: Win, with_timestamps: bool, nick_size: int)
return 0
@write_pre.register(UIMessage)
def write_pre_uimessage(msg: UIMessage, win: Win, with_timestamps: bool, nick_size: int) -> int:
""" Write the prefix of a ui message log
- timestamp (short or long)
- level
"""
color: Optional[Tuple]
offset = 0
if with_timestamps:
offset += PreMessageHelpers.write_time(win, False, msg.time)
if not msg.level: # not a message, nothing to do afterwards
return offset
level = truncate_nick(msg.level, nick_size)
offset += poopt.wcswidth(level)
color = msg.color
PreMessageHelpers.write_nickname(win, level, color, False)
win.addstr('> ')
offset += 2
return offset
@write_pre.register(Message)
def write_pre_message(msg: Message, win: Win, with_timestamps: bool, nick_size: int) -> int:
"""Write the part before the body:

View file

@ -43,6 +43,33 @@ class InfoMessage(BaseMessage):
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