mirror of
https://github.com/jrnl-org/jrnl.git
synced 2025-05-12 09:28:31 +02:00
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:
parent
611f3af772
commit
5c42520ddf
2 changed files with 17 additions and 9 deletions
|
@ -11,6 +11,7 @@ import ansiwrap
|
||||||
|
|
||||||
from jrnl.color import colorize
|
from jrnl.color import colorize
|
||||||
from jrnl.color import highlight_tags_with_background_color
|
from jrnl.color import highlight_tags_with_background_color
|
||||||
|
from jrnl.output import wrap_with_ansi_colors
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from .Journal import Journal
|
from .Journal import Journal
|
||||||
|
@ -129,7 +130,7 @@ class Entry:
|
||||||
columns = 79
|
columns = 79
|
||||||
|
|
||||||
# Color date / title and bold title
|
# Color date / title and bold title
|
||||||
title = ansiwrap.fill(
|
title = wrap_with_ansi_colors(
|
||||||
date_str
|
date_str
|
||||||
+ " "
|
+ " "
|
||||||
+ highlight_tags_with_background_color(
|
+ highlight_tags_with_background_color(
|
||||||
|
@ -145,16 +146,9 @@ class Entry:
|
||||||
)
|
)
|
||||||
body_text = [
|
body_text = [
|
||||||
colorize(
|
colorize(
|
||||||
ansiwrap.fill(
|
wrap_with_ansi_colors(line, columns, indent_text=indent),
|
||||||
line,
|
|
||||||
columns,
|
|
||||||
initial_indent=indent,
|
|
||||||
subsequent_indent=indent,
|
|
||||||
drop_whitespace=True,
|
|
||||||
),
|
|
||||||
self.journal.config["colors"]["body"],
|
self.journal.config["colors"]["body"],
|
||||||
)
|
)
|
||||||
or indent
|
|
||||||
for line in body.rstrip(" \n").splitlines()
|
for line in body.rstrip(" \n").splitlines()
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
|
@ -128,3 +128,17 @@ def format_msg_text(msg: Message) -> Text:
|
||||||
text = textwrap.dedent(text)
|
text = textwrap.dedent(text)
|
||||||
text = text.strip()
|
text = text.strip()
|
||||||
return Text(text)
|
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()])
|
||||||
|
|
Loading…
Add table
Reference in a new issue