fix(app): fix garbled inline app output#6080
Conversation
Fix garbled inline app output when `inline_no_clear=True`. Currently the terminal cursor isn't actually "reset" before printing the screen contents. This doesn't matter for most inline apps as the cursor is already offset at (0, 0). But if the app contains an `Input` widget, this will update the cursor position so the output will be garbled. Fixes #6064
|
Example app for testing: from textual.app import App, ComposeResult
from textual.widgets import Input
class ExampleApp(App):
def compose(self) -> ComposeResult:
yield Input("Press ctrl+q to quit")
def on_mount(self) -> None:
# Workaround possible bug with the initial input cursor position
# https://github.com/Textualize/textual/issues/6064#issuecomment-3217177954
self.simulate_key("right")
# Show the terminal cursor for debugging
self._driver.write("\x1b[?25h")
if __name__ == "__main__":
app = ExampleApp()
app.run(
inline=True,
inline_no_clear=True,
) |
| cursor_x, cursor_y = self._previous_cursor_position | ||
| self._driver.write( | ||
| Control.move(-cursor_x, -cursor_y + 1).segment.text | ||
| Control.move(-cursor_x, -cursor_y).segment.text |
There was a problem hiding this comment.
-cursor_y + 1 will duplicate the first line in the output. But I think this was included in case the app crashes so the traceback doesn't overwrite the prompt?
There was a problem hiding this comment.
Another test app to check the prompt is correctly restored:
from textual.app import App, ComposeResult
from textual.widgets import Button
INLINE_NO_CLEAR = False
INVALID_CSS = False
class ExitRenderablesApp(App):
CSS = "Screen { background: invalid; }" if INVALID_CSS else ""
def compose(self) -> ComposeResult:
yield Button.error("Runtime Error")
def on_mount(self) -> None:
# Show the terminal cursor for debugging
self._driver.write("\x1b[?25h")
def on_button_pressed(self) -> None:
raise RuntimeError()
if __name__ == "__main__":
app = ExitRenderablesApp()
app.run(
inline=True,
inline_no_clear=INLINE_NO_CLEAR,
)Following the fix for garbled inline app outputs in d7e9168, update the other terminal cursor move accordingly to correctly restore the prompt.
|
Currently there is an empty line after the app output, which is removed with this change. But I'm not sure if this bottom 'padding' was intentional or not, since the I've marked as ready for review - I'm annoyed that I spent so long debugging this for what might be a simple 4 line change! |
It's not the total number of lines that counts, but knowing which 4 lines to change! |
Fix garbled inline app output when
inline_no_clear=True.Currently the terminal cursor isn't actually "reset" before printing the screen contents. This doesn't matter for most inline apps as the cursor is already offset at (0, 0). But if the app contains an
Inputwidget, this will update the cursor position so the output will be garbled.Fixes #6064
Please review the following checklist.