First attempt at replacing ansiwrap.fill with rich. It is working except that it is un-colorizing the indent character

This commit is contained in:
Micah Jerome Ellison 2023-02-27 12:27:01 -08:00
parent 611f3af772
commit 5c42520ddf
2 changed files with 17 additions and 9 deletions

View file

@ -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()
]

View file

@ -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()])