From cb69bb474c55aa013e7c816f01fe25c1224c94fe Mon Sep 17 00:00:00 2001 From: Micah Jerome Ellison Date: Sat, 29 Jul 2023 14:36:43 -0700 Subject: [PATCH] Use rich instead of ansiwrap to wrap text (#1693) --- jrnl/journals/Entry.py | 43 +++++++++---------------------- jrnl/output.py | 9 +++++++ poetry.lock | 27 +------------------ pyproject.toml | 1 - tests/bdd/features/format.feature | 2 +- 5 files changed, 23 insertions(+), 59 deletions(-) diff --git a/jrnl/journals/Entry.py b/jrnl/journals/Entry.py index 5b35d7346..6c45303eb 100644 --- a/jrnl/journals/Entry.py +++ b/jrnl/journals/Entry.py @@ -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 @@ -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( @@ -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 diff --git a/jrnl/output.py b/jrnl/output.py index 2d7064cb3..0230244bc 100644 --- a/jrnl/output.py +++ b/jrnl/output.py @@ -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() diff --git a/poetry.lock b/poetry.lock index 40fceb11b..fe5a66700 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1,19 +1,5 @@ # This file is automatically @generated by Poetry 1.5.0 and should not be changed by hand. -[[package]] -name = "ansiwrap" -version = "0.8.4" -description = "textwrap, but savvy to ANSI colors and styles" -optional = false -python-versions = "*" -files = [ - {file = "ansiwrap-0.8.4-py2.py3-none-any.whl", hash = "sha256:7b053567c88e1ad9eed030d3ac41b722125e4c1271c8a99ade797faff1f49fb1"}, - {file = "ansiwrap-0.8.4.zip", hash = "sha256:ca0c740734cde59bf919f8ff2c386f74f9a369818cdc60efe94893d01ea8d9b7"}, -] - -[package.dependencies] -textwrap3 = ">=0.9.2" - [[package]] name = "appnope" version = "0.1.3" @@ -1450,17 +1436,6 @@ pure-eval = "*" [package.extras] tests = ["cython", "littleutils", "pygments", "pytest", "typeguard"] -[[package]] -name = "textwrap3" -version = "0.9.2" -description = "textwrap from Python 3.6 backport (plus a few tweaks)" -optional = false -python-versions = "*" -files = [ - {file = "textwrap3-0.9.2-py2.py3-none-any.whl", hash = "sha256:bf5f4c40faf2a9ff00a9e0791fed5da7415481054cef45bb4a3cfb1f69044ae0"}, - {file = "textwrap3-0.9.2.zip", hash = "sha256:5008eeebdb236f6303dcd68f18b856d355f6197511d952ba74bc75e40e0c3414"}, -] - [[package]] name = "toml" version = "0.10.2" @@ -1680,4 +1655,4 @@ testing = ["flake8 (<5)", "func-timeout", "jaraco.functools", "jaraco.itertools" [metadata] lock-version = "2.0" python-versions = ">=3.10.0, <3.13" -content-hash = "fe86fc1b8b2f74aba939a019b87fbe33fec5853c40b60a596ce8467a97de0045" +content-hash = "83b197f5b2f23aa128778616baf8a0300e8a4fb0ad8ac539593cf4e7702fa5e6" diff --git a/pyproject.toml b/pyproject.toml index 3f9b83701..e752aa2e5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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 diff --git a/tests/bdd/features/format.feature b/tests/bdd/features/format.feature index d03b1ab2c..b02831d67 100644 --- a/tests/bdd/features/format.feature +++ b/tests/bdd/features/format.feature @@ -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