home · contact · privacy
Use whole prefix for log color formatting, add ClientTui-specific colorings.
authorChristian Heller <c.heller@plomlompom.de>
Sat, 4 Oct 2025 19:50:23 +0000 (21:50 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Sat, 4 Oct 2025 19:50:23 +0000 (21:50 +0200)
src/ircplom/client_tui.py
src/ircplom/testing.py
src/ircplom/tui_base.py

index 804a837727e1de0d726b6b8ea27ef00def2f951b..859a06affde9db7918f7f13dd631ad0cc01df40f 100644 (file)
@@ -6,7 +6,7 @@ from tomllib import load as toml_load
 from typing import Any, Callable, Optional, Sequence
 # ourselves
 from ircplom.tui_base import (BaseTui, PromptWidget, TuiEvent, Window,
-                              CMD_SHORTCUTS)
+                              CMD_SHORTCUTS, LOG_FMT_ATTRS)
 from ircplom.client import (
     AutoAttrMixin, Channel, ChatMessage, Client, ClientQueueMixin, Dict,
     DictItem, ImplementationFail, IrcConnSetup, NewClientEvent, NickUserHost,
@@ -25,6 +25,10 @@ _LOG_PREFIX_SERVER = '$'
 _LOG_PREFIX_OUT = '>'
 LOG_PREFIX_IN = '<'
 
+LOG_FMT_ATTRS[LOG_PREFIX_IN] = ('bright_white',)
+LOG_FMT_ATTRS[_LOG_PREFIX_OUT] = ('bright_green',)
+LOG_FMT_ATTRS[_LOG_PREFIX_SERVER] = ('bright_yellow',)
+
 _PATH_LOGS = Path.home().joinpath('.local', 'share', 'ircplom', 'logs')
 _PATH_CONFIG = Path.home().joinpath('.config', 'ircplom', 'ircplom.toml')
 
index d4f0a8eef898dcc449d6b41aa98cbd61e0559f4c..b3ecd3ed4a8359c7f99a0238b09fb00ab8bc23b9 100644 (file)
@@ -10,7 +10,7 @@ from ircplom.client_tui import ClientKnowingTui, ClientTui, LOG_PREFIX_IN
 from ircplom.irc_conn import (IrcConnAbortException, IrcConnTimeoutException,
                               IrcMessage)
 from ircplom.tui_base import (TerminalInterface, TuiEvent,
-                              LOG_FMT_ALERT, LOG_FMT_HIGHLIGHT, LOG_FMT_NONE)
+                              LOG_FMT_SEP, LOG_FMT_ATTRS)
 
 
 PATH_TESTS = Path('tests')
@@ -139,11 +139,12 @@ class _Playbook:
 
         def split_server_put_and_log(line: str, **_) -> list[str]:
             context, msg = self._split_by_context_separator(line)
-            assert msg[0] in {LOG_FMT_NONE, LOG_FMT_HIGHLIGHT}
-            assert msg[1] in {LOG_FMT_NONE, LOG_FMT_ALERT}
-            assert msg[2:4] == f'{LOG_PREFIX_IN} '
+            fmt, msg_no_fmt = msg.split(LOG_FMT_SEP, maxsplit=1)
+            for c in fmt[:-1]:
+                assert c in LOG_FMT_ATTRS
+            assert fmt[-1] == LOG_PREFIX_IN
             c_id, win_ids = context[1:].split(_CHAR_ID_TYPE_SEP, maxsplit=1)
-            return [f'{_CHAR_SERVER_MSG}{c_id}{_CHAR_CONTEXT_SEP}{msg[4:]}',
+            return [f'{_CHAR_SERVER_MSG}{c_id}{_CHAR_CONTEXT_SEP}{msg_no_fmt}',
                     win_ids + _CHAR_CONTEXT_SEP + msg]
 
         def repeat(line: str, anchors: dict[str, int], **__) -> list[str]:
@@ -255,8 +256,8 @@ class TestingClientTui(ClientTui):
 
     def log(self, msg: str, **kwargs) -> tuple[tuple[int, ...], str]:
         win_ids, logged_msg = super().log(msg, **kwargs)
-        time_str, msg_sans_time = logged_msg[2:].split(' ', maxsplit=1)
-        msg_sans_time = logged_msg[:2] + msg_sans_time
+        fmt, time_str, msg_sans_time = logged_msg.split(' ', maxsplit=2)
+        msg_sans_time = fmt + ' ' + msg_sans_time
         assert len(time_str) == 8
         for c in time_str[:2] + time_str[3:5] + time_str[6:]:
             assert c.isdigit()
index 6c4cc506bff63903db59d4b8c2dffe7dd8be7e55..4ab6dd14970a9d6dd726c413a8329cf1f23a7e49 100644 (file)
@@ -14,9 +14,16 @@ from blessed import Terminal as BlessedTerminal
 from ircplom.events import AffectiveEvent, Loop, QueueMixin, QuitEvent
 
 _LOG_PREFIX_DEFAULT = '#'
+LOG_FMT_SEP = ' '
 LOG_FMT_NONE = '.'
 LOG_FMT_ALERT = '!'
 LOG_FMT_HIGHLIGHT = '*'
+LOG_FMT_ATTRS: dict[str, tuple[str, ...]] = {
+    LOG_FMT_NONE: tuple(),
+    LOG_FMT_HIGHLIGHT: ('reverse',),
+    LOG_FMT_ALERT: ('bold', 'bright_red'),
+    _LOG_PREFIX_DEFAULT: ('bright_cyan',),
+}
 
 _MIN_HEIGHT = 4
 _MIN_WIDTH = 32
@@ -162,15 +169,14 @@ class _HistoryWidget(_ScrollableWidget):
                    + self._wrapped[bookmark_idx_pos:])
 
         to_write_w_attrs: list[tuple[Optional[str], str]] = []
-        attrs: str
         prev_idx_unwrapped: Optional[int] = -1
         for idx_unwrapped, line in wrapped[start_idx_neg:end_idx_neg]:
+            attrs = ['on_black']
             if idx_unwrapped != prev_idx_unwrapped:
-                attrs = ','.join(
-                        (['bold'] if line[:1] == LOG_FMT_HIGHLIGHT else []) +
-                        (['red'] if line[1:2] == LOG_FMT_ALERT else []))
+                for c in line.split(LOG_FMT_SEP, maxsplit=1)[0]:
+                    attrs += list(LOG_FMT_ATTRS.get(c, tuple()))
                 prev_idx_unwrapped = idx_unwrapped
-            to_write_w_attrs += [(attrs, line)]
+            to_write_w_attrs += [(','.join(attrs), line)]
 
         if add_scroll_info:
             scroll_info = f'vvv [{(-1) * self._wrapped_idx_neg}] '
@@ -493,10 +499,10 @@ class BaseTui(QueueMixin):
         prefix = kwargs.get('prefix', _LOG_PREFIX_DEFAULT)
         now = str(datetime.now())
         today, time = now[:10], now[11:19]
-        fmt = ''
-        for t in (('highlight', LOG_FMT_HIGHLIGHT), ('alert', LOG_FMT_ALERT)):
-            fmt += t[1] if kwargs.get(t[0], False) else LOG_FMT_NONE
-        msg = f'{fmt}{time} {prefix} {msg}'
+        for t in (('alert', LOG_FMT_ALERT), ('highlight', LOG_FMT_HIGHLIGHT)):
+            prefix = (t[1] if kwargs.get(t[0], False) else LOG_FMT_NONE
+                      ) + prefix
+        msg = f'{prefix}{LOG_FMT_SEP}{time} {msg}'
         affected_win_indices = []
         for win in self._log_target_wins(**kwargs):
             affected_win_indices += [win.idx]