Skip to content

Color command output in docs? #2399

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
henryiii opened this issue May 19, 2025 · 3 comments · May be fixed by #2407
Open

Color command output in docs? #2399

henryiii opened this issue May 19, 2025 · 3 comments · May be fixed by #2407

Comments

@henryiii
Copy link
Contributor

Should we color the command output in docs? As a proof of concept, I ran this in 3.14 with the following hack:

build.py

import os
import re
import subprocess
import sysconfig
from typing import Any

import rich.console
import rich.text


def define_env(env: Any) -> None:
    "Hook function for mkdocs-macros"

    @env.macro  # type: ignore[misc]
    def subprocess_run(*args: str) -> str:
        "Run a subprocess and return the stdout"
        env = os.environ.copy()
        scripts = sysconfig.get_path("scripts")
        env["PATH"] = f"{scripts}{os.pathsep}{env.get('PATH', '')}"
        env["PYTHON_COLORS"] = "1"
        output = subprocess.run(args, check=True, capture_output=True, text=True, env=env).stdout
        rich_text = rich.text.Text.from_ansi(output)
        console = rich.console.Console(record=True)
        console.print(rich_text)
        page = console.export_html(inline_styles=True)
        text = re.search(r"<body.*?>(.*?)</body>", page, re.DOTALL | re.IGNORECASE).group(1)
        return text.strip()

Result:

Image

@joerick
Copy link
Contributor

joerick commented May 19, 2025

I like it!

@henryiii henryiii linked a pull request May 22, 2025 that will close this issue
@danielhollas
Copy link

Out of curiosity, why is '-h' green in the output? Looks like a bug?

@henryiii
Copy link
Contributor Author

Maybe short options are green? That's in the CLI output too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants