From 5c42520ddf7bb7287bc7d9e02f77960def5ce407 Mon Sep 17 00:00:00 2001 From: Micah Jerome Ellison Date: Mon, 27 Feb 2023 12:27:01 -0800 Subject: [PATCH] First attempt at replacing ansiwrap.fill with rich. It is working except that it is un-colorizing the indent character --- jrnl/journals/Entry.py | 12 +++--------- jrnl/output.py | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/jrnl/journals/Entry.py b/jrnl/journals/Entry.py index 93b825f8..907adb90 100644 --- a/jrnl/journals/Entry.py +++ b/jrnl/journals/Entry.py @@ -11,6 +11,7 @@ import ansiwrap from jrnl.color import colorize from jrnl.color import highlight_tags_with_background_color +from jrnl.output import wrap_with_ansi_colors if TYPE_CHECKING: from .Journal import Journal @@ -129,7 +130,7 @@ class Entry: columns = 79 # Color date / title and bold title - title = ansiwrap.fill( + title = wrap_with_ansi_colors( date_str + " " + highlight_tags_with_background_color( @@ -145,16 +146,9 @@ class Entry: ) body_text = [ colorize( - ansiwrap.fill( - line, - columns, - initial_indent=indent, - subsequent_indent=indent, - drop_whitespace=True, - ), + wrap_with_ansi_colors(line, columns, indent_text=indent), self.journal.config["colors"]["body"], ) - or indent for line in body.rstrip(" \n").splitlines() ] diff --git a/jrnl/output.py b/jrnl/output.py index f11f2382..800b3e42 100644 --- a/jrnl/output.py +++ b/jrnl/output.py @@ -128,3 +128,17 @@ def format_msg_text(msg: Message) -> Text: text = textwrap.dedent(text) text = text.strip() return Text(text) + + +def wrap_with_ansi_colors( + text: str, width: int, indent_text: str = "", drop_whitespace: bool = True +) -> str: + richtext = Text.from_ansi(text, no_wrap=False, tab_size=None) + pre_indent_width = width - len(indent_text) + + console = Console(width=pre_indent_width) + with console.capture() as capture: + console.print(richtext) + wrapped_text = capture.get() + + return "\n".join([indent_text + line for line in wrapped_text.splitlines()])