Skip to content

Commit 3cb492b

Browse files
committed
docs: color output for docs
Signed-off-by: Henry Schreiner <[email protected]>
1 parent 733573f commit 3cb492b

File tree

4 files changed

+16
-7
lines changed

4 files changed

+16
-7
lines changed

.readthedocs.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@
33
version: 2
44

55
build:
6-
os: ubuntu-22.04
7-
tools:
8-
python: "3.12"
6+
os: ubuntu-24.04
97
commands:
108
- asdf plugin add uv
119
- asdf install uv latest
1210
- asdf global uv latest
13-
- NO_COLOR=1 uv run --no-dev --group docs mkdocs build --strict --site-dir $READTHEDOCS_OUTPUT/html
11+
- NO_COLOR=1 uv run -p 3.14 --no-dev --group docs mkdocs build --strict --site-dir $READTHEDOCS_OUTPUT/html

docs/main.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import os
2+
import re
23
import subprocess
34
import sysconfig
45
from typing import Any
56

7+
import rich.console
8+
import rich.text
9+
610

711
def define_env(env: Any) -> None:
812
"Hook function for mkdocs-macros"
@@ -12,5 +16,13 @@ def subprocess_run(*args: str) -> str:
1216
"Run a subprocess and return the stdout"
1317
env = os.environ.copy()
1418
scripts = sysconfig.get_path("scripts")
19+
env.pop("NO_COLOR", None)
1520
env["PATH"] = f"{scripts}{os.pathsep}{env.get('PATH', '')}"
16-
return subprocess.run(args, check=True, capture_output=True, text=True, env=env).stdout
21+
env["PYTHON_COLORS"] = "1"
22+
output = subprocess.run(args, check=True, capture_output=True, text=True, env=env).stdout
23+
rich_text = rich.text.Text.from_ansi(output)
24+
console = rich.console.Console(record=True)
25+
console.print(rich_text)
26+
page = console.export_html(inline_styles=True)
27+
text = re.search(r"<body.*?>(.*?)</body>", page, re.DOTALL | re.IGNORECASE).group(1)
28+
return text.strip()

docs/options.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1714,9 +1714,7 @@ Platform-specific environment variables are also available:<br/>
17141714

17151715
### Options
17161716

1717-
```text
17181717
« subprocess_run("cibuildwheel", "--help") »
1719-
```
17201718

17211719
### Return codes
17221720

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ docs = [
7676
"mkdocs-macros-plugin",
7777
"mkdocs==1.6.1",
7878
"pymdown-extensions",
79+
"rich",
7980
]
8081
test = [
8182
"build",

0 commit comments

Comments
 (0)