summaryrefslogtreecommitdiffstats
path: root/httpie/output/ui/rich_utils.py
blob: 4843d1498cec1a6222bc94ab8eb379d53ccbb58d (plain)
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
import os

from typing import Iterator
from contextlib import contextmanager

from rich.console import Console, RenderableType
from rich.highlighter import Highlighter

from httpie.output.ui.rich_palette import _make_rich_color_theme


def render_as_string(renderable: RenderableType) -> str:
    """Render any `rich` object in a fake console and
    return a *style-less* version of it as a string."""

    with open(os.devnull, 'w') as null_stream:
        fake_console = Console(file=null_stream, record=True, theme=_make_rich_color_theme())
        fake_console.print(renderable)
        return fake_console.export_text()


@contextmanager
def enable_highlighter(
    console: Console,
    highlighter: Highlighter,
) -> Iterator[Console]:
    """Enable a higlighter temporarily."""

    original_highlighter = console.highlighter
    try:
        console.highlighter = highlighter
        yield console
    finally:
        console.highlighter = original_highlighter