Improve typing in wcwidth module

pull/427/merge
Ivan Habunek 2023-11-24 09:52:22 +01:00
rodzic 509afd16a8
commit 48d9caef05
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: F5F0623FF5EBCB3D
1 zmienionych plików z 8 dodań i 7 usunięć

Wyświetl plik

@ -3,11 +3,12 @@ Utilities for dealing with string containing wide characters.
""" """
import re import re
from typing import Generator, List
from wcwidth import wcwidth, wcswidth from wcwidth import wcwidth, wcswidth
def _wc_hard_wrap(line, length): def _wc_hard_wrap(line: str, length: int) -> Generator[str, None, None]:
""" """
Wrap text to length characters, breaking when target length is reached, Wrap text to length characters, breaking when target length is reached,
taking into account character width. taking into account character width.
@ -20,7 +21,7 @@ def _wc_hard_wrap(line, length):
char_len = wcwidth(char) char_len = wcwidth(char)
if chars_len + char_len > length: if chars_len + char_len > length:
yield "".join(chars) yield "".join(chars)
chars = [] chars: List[str] = []
chars_len = 0 chars_len = 0
chars.append(char) chars.append(char)
@ -30,7 +31,7 @@ def _wc_hard_wrap(line, length):
yield "".join(chars) yield "".join(chars)
def wc_wrap(text, length): def wc_wrap(text: str, length: int) -> Generator[str, None, None]:
""" """
Wrap text to given length, breaking on whitespace and taking into account Wrap text to given length, breaking on whitespace and taking into account
character width. character width.
@ -38,7 +39,7 @@ def wc_wrap(text, length):
Meant for use on a single line or paragraph. Will destroy spacing between Meant for use on a single line or paragraph. Will destroy spacing between
words and paragraphs and any indentation. words and paragraphs and any indentation.
""" """
line_words = [] line_words: List[str] = []
line_len = 0 line_len = 0
words = re.split(r"\s+", text.strip()) words = re.split(r"\s+", text.strip())
@ -66,7 +67,7 @@ def wc_wrap(text, length):
yield from _wc_hard_wrap(line, length) yield from _wc_hard_wrap(line, length)
def trunc(text, length): def trunc(text: str, length: int) -> str:
""" """
Truncates text to given length, taking into account wide characters. Truncates text to given length, taking into account wide characters.
@ -98,7 +99,7 @@ def trunc(text, length):
return text[:-n].strip() + '' return text[:-n].strip() + ''
def pad(text, length): def pad(text: str, length: int) -> str:
"""Pads text to given length, taking into account wide characters.""" """Pads text to given length, taking into account wide characters."""
text_length = wcswidth(text) text_length = wcswidth(text)
@ -108,7 +109,7 @@ def pad(text, length):
return text return text
def fit_text(text, length): def fit_text(text: str, length: int) -> str:
"""Makes text fit the given length by padding or truncating it.""" """Makes text fit the given length by padding or truncating it."""
text_length = wcswidth(text) text_length = wcswidth(text)