Skip to content

Commit

Permalink
Improve typing in wcwidth module
Browse files Browse the repository at this point in the history
  • Loading branch information
ihabunek committed Nov 24, 2023
1 parent 509afd1 commit 48d9cae
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions toot/wcstring.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
"""

import re
from typing import Generator, List

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,
taking into account character width.
Expand All @@ -20,7 +21,7 @@ def _wc_hard_wrap(line, length):
char_len = wcwidth(char)
if chars_len + char_len > length:
yield "".join(chars)
chars = []
chars: List[str] = []
chars_len = 0

chars.append(char)
Expand All @@ -30,15 +31,15 @@ def _wc_hard_wrap(line, length):
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
character width.
Meant for use on a single line or paragraph. Will destroy spacing between
words and paragraphs and any indentation.
"""
line_words = []
line_words: List[str] = []
line_len = 0

words = re.split(r"\s+", text.strip())
Expand Down Expand Up @@ -66,7 +67,7 @@ def wc_wrap(text, 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.
Expand Down Expand Up @@ -98,7 +99,7 @@ def trunc(text, length):
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."""
text_length = wcswidth(text)

Expand All @@ -108,7 +109,7 @@ def pad(text, length):
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."""
text_length = wcswidth(text)

Expand Down

0 comments on commit 48d9cae

Please sign in to comment.