httpie-cli/httpie/output/ui/rich_utils.py
Batuhan Taskaya f9b5c2f696
Man page fixes (#1364)
- Highlighting for options (-x, --x) now doesn't strip the prefix (may be whitespace).
- Escape sequences are now cross-platform compatible (directly taken by groff/troff [man's renderer])
- Now we check for the section before displaying the man pages.
- On MacOS, there is HTTP(n) which is different from our HTTP(1). This used to conflict with it, and we showed the wrong page. Now we specifically ask foir HTTP(1).
- Errors that might happen (e.g non executable man command) is now suppressed. So in the worst case (if anything regarding man execution goes wrong), we'll always display the manual.
- Docs for man pages.
- HTTPie man pages.
- Epilog for the man pages (see also)
- Auto-generated comments.
2022-05-05 11:17:37 -07:00

35 lines
972 B
Python

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