From 0d53952ff7421845d29998e1e1a02e4f7be7e0f7 Mon Sep 17 00:00:00 2001 From: GitHub Copilot Date: Sat, 24 Jan 2026 11:21:09 +0000 Subject: [PATCH] fix: handle all pyte color formats for Rich compatibility - Add 'brown' -> 'yellow' mapping (pyte uses 'brown' for ANSI yellow) - Add helper function to convert hex colors (pyte outputs 'ff8700', Rich needs '#ff8700') - Handles 256-color and truecolor (24-bit) ANSI codes properly Bumps version to 0.1.14. --- pyproject.toml | 2 +- src/textual_webterm/local_server.py | 21 ++++++++++++++++++--- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index ca754a7..6d08267 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "textual-webterm" -version = "0.1.13" +version = "0.1.14" description = "Serve terminal sessions over the web" authors = ["Will McGugan "] license = "MIT" diff --git a/src/textual_webterm/local_server.py b/src/textual_webterm/local_server.py index bd7ab9c..4dafd22 100644 --- a/src/textual_webterm/local_server.py +++ b/src/textual_webterm/local_server.py @@ -53,8 +53,23 @@ PYTE_TO_RICH_COLOR = { "brightmagenta": "bright_magenta", "brightcyan": "bright_cyan", "brightwhite": "bright_white", + "brown": "yellow", # pyte uses 'brown' for ANSI yellow } + +def _pyte_color_to_rich(color: str) -> str: + """Convert pyte color to Rich-compatible color string.""" + if color == "default": + return color + # Check mapping first + if color in PYTE_TO_RICH_COLOR: + return PYTE_TO_RICH_COLOR[color] + # If it looks like a hex color without #, add it + if len(color) == 6 and all(c in "0123456789abcdefABCDEF" for c in color): + return f"#{color}" + return color + + WEBTERM_STATIC_PATH = Path(__file__).parent / "static" @@ -546,12 +561,12 @@ class LocalServer: char_data = char["data"] # Build Rich style from pyte character attributes - # Map pyte color names to Rich-compatible names + # Convert pyte color names to Rich-compatible format style_kwargs = {} if char["fg"] != "default": - style_kwargs["color"] = PYTE_TO_RICH_COLOR.get(char["fg"], char["fg"]) + style_kwargs["color"] = _pyte_color_to_rich(char["fg"]) if char["bg"] != "default": - style_kwargs["bgcolor"] = PYTE_TO_RICH_COLOR.get(char["bg"], char["bg"]) + style_kwargs["bgcolor"] = _pyte_color_to_rich(char["bg"]) if char["bold"]: style_kwargs["bold"] = True if char["italics"]: