Debian Codegraph @

tools/make_width_tables.py

raw
1 import subprocess
2 from pathlib import Path
3
4 from wcwidth import list_versions
5 from wcwidth.table_vs16 import VS16_NARROW_TO_WIDE
6 from wcwidth.table_wide import WIDE_EASTASIAN
7 from wcwidth.table_zero import ZERO_WIDTH
8
9 from rich.cells import CellTable
10
11 UNICODE_VERSIONS: list[str] = list_versions()
12
13
14 path = Path("../rich/_unicode_data/_versions.py").resolve().absolute()
15 init = f"""\
16 VERSIONS = {UNICODE_VERSIONS!r}
17 """
18
19 with open(path, "wt") as init_file:
20 init_file.write(init)
21
22 subprocess.run(f"black {path}", shell=True)
23
24
25 narrow_to_wide: set[str] = set()
26 for start, end in VS16_NARROW_TO_WIDE["9.0.0"]:
27 narrow_to_wide.update(chr(codepoint) for codepoint in range(start, end + 1))
28
29 for version in UNICODE_VERSIONS:
30 table: list[tuple[int, int, int]] = []
31 wide_east_asian: list[tuple[int, int]] = WIDE_EASTASIAN.get(version, [])
32 for start, end in wide_east_asian:
33 table.append((start, end, 2))
34 zero_wide: list[tuple[int, int]] = ZERO_WIDTH.get(version, [])
35 for start, end in zero_wide:
36 table.append((start, end, 0))
37 table.sort()
38
39 cell_table = CellTable(version, table, frozenset(narrow_to_wide))
40
41 table_file = f"""# Auto generated by tools/make_width_tables.py
42 # Data from wcwidth project (https://github.com/jquast/wcwidth)
43
44 from rich.cells import CellTable
45
46 cell_table = CellTable({cell_table.unicode_version!r}, {cell_table.widths!r}, frozenset({sorted(cell_table.narrow_to_wide)!r}))
47 """
48 version_path = version.replace(".", "-")
49 path = Path(f"../rich/_unicode_data/unicode{version_path}.py").resolve().absolute()
50 with open(path, "wt") as file_out:
51 file_out.write(table_file)
52
53 subprocess.run(f"black {path}", shell=True)