From a17e5a456b9bf1a949d9ed1d0d1df90013c69e6d Mon Sep 17 00:00:00 2001 From: mathieui Date: Thu, 15 Apr 2021 18:44:22 +0200 Subject: [PATCH] internal: add a different class for UI messages --- poezio/core/core.py | 14 +++++--------- poezio/theming.py | 2 +- poezio/ui/render.py | 24 ++++++++++++++++++++++++ poezio/ui/types.py | 27 +++++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 10 deletions(-) diff --git a/poezio/core/core.py b/poezio/core/core.py index a940e142..9fa003b6 100644 --- a/poezio/core/core.py +++ b/poezio/core/core.py @@ -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'] diff --git a/poezio/theming.py b/poezio/theming.py index c5df114e..32763c66 100755 --- a/poezio/theming.py +++ b/poezio/theming.py @@ -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), diff --git a/poezio/ui/render.py b/poezio/ui/render.py index f377df7f..0d1e8d2b 100644 --- a/poezio/ui/render.py +++ b/poezio/ui/render.py @@ -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: diff --git a/poezio/ui/types.py b/poezio/ui/types.py index e8f7f7eb..242fee88 100644 --- a/poezio/ui/types.py +++ b/poezio/ui/types.py @@ -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