-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathmacros.py
31 lines (27 loc) · 823 Bytes
/
macros.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import re
from subprocess import run
from mkdocs_macros.plugin import MacrosPlugin
# fmt: off
# https://stackoverflow.com/questions/14693701
ANSI_ESCAPE_8BIT = re.compile(
r"""
\x1B # ESC
(?: # 7-bit C1 Fe (except CSI)
[@-Z\\-_]
| # or [ for CSI, followed by a control sequence
\[
[0-?]* # Parameter bytes
[ -/]* # Intermediate bytes
[@-~] # Final byte
)
""",
re.VERBOSE,
)
# fmt: on
def define_env(env: MacrosPlugin) -> None:
@env.macro
def get_help_message(*args: str):
command = ["uv", "run", "python", "-m", *args, "--help"]
message = run(command, capture_output=True).stdout.decode("utf-8")
message = ANSI_ESCAPE_8BIT.sub("", message)
return f"```bash\n➜ {' '.join(command)}\n{message}\n```"