-
Notifications
You must be signed in to change notification settings - Fork 641
/
color.py
72 lines (57 loc) · 2.26 KB
/
color.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"""Console coloring and terminal support."""
from __future__ import annotations
from typing import Any
import rich
from rich.console import Console
from rich.default_styles import DEFAULT_STYLES
from rich.style import Style
from rich.syntax import Syntax
from rich.theme import Theme
DEFAULT_STYLES.update(
{
# "code": Style(color="bright_black", bgcolor="red"),
"markdown.code": Style(color="bright_black"),
"markdown.code_block": Style(dim=True, color="cyan"),
}
)
_theme = Theme(
{
"info": "cyan",
"warning": "dim yellow",
"danger": "bold red",
"title": "yellow",
"error_code": "bright_red",
"error_title": "red",
"filename": "blue",
}
)
console_options: dict[str, Any] = {"emoji": False, "theme": _theme, "soft_wrap": True}
console_options_stderr = console_options.copy()
console_options_stderr["stderr"] = True
console = rich.get_console()
console_stderr = Console(**console_options_stderr)
def reconfigure(new_options: dict[str, Any]) -> None:
"""Reconfigure console options."""
global console_options # pylint: disable=global-statement,invalid-name
global console_stderr # pylint: disable=global-statement,invalid-name,global-variable-not-assigned
console_options = new_options
rich.reconfigure(**new_options)
# see https://github.com/willmcgugan/rich/discussions/484#discussioncomment-200182
new_console_options_stderr = console_options.copy()
new_console_options_stderr["stderr"] = True
tmp_console = Console(**new_console_options_stderr)
console_stderr.__dict__ = tmp_console.__dict__
def render_yaml(text: str) -> Syntax:
"""Colorize YAMl for nice display."""
return Syntax(text, "yaml", theme="ansi_dark")
# pylint: disable=redefined-outer-name,unused-argument
def _rich_heading_custom_rich_console(
self: rich.markdown.Heading,
console: rich.console.Console,
options: rich.console.ConsoleOptions,
) -> rich.console.RenderResult:
"""Override for rich console heading."""
yield f"[bright_white]{self.level * '#'} {self.text}[/]"
# Monkey-patch rich to alter its rendering of headings
# https://github.com/python/mypy/issues/2427
rich.markdown.Heading.__rich_console__ = _rich_heading_custom_rich_console # type: ignore