mirror of
https://github.com/jrnl-org/jrnl.git
synced 2025-05-10 08:38:32 +02:00
Use rich instead of ansiwrap to wrap text (#1693)
This commit is contained in:
parent
525cce3e92
commit
cb69bb474c
5 changed files with 22 additions and 58 deletions
|
@ -7,10 +7,9 @@ import os
|
||||||
import re
|
import re
|
||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
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 +128,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(
|
||||||
|
@ -143,35 +142,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(
|
|
||||||
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
|
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.
|
body = "\n".join(
|
||||||
# TL;DR: I'm sorry.
|
|
||||||
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
|
||||||
|
|
|
@ -131,3 +131,12 @@ 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) -> 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
27
poetry.lock
generated
|
@ -1,19 +1,5 @@
|
||||||
# This file is automatically @generated by Poetry 1.5.0 and should not be changed by hand.
|
# 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]]
|
[[package]]
|
||||||
name = "appnope"
|
name = "appnope"
|
||||||
version = "0.1.3"
|
version = "0.1.3"
|
||||||
|
@ -1450,17 +1436,6 @@ pure-eval = "*"
|
||||||
[package.extras]
|
[package.extras]
|
||||||
tests = ["cython", "littleutils", "pygments", "pytest", "typeguard"]
|
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]]
|
[[package]]
|
||||||
name = "toml"
|
name = "toml"
|
||||||
version = "0.10.2"
|
version = "0.10.2"
|
||||||
|
@ -1680,4 +1655,4 @@ testing = ["flake8 (<5)", "func-timeout", "jaraco.functools", "jaraco.itertools"
|
||||||
[metadata]
|
[metadata]
|
||||||
lock-version = "2.0"
|
lock-version = "2.0"
|
||||||
python-versions = ">=3.10.0, <3.13"
|
python-versions = ">=3.10.0, <3.13"
|
||||||
content-hash = "fe86fc1b8b2f74aba939a019b87fbe33fec5853c40b60a596ce8467a97de0045"
|
content-hash = "83b197f5b2f23aa128778616baf8a0300e8a4fb0ad8ac539593cf4e7702fa5e6"
|
||||||
|
|
|
@ -29,7 +29,6 @@ classifiers = [
|
||||||
[tool.poetry.dependencies]
|
[tool.poetry.dependencies]
|
||||||
python = ">=3.10.0, <3.13"
|
python = ">=3.10.0, <3.13"
|
||||||
|
|
||||||
ansiwrap = "^0.8.4"
|
|
||||||
colorama = ">=0.4" # https://github.com/tartley/colorama/blob/master/CHANGELOG.rst
|
colorama = ">=0.4" # https://github.com/tartley/colorama/blob/master/CHANGELOG.rst
|
||||||
cryptography = ">=3.0" # https://cryptography.io/en/latest/api-stability.html
|
cryptography = ">=3.0" # https://cryptography.io/en/latest/api-stability.html
|
||||||
keyring = ">=21.0" # https://github.com/jaraco/keyring#integration
|
keyring = ">=21.0" # https://github.com/jaraco/keyring#integration
|
||||||
|
|
Loading…
Add table
Reference in a new issue