ruff is faster and handle everything we had prior. isort configuration done based on the indication from https://github.com/astral-sh/ruff/issues/4670, previousely based on reorder-python-import (#11896) flake8-docstrings was a wrapper around pydocstyle (now archived) that explicitly asks to use ruff in https://github.com/PyCQA/pydocstyle/pull/658. flake8-typing-import is useful mainly for project that support python 3.7 and the one useful check will be implemented in https://github.com/astral-sh/ruff/issues/2302 We need to keep blacken-doc because ruff does not handle detection of python code inside .md and .rst. The direct link to the repo is now used to avoid a redirection. Manual fixes: - Lines that became too long - % formatting that was not done automatically - type: ignore that were moved around - noqa of hard to fix issues (UP031 generally) - fmt: off and fmt: on that is not really identical between black and ruff - autofix re-order in pre-commit from faster to slower Co-authored-by: Ran Benita <ran@unusedvar.com>
56 lines
1.2 KiB
Python
56 lines
1.2 KiB
Python
from functools import lru_cache
|
|
import unicodedata
|
|
|
|
|
|
@lru_cache(100)
|
|
def wcwidth(c: str) -> int:
|
|
"""Determine how many columns are needed to display a character in a terminal.
|
|
|
|
Returns -1 if the character is not printable.
|
|
Returns 0, 1 or 2 for other characters.
|
|
"""
|
|
o = ord(c)
|
|
|
|
# ASCII fast path.
|
|
if 0x20 <= o < 0x07F:
|
|
return 1
|
|
|
|
# Some Cf/Zp/Zl characters which should be zero-width.
|
|
if (
|
|
o == 0x0000
|
|
or 0x200B <= o <= 0x200F
|
|
or 0x2028 <= o <= 0x202E
|
|
or 0x2060 <= o <= 0x2063
|
|
):
|
|
return 0
|
|
|
|
category = unicodedata.category(c)
|
|
|
|
# Control characters.
|
|
if category == "Cc":
|
|
return -1
|
|
|
|
# Combining characters with zero width.
|
|
if category in ("Me", "Mn"):
|
|
return 0
|
|
|
|
# Full/Wide east asian characters.
|
|
if unicodedata.east_asian_width(c) in ("F", "W"):
|
|
return 2
|
|
|
|
return 1
|
|
|
|
|
|
def wcswidth(s: str) -> int:
|
|
"""Determine how many columns are needed to display a string in a terminal.
|
|
|
|
Returns -1 if the string contains non-printable characters.
|
|
"""
|
|
width = 0
|
|
for c in unicodedata.normalize("NFC", s):
|
|
wc = wcwidth(c)
|
|
if wc < 0:
|
|
return -1
|
|
width += wc
|
|
return width
|