Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for ANSI colored/styled text #84

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/rich_cli/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
INSPECT = 7
CSV = 8
IPYNB = 9
ANSI = 10


def on_error(message: str, error: Optional[Exception] = None, code=-1) -> NoReturn:
Expand Down Expand Up @@ -259,6 +260,7 @@ class OptionHighlighter(RegexHighlighter):
@click.option("--rst", is_flag=True, help="Display [u]restructured text[/u].")
@click.option("--csv", is_flag=True, help="Display [u]CSV[/u] as a table.")
@click.option("--ipynb", is_flag=True, help="Display [u]Jupyter notebook[/u].")
@click.option("--ansi", is_flag=True, help="Display [u]ANSI colored/styled text[/u].")
@click.option("--syntax", is_flag=True, help="[u]Syntax[/u] highlighting.")
@click.option("--inspect", is_flag=True, help="[u]Inspect[/u] a python object.")
@click.option(
Expand Down Expand Up @@ -409,6 +411,7 @@ def main(
rst: bool = False,
csv: bool = False,
ipynb: bool = False,
ansi: bool = False,
inspect: bool = True,
emoji: bool = False,
left: bool = False,
Expand Down Expand Up @@ -494,6 +497,8 @@ def print_usage() -> None:
resource_format = RST
elif ipynb:
resource_format = IPYNB
elif ansi:
resource_format = ANSI

if resource_format == AUTO and "." in resource:
import os.path
Expand Down Expand Up @@ -622,6 +627,14 @@ def print_usage() -> None:
guides,
no_wrap,
)
elif resource_format == ANSI:
from rich.text import Text

ansi_data, _lexer = read_resource(resource, lexer=None)
try:
renderable = Text.from_ansi(ansi_data)
except Exception as error:
on_error("unable to read ansi", error)

else:
if not resource:
Expand Down