Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions src/kimi_cli/utils/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import importlib
import socket
import sys
import textwrap


Expand Down Expand Up @@ -86,6 +87,25 @@ def get_network_addresses() -> list[str]:
return addresses


def _safe_print(text: str) -> None:
"""Print *text*, degrading gracefully when stdout can't encode a character.

When stdout is redirected to a file or pipe on Windows, Python uses the
system locale encoding (e.g. GBK/cp936) instead of UTF-8. Banner characters
such as ``➜`` (U+279C) are not representable there, so a plain ``print``
raises ``UnicodeEncodeError`` and kills the process before the server binds
its port. Fall back to the stream's encoding with ``errors="replace"`` so
the banner still prints (with a placeholder) and startup continues.
"""
try:
print(text)
except UnicodeEncodeError:
stream = sys.stdout
encoding = getattr(stream, "encoding", None) or "utf-8"
safe = text.encode(encoding, errors="replace").decode(encoding)
print(safe)


def print_banner(lines: list[str]) -> None:
"""Print a boxed banner with tag conventions (<center>, <nowrap>, <hr>)."""
processed: list[str] = []
Expand All @@ -106,16 +126,16 @@ def strip_tags(s: str) -> str:
width = max(60, *(len(line) for line in content_lines))
top = "+" + "=" * (width + 2) + "+"

print(top)
_safe_print(top)
for line in processed:
if line == "<hr>":
print("|" + "-" * (width + 2) + "|")
_safe_print("|" + "-" * (width + 2) + "|")
elif line.startswith("<center>"):
content = line.removeprefix("<center>")
print(f"| {content.center(width)} |")
_safe_print(f"| {content.center(width)} |")
elif line.startswith("<nowrap>"):
content = line.removeprefix("<nowrap>")
print(f"| {content.ljust(width)} |")
_safe_print(f"| {content.ljust(width)} |")
else:
print(f"| {line.ljust(width)} |")
print(top)
_safe_print(f"| {line.ljust(width)} |")
_safe_print(top)
Loading