Return a console-safe preview that will not crash on Windows GBK terminals.
(text: Any, max_len: int = 120)
| 906 | |
| 907 | |
| 908 | def safe_console_text(text: Any, max_len: int = 120) -> str: |
| 909 | """Return a console-safe preview that will not crash on Windows GBK terminals.""" |
| 910 | preview = str(text).replace("\r", "").replace("\n", "\\n")[:max_len] |
| 911 | encoding = getattr(sys.stdout, "encoding", None) or "utf-8" |
| 912 | try: |
| 913 | return preview.encode(encoding, errors="replace").decode(encoding, errors="replace") |
| 914 | except Exception: |
| 915 | return preview.encode("ascii", errors="replace").decode("ascii") |
| 916 | |
| 917 | |
| 918 | PROFILE_WEIGHT_COMMAND_RE = re.compile( |