Skip to content

The agent that grows with you

fix(cli): measure the re-wrapped chrome as prompt_toolkit wrote it

teknium129b9d7a5c2a5
diff --git a/hermes_cli/cli_terminal_mixin.py b/hermes_cli/cli_terminal_mixin.py
index 577bb57ec6a..3199228e3c1 100644
--- a/hermes_cli/cli_terminal_mixin.py
+++ b/hermes_cli/cli_terminal_mixin.py
@@ -246,11 +246,21 @@ class CLITerminalMixin:
if screen is None:
drawn = app.layout.container.preferred_height(size.columns, size.rows).preferred
elif reflowed and size.columns > 0:
+ # prompt_toolkit writes a row up to its last cell that shows something (trailing
+ # blanks without a colour are left to erase-to-end-of-line): that is its width.
+ has_style = renderer._style_string_has_style
+
+ def shows(ch):
+ if ch.char != " ":
+ return True
+ try:
+ return bool(has_style[ch.style]) if has_style is not None else bool(ch.style)
+ except Exception:
+ return bool(ch.style)
drawn = 0
for y in range(screen.height):
- # Trailing default-style blanks are not written (erase-to-end-of-line instead).
- cells = [x for x, ch in screen.data_buffer[y].items() if ch.char != " " or ch.style]
- drawn += max(1, -(-(max(cells) + 1) // size.columns)) if cells else 1
+ last = max((x for x, ch in screen.data_buffer[y].items() if shows(ch)), default=0)
+ drawn += -(-(last + 1) // size.columns)
else:
drawn = screen.height
rows = max(0, size.rows - max(renderer._min_available_height, drawn))
diff --git a/tests/hermes_cli/test_cli_force_redraw.py b/tests/hermes_cli/test_cli_force_redraw.py
index 5784b10f6ee..259ef10c86d 100644
--- a/tests/hermes_cli/test_cli_force_redraw.py
+++ b/tests/hermes_cli/test_cli_force_redraw.py
@@ -40,6 +40,7 @@ def _fake_app(*, rows, columns, chrome, painted=None):
screen.data_buffer[y][x] = Char("─")
screen.height = len(painted)
app.renderer._last_screen = screen
+ app.renderer._style_string_has_style = None
app.renderer._min_available_height = 0
app.layout.container.preferred_height.return_value.preferred = chrome
return app
@@ -67,6 +68,9 @@ class TestForceFullRedraw:
height), each of its rows re-wrapped to the new width (#95375).
"""
app = _fake_app(rows=30, columns=90, chrome=5, painted=[0, 200, 50, 200, 30, 200, 89])
+ from prompt_toolkit.layout.screen import Char
+ for x in range(200): # blanks without a colour are never written: one row, not three
+ app.renderer._last_screen.data_buffer[0][x] = Char(" ")
events = []
fits = []
app.renderer.output.erase_end_of_line.side_effect = lambda: events.append("erase")