mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-01-23 09:20:31 -06:00
fix CJK characters width in output
This commit is contained in:
committed by
Anthony Sottile
parent
528c7afd18
commit
605b39f617
@@ -6,6 +6,7 @@ import os
|
||||
import re
|
||||
import subprocess
|
||||
import time
|
||||
import unicodedata
|
||||
from typing import Any
|
||||
from typing import Collection
|
||||
from typing import Dict
|
||||
@@ -33,8 +34,13 @@ from pre_commit.util import EnvironT
|
||||
logger = logging.getLogger('pre_commit')
|
||||
|
||||
|
||||
def _len_cjk(msg: str) -> int:
|
||||
widths = {'A': 1, 'F': 2, 'H': 1, 'N': 1, 'Na': 1, 'W': 2}
|
||||
return sum(widths[unicodedata.east_asian_width(c)] for c in msg)
|
||||
|
||||
|
||||
def _start_msg(*, start: str, cols: int, end_len: int) -> str:
|
||||
dots = '.' * (cols - len(start) - end_len - 1)
|
||||
dots = '.' * (cols - _len_cjk(start) - end_len - 1)
|
||||
return f'{start}{dots}'
|
||||
|
||||
|
||||
@@ -47,7 +53,7 @@ def _full_msg(
|
||||
use_color: bool,
|
||||
postfix: str = '',
|
||||
) -> str:
|
||||
dots = '.' * (cols - len(start) - len(postfix) - len(end_msg) - 1)
|
||||
dots = '.' * (cols - _len_cjk(start) - len(postfix) - len(end_msg) - 1)
|
||||
end = color.format_color(end_msg, end_color, use_color)
|
||||
return f'{start}{dots}{postfix}{end}\n'
|
||||
|
||||
@@ -206,7 +212,7 @@ def _compute_cols(hooks: Sequence[Hook]) -> int:
|
||||
Hook name...(no files to check) Skipped
|
||||
"""
|
||||
if hooks:
|
||||
name_len = max(len(hook.name) for hook in hooks)
|
||||
name_len = max(_len_cjk(hook.name) for hook in hooks)
|
||||
else:
|
||||
name_len = 0
|
||||
|
||||
|
||||
@@ -52,6 +52,18 @@ def test_full_msg():
|
||||
assert ret == 'start......end\n'
|
||||
|
||||
|
||||
def test_full_msg_with_cjk():
|
||||
ret = _full_msg(
|
||||
start='啊あ아',
|
||||
end_msg='end',
|
||||
end_color='',
|
||||
use_color=False,
|
||||
cols=15,
|
||||
)
|
||||
# 5 dots: 15 - 6 - 3 - 1
|
||||
assert ret == '啊あ아.....end\n'
|
||||
|
||||
|
||||
def test_full_msg_with_color():
|
||||
ret = _full_msg(
|
||||
start='start',
|
||||
|
||||
Reference in New Issue
Block a user