rich/tools/make_width_tables.py
2026-01-18 14:08:11 +00:00

51 lines
1.4 KiB
Python

import subprocess
from pathlib import Path
from wcwidth import list_versions
from wcwidth.table_vs16 import VS16_NARROW_TO_WIDE
from wcwidth.table_wide import WIDE_EASTASIAN
from wcwidth.table_zero import ZERO_WIDTH
from rich.cell_string import CellTable
UNICODE_VERSIONS: list[str] = list_versions()
path = Path("../rich/_unicode_data/_versions.py").resolve().absolute()
init = f"""\
VERSIONS = {UNICODE_VERSIONS!r}
"""
with open(path, "wt") as init_file:
init_file.write(init)
narrow_to_wide: set[int] = set()
for start, end in VS16_NARROW_TO_WIDE["9.0.0"]:
narrow_to_wide.update(range(start, end))
for version in UNICODE_VERSIONS:
table: list[tuple[int, int, int]] = []
wide_east_asian: list[tuple[int, int]] = WIDE_EASTASIAN.get(version, [])
for start, end in wide_east_asian:
table.append((start, end, 2))
zero_wide: list[tuple[int, int]] = ZERO_WIDTH.get(version, [])
for start, end in zero_wide:
table.append((start, end, 0))
table.sort()
cell_table = CellTable(version, table, frozenset(narrow_to_wide))
table_file = f"""# Auto generated by tools/make_width_tables.py
from rich.cell_string import CellTable
cell_table = {cell_table!r}
"""
version_path = version.replace(".", "-")
path = Path(f"../rich/_unicode_data/unicode{version_path}.py").resolve().absolute()
with open(path, "wt") as file_out:
file_out.write(table_file)
subprocess.run(f"black {path}", shell=True)