Fix indent character colorize issue

This commit is contained in:
Micah Jerome Ellison 2023-02-27 12:33:48 -08:00
parent 5c42520ddf
commit 8f1da7249d
2 changed files with 12 additions and 28 deletions

View file

@ -144,28 +144,17 @@ class Entry:
body = highlight_tags_with_background_color(
self, self.body.rstrip(" \n"), self.journal.config["colors"]["body"]
)
body_text = [
colorize(
wrap_with_ansi_colors(line, columns, indent_text=indent),
self.journal.config["colors"]["body"],
)
for line in body.rstrip(" \n").splitlines()
]
# ansiwrap doesn't handle lines with only the "\n" character and some
# ANSI escapes properly, so we have this hack here to make sure the
# beginning of each line has the indent character and it's colored
# properly. textwrap doesn't have this issue, however, it doesn't wrap
# the strings properly as it counts ANSI escapes as literal characters.
# TL;DR: I'm sorry.
body = "\n".join(
[
body = wrap_with_ansi_colors(body, columns - len(indent))
if indent:
# Without explicitly colorizing the indent character, it will lose its
# color after a tag appears.
body = "\n".join(
colorize(indent, self.journal.config["colors"]["body"]) + line
if not ansiwrap.strip_color(line).startswith(indent)
else line
for line in body_text
]
)
for line in body.splitlines()
)
body = colorize(body, self.journal.config["colors"]["body"])
else:
title = (
date_str

View file

@ -130,15 +130,10 @@ def format_msg_text(msg: Message) -> Text:
return Text(text)
def wrap_with_ansi_colors(
text: str, width: int, indent_text: str = "", drop_whitespace: bool = True
) -> str:
def wrap_with_ansi_colors(text: str, width: int) -> 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)
console = Console(width=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()])
return capture.get()