Disregard the length for styling tags

This commit is contained in:
Huy Ngo 2024-08-15 17:23:32 +07:00
parent 2ad49b7774
commit b7dab5926e

View file

@ -7,6 +7,18 @@ from typing import Generator, List
from wcwidth import wcswidth, wcwidth
STYLE_TAG_PATTERN = re.compile(r"""
(
(?<!\\) # not preceeded by a backslash - allows escaping
< # literal
(/)? # optional closing - first group
(.*?) # style names - ungreedy - second group
>) # literal
""", re.X)
STYLES = {'reset', 'bold', 'dim', 'italic', 'underline',
'red', 'green', 'yellow', 'blue', 'magenta', 'cyan'}
def _wc_hard_wrap(line: str, length: int) -> Generator[str, None, None]:
"""
@ -45,6 +57,11 @@ def wc_wrap(text: str, length: int) -> Generator[str, None, None]:
words = re.split(r"\s+", text.strip())
for word in words:
word_len = wcswidth(word)
matches = re.findall(STYLE_TAG_PATTERN, word)
for match in matches:
full, _, name = match
if name in STYLES:
word_len -= len(full)
if line_words and line_len + word_len > length:
line = " ".join(line_words)