2020-03-16 22:18:22 +00:00
|
|
|
import subprocess
|
2022-01-13 14:23:15 +00:00
|
|
|
from typing import List, Tuple
|
2022-01-17 15:02:04 +00:00
|
|
|
import sys
|
|
|
|
|
|
|
|
from rich.progress import Progress
|
2020-03-15 15:46:03 +00:00
|
|
|
|
2020-03-16 22:18:22 +00:00
|
|
|
from wcwidth import wcwidth
|
2020-03-15 15:46:03 +00:00
|
|
|
|
|
|
|
|
2020-03-16 22:18:22 +00:00
|
|
|
progress = Progress()
|
|
|
|
|
|
|
|
|
2020-03-22 15:46:43 +00:00
|
|
|
def make_widths_table() -> List[Tuple[int, int, int]]:
|
2020-03-16 22:18:22 +00:00
|
|
|
table: List[Tuple[int, int, int]] = []
|
|
|
|
append = table.append
|
|
|
|
|
|
|
|
make_table_task = progress.add_task("Calculating table...")
|
|
|
|
|
2022-01-17 15:02:04 +00:00
|
|
|
widths = (
|
|
|
|
(codepoint, wcwidth(chr(codepoint)))
|
|
|
|
for codepoint in range(0, sys.maxunicode + 1)
|
|
|
|
)
|
2020-03-16 22:18:22 +00:00
|
|
|
|
2020-03-22 15:46:43 +00:00
|
|
|
_widths = [(codepoint, width) for codepoint, width in widths if width != 1]
|
|
|
|
iter_widths = iter(_widths)
|
2020-03-16 22:18:22 +00:00
|
|
|
|
|
|
|
endpoint, group_cell_size = next(iter_widths)
|
|
|
|
start_codepoint = end_codepoint = endpoint
|
|
|
|
for codepoint, cell_size in progress.track(
|
2020-03-22 15:46:43 +00:00
|
|
|
iter_widths, task_id=make_table_task, total=len(_widths) - 1
|
2020-03-16 22:18:22 +00:00
|
|
|
):
|
|
|
|
if cell_size != group_cell_size or codepoint != end_codepoint + 1:
|
|
|
|
append((start_codepoint, end_codepoint, group_cell_size))
|
|
|
|
start_codepoint = end_codepoint = codepoint
|
|
|
|
group_cell_size = cell_size
|
2020-03-15 15:46:03 +00:00
|
|
|
else:
|
2020-03-16 22:18:22 +00:00
|
|
|
end_codepoint = codepoint
|
|
|
|
append((start_codepoint, end_codepoint, group_cell_size))
|
|
|
|
return table
|
2020-03-15 15:46:03 +00:00
|
|
|
|
2020-03-16 22:18:22 +00:00
|
|
|
|
|
|
|
def get_cell_size(table: List[Tuple[int, int, int]], character: str) -> int:
|
2022-01-17 15:02:04 +00:00
|
|
|
|
2020-03-16 22:18:22 +00:00
|
|
|
codepoint = ord(character)
|
|
|
|
lower_bound = 0
|
|
|
|
upper_bound = len(table) - 1
|
|
|
|
index = (lower_bound + upper_bound) // 2
|
|
|
|
while True:
|
|
|
|
start, end, width = table[index]
|
|
|
|
if codepoint < start:
|
|
|
|
upper_bound = index - 1
|
|
|
|
elif codepoint > end:
|
|
|
|
lower_bound = index + 1
|
|
|
|
else:
|
|
|
|
return width
|
|
|
|
if upper_bound < lower_bound:
|
|
|
|
break
|
|
|
|
index = (lower_bound + upper_bound) // 2
|
|
|
|
return 1
|
|
|
|
|
|
|
|
|
|
|
|
def test(widths_table):
|
|
|
|
for codepoint in progress.track(
|
|
|
|
range(0, sys.maxunicode + 1), description="Testing..."
|
|
|
|
):
|
|
|
|
character = chr(codepoint)
|
|
|
|
width1 = get_cell_size(widths_table, character)
|
|
|
|
width2 = wcwidth(character)
|
2022-01-17 15:02:04 +00:00
|
|
|
if width1 != width2:
|
2020-03-16 22:18:22 +00:00
|
|
|
print(f"{width1} != {width2}")
|
2022-01-17 15:02:04 +00:00
|
|
|
break
|
2020-03-15 15:46:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
def run():
|
2020-03-16 22:18:22 +00:00
|
|
|
with progress:
|
|
|
|
widths_table = make_widths_table()
|
|
|
|
test(widths_table)
|
|
|
|
table_file = f"""# Auto generated by make_terminal_widths.py
|
|
|
|
|
|
|
|
CELL_WIDTHS = {widths_table!r}
|
|
|
|
|
|
|
|
"""
|
|
|
|
with open("../rich/_cell_widths.py", "wt") as fh:
|
|
|
|
fh.write(table_file)
|
|
|
|
|
|
|
|
subprocess.run("black ../rich/_cell_widths.py", shell=True)
|
2020-03-15 15:46:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
run()
|