Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use rich instead of ansiwrap to wrap text #1693

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 12 additions & 31 deletions jrnl/journals/Entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
import re
from typing import TYPE_CHECKING

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
Expand Down Expand Up @@ -129,7 +128,7 @@ def pprint(self, short: bool = False) -> str:
columns = 79

# Color date / title and bold title
title = ansiwrap.fill(
title = wrap_with_ansi_colors(
date_str
+ " "
+ highlight_tags_with_background_color(
Expand All @@ -143,35 +142,17 @@ def pprint(self, short: bool = False) -> str:
body = highlight_tags_with_background_color(
self, self.body.rstrip(" \n"), self.journal.config["colors"]["body"]
)
body_text = [
colorize(
ansiwrap.fill(
line,
columns,
initial_indent=indent,
subsequent_indent=indent,
drop_whitespace=True,
),
self.journal.config["colors"]["body"],
)
or indent
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
Expand Down
9 changes: 9 additions & 0 deletions jrnl/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,12 @@ 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) -> str:
richtext = Text.from_ansi(text, no_wrap=False, tab_size=None)

console = Console(width=width)
with console.capture() as capture:
console.print(richtext, sep="", end="")
return capture.get()
27 changes: 1 addition & 26 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ classifiers = [
[tool.poetry.dependencies]
python = ">=3.10.0, <3.13"

ansiwrap = "^0.8.4"
colorama = ">=0.4" # https://github.com/tartley/colorama/blob/master/CHANGELOG.rst
cryptography = ">=3.0" # https://cryptography.io/en/latest/api-stability.html
keyring = ">=21.0" # https://github.com/jaraco/keyring#integration
Expand Down
2 changes: 1 addition & 1 deletion tests/bdd/features/format.feature
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ Feature: Custom formats
And the output should be
2020-08-29 11:11 Entry the first.
| Lorem @ipsum dolor sit amet, consectetur adipiscing elit. Praesent malesuada
| quis est ac dignissim. Aliquam dignissim rutrum pretium. Phasellus
| quis est ac dignissim. Aliquam dignissim rutrum pretium. Phasellus
| pellentesque
| augue et venenatis facilisis. Suspendisse potenti. Sed dignissim sed nisl eu
| consequat. Aenean ante ex, elementum ut interdum et, mattis eget lacus. In
Expand Down