diff --git a/CHANGELOG.md b/CHANGELOG.md index 5f3012c..01a43bb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed + +- Termynal mode now keeps its colors when `NO_COLOR` is set in the build environment (e.g. ReadTheDocs). The capture `Console` passes `no_color=False`, so the help is no longer silently rendered monochrome — the output is a build artifact converted to HTML, not interactive terminal output ([#38](https://github.com/syn54x/mkdocs-typer2/issues/38)). + ## [0.4.0] - 2026-06-16 ### Added diff --git a/src/mkdocs_typer2/termynal_render.py b/src/mkdocs_typer2/termynal_render.py index b30d1c9..75bb102 100644 --- a/src/mkdocs_typer2/termynal_render.py +++ b/src/mkdocs_typer2/termynal_render.py @@ -160,7 +160,10 @@ def _colored_help(command: click.core.Command, info_name: str, width: int = 80) ``rich_format_help()`` hardcodes ``console = _get_rich_console()`` with no console/file parameter to inject (``CliRunner`` drops rich color, and the env-var knobs are import-time + process-global). So we swap that private - factory for a buffer-backed ``Console``. + factory for a buffer-backed ``Console``. That console sets + ``no_color=False`` so the captured help keeps its color even when + ``NO_COLOR`` is set in the environment (e.g. ReadTheDocs): this is a build + artifact converted to HTML, not interactive terminal output. If that private hook ever disappears (a future typer rename), we degrade safely: ``format_help`` runs with stdout redirected into the same buffer, so @@ -184,6 +187,7 @@ def _colored_help(command: click.core.Command, info_name: str, width: int = 80) lambda stderr=False: Console( # noqa: ARG005 force_terminal=True, color_system="standard", + no_color=False, width=width, file=buf, highlight=False, diff --git a/tests/test_termynal_render.py b/tests/test_termynal_render.py index bc5820e..b921f7b 100644 --- a/tests/test_termynal_render.py +++ b/tests/test_termynal_render.py @@ -36,6 +36,19 @@ def test_render_termynal_html_typer_colored_and_balanced(): assert html.count("") +def test_render_termynal_html_keeps_color_when_no_color_set(monkeypatch): + # NO_COLOR in the build env (e.g. ReadTheDocs) must not strip color from the + # captured help: it is a build artifact converted to HTML, not interactive + # terminal output. rich's no_color defaults to ("NO_COLOR" in os.environ). + monkeypatch.setenv("NO_COLOR", "1") + + html = render_termynal_html( + "mkdocs_typer2.cli.cli", "mkdocs-typer2", TermynalOptions(subcommands=1) + ) + + assert 'style="color:' in html + + def test_render_termynal_html_root_only_by_default(): html = render_termynal_html("mkdocs_typer2.cli.cli", "mkdocs-typer2")