Skip to content
Open
Show file tree
Hide file tree
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
7 changes: 5 additions & 2 deletions src/kimi_cli/utils/string.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@ def shorten(text: str, *, width: int, placeholder: str = "…") -> str:

def shorten_middle(text: str, width: int, remove_newline: bool = True) -> str:
"""Shorten the text by inserting ellipsis in the middle."""
if len(text) <= width:
return text
# Strip newlines first: callers rely on this to keep the result on a single
# line, and doing it before the length check ensures short multi-line inputs
# are flattened too (and that the check sees the post-flattening length).
if remove_newline:
text = _NEWLINE_RE.sub(" ", text)
if len(text) <= width:
return text
return text[: width // 2] + "..." + text[-width // 2 :]


Expand Down
31 changes: 31 additions & 0 deletions tests/utils/test_shorten.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,34 @@ def test_placeholder_longer_than_cut():
result = shorten("hello", width=1, placeholder="...")
assert len(result) <= 1
assert result == "h"


def test_shorten_middle_short_text_unchanged():
from kimi_cli.utils.string import shorten_middle

assert shorten_middle("hello", 50) == "hello"


def test_shorten_middle_strips_newlines_in_short_text():
"""remove_newline must apply even when the text is shorter than width."""
from kimi_cli.utils.string import shorten_middle

result = shorten_middle("ls -la\npwd\necho done", 50)
assert "\n" not in result
assert result == "ls -la pwd echo done"


def test_shorten_middle_can_keep_newlines():
from kimi_cli.utils.string import shorten_middle

result = shorten_middle("a\nb", 50, remove_newline=False)
assert result == "a\nb"


def test_shorten_middle_long_text_gets_ellipsis():
from kimi_cli.utils.string import shorten_middle

text = "x" * 100
result = shorten_middle(text, 50)
assert "..." in result
assert result == "x" * 25 + "..." + "x" * 25
Loading