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
9 changes: 7 additions & 2 deletions deeptutor_cli/_tool_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def truncate_for_display(
if head_lines <= 0:
return "", _line_count(body)

lines = body.split("\n")
lines = _lines(body)
if len(lines) <= head_lines:
head_slice = lines
hidden = 0
Expand All @@ -62,10 +62,15 @@ def truncate_for_display(
return "\n".join(clipped), hidden


def _lines(body: str) -> list[str]:
# The newline that ends the last line does not start another one.
return body.removesuffix("\n").split("\n")


def _line_count(body: str) -> int:
if not body:
return 0
return body.count("\n") + 1
return len(_lines(body))


def _clip_long_line(line: str, line_hard_cap: int) -> str:
Expand Down
33 changes: 33 additions & 0 deletions tests/cli/test_tool_result.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""A trailing newline is not a hidden line in the inline tool-result preview.

Tool output usually ends with ``\\n``. ``body.split("\\n")`` counted the empty string
after it as a line, so output that exactly fit the preview printed "+1 more line" with
nothing left to expand.
"""

from deeptutor_cli._tool_result import truncate_for_display


def test_output_that_fits_hides_nothing_even_with_a_trailing_newline() -> None:
body = "".join(f"line {n}\n" for n in range(10))

visible, hidden = truncate_for_display(body, head_lines=10)

assert hidden == 0
assert visible.splitlines() == [f"line {n}" for n in range(10)]


def test_hidden_count_ignores_a_trailing_newline() -> None:
body = "".join(f"line {n}\n" for n in range(12))

assert truncate_for_display(body, head_lines=10)[1] == 2
assert truncate_for_display(body.rstrip("\n"), head_lines=10)[1] == 2


def test_collapsed_preview_counts_lines_the_same_way() -> None:
assert truncate_for_display("a\nb\n", head_lines=0) == ("", 2)
assert truncate_for_display("a\nb", head_lines=0) == ("", 2)


def test_a_real_blank_last_line_still_counts() -> None:
assert truncate_for_display("a\n\n", head_lines=0) == ("", 2)
Loading