Use rich instead of ansiwrap to wrap text (#1693)

This commit is contained in:
Micah Jerome Ellison 2023-07-29 14:36:43 -07:00 committed by GitHub
parent 525cce3e92
commit cb69bb474c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 22 additions and 58 deletions

View file

@ -7,10 +7,9 @@ import os
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 @@ class Entry:
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 @@ class Entry:
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 = 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

@ -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
poetry.lock generated
View file

@ -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"

View file

@ -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