-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunicode_grep_warn.py
More file actions
37 lines (29 loc) · 990 Bytes
/
unicode_grep_warn.py
File metadata and controls
37 lines (29 loc) · 990 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/env python3
"""PreToolUse hook: warn to also search unicode escapes when grepping CJK characters."""
import json
import re
import sys
def main():
try:
input_data = json.load(sys.stdin)
except (json.JSONDecodeError, EOFError):
print("{}")
return
tool_name = input_data.get("tool_name", "")
tool_input = input_data.get("tool_input", {})
if tool_name not in ("Bash", "Grep"):
print("{}")
return
# Check for CJK characters in the search pattern/command
search_text = tool_input.get("command", "") or tool_input.get("pattern", "")
if not re.search(r'[\u4e00-\u9fff\u3400-\u4dbf]', search_text):
print("{}")
return
print(json.dumps({
"systemMessage": (
"⚠️ **CJK grep detected.** Also search unicode escapes (`\\uXXXX`) — "
"some files store Chinese text as escaped unicode, not raw UTF-8."
)
}))
if __name__ == "__main__":
main()