1 parent 528c7af commit 605b39fCopy full SHA for 605b39f
2 files changed
pre_commit/commands/run.py
@@ -6,6 +6,7 @@
6
import re
7
import subprocess
8
import time
9
+import unicodedata
10
from typing import Any
11
from typing import Collection
12
from typing import Dict
@@ -33,8 +34,13 @@
33
34
logger = logging.getLogger('pre_commit')
35
36
37
+def _len_cjk(msg: str) -> int:
38
+ widths = {'A': 1, 'F': 2, 'H': 1, 'N': 1, 'Na': 1, 'W': 2}
39
+ return sum(widths[unicodedata.east_asian_width(c)] for c in msg)
40
+
41
42
def _start_msg(*, start: str, cols: int, end_len: int) -> str:
- dots = '.' * (cols - len(start) - end_len - 1)
43
+ dots = '.' * (cols - _len_cjk(start) - end_len - 1)
44
return f'{start}{dots}'
45
46
@@ -47,7 +53,7 @@ def _full_msg(
47
53
use_color: bool,
48
54
postfix: str = '',
49
55
) -> str:
50
- dots = '.' * (cols - len(start) - len(postfix) - len(end_msg) - 1)
56
+ dots = '.' * (cols - _len_cjk(start) - len(postfix) - len(end_msg) - 1)
51
57
end = color.format_color(end_msg, end_color, use_color)
52
58
return f'{start}{dots}{postfix}{end}\n'
59
@@ -206,7 +212,7 @@ def _compute_cols(hooks: Sequence[Hook]) -> int:
206
212
Hook name...(no files to check) Skipped
207
213
"""
208
214
if hooks:
209
- name_len = max(len(hook.name) for hook in hooks)
215
+ name_len = max(_len_cjk(hook.name) for hook in hooks)
210
216
else:
211
217
name_len = 0
218
tests/commands/run_test.py
@@ -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='',
60
+ use_color=False,
61
+ cols=15,
62
+ )
63
+ # 5 dots: 15 - 6 - 3 - 1
64
+ assert ret == '啊あ아.....end\n'
65
66
67
def test_full_msg_with_color():
68
ret = _full_msg(
69
start='start',
0 commit comments