mirror of
https://github.com/jrnl-org/jrnl.git
synced 2025-05-14 02:08:31 +02:00
Fix indent character colorize issue
This commit is contained in:
parent
5c42520ddf
commit
8f1da7249d
2 changed files with 12 additions and 28 deletions
|
@ -144,28 +144,17 @@ class Entry:
|
||||||
body = highlight_tags_with_background_color(
|
body = highlight_tags_with_background_color(
|
||||||
self, self.body.rstrip(" \n"), self.journal.config["colors"]["body"]
|
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
|
body = wrap_with_ansi_colors(body, columns - len(indent))
|
||||||
# ANSI escapes properly, so we have this hack here to make sure the
|
if indent:
|
||||||
# beginning of each line has the indent character and it's colored
|
# Without explicitly colorizing the indent character, it will lose its
|
||||||
# properly. textwrap doesn't have this issue, however, it doesn't wrap
|
# color after a tag appears.
|
||||||
# the strings properly as it counts ANSI escapes as literal characters.
|
|
||||||
# TL;DR: I'm sorry.
|
|
||||||
body = "\n".join(
|
body = "\n".join(
|
||||||
[
|
|
||||||
colorize(indent, self.journal.config["colors"]["body"]) + line
|
colorize(indent, self.journal.config["colors"]["body"]) + line
|
||||||
if not ansiwrap.strip_color(line).startswith(indent)
|
for line in body.splitlines()
|
||||||
else line
|
|
||||||
for line in body_text
|
|
||||||
]
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
body = colorize(body, self.journal.config["colors"]["body"])
|
||||||
else:
|
else:
|
||||||
title = (
|
title = (
|
||||||
date_str
|
date_str
|
||||||
|
|
|
@ -130,15 +130,10 @@ def format_msg_text(msg: Message) -> Text:
|
||||||
return Text(text)
|
return Text(text)
|
||||||
|
|
||||||
|
|
||||||
def wrap_with_ansi_colors(
|
def wrap_with_ansi_colors(text: str, width: int) -> str:
|
||||||
text: str, width: int, indent_text: str = "", drop_whitespace: bool = True
|
|
||||||
) -> str:
|
|
||||||
richtext = Text.from_ansi(text, no_wrap=False, tab_size=None)
|
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:
|
with console.capture() as capture:
|
||||||
console.print(richtext)
|
console.print(richtext)
|
||||||
wrapped_text = capture.get()
|
return capture.get()
|
||||||
|
|
||||||
return "\n".join([indent_text + line for line in wrapped_text.splitlines()])
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue