diff --git a/sqlit/shared/ui/widgets_text_area.py b/sqlit/shared/ui/widgets_text_area.py index b3d5ffba..cb516291 100644 --- a/sqlit/shared/ui/widgets_text_area.py +++ b/sqlit/shared/ui/widgets_text_area.py @@ -74,8 +74,8 @@ def _is_insert_mode(self) -> bool: return vim_mode == VimMode.INSERT def _should_use_terminal_cursor(self) -> bool: - """Use a terminal bar cursor only in INSERT mode with focus.""" - return self.has_focus and self._is_insert_mode() + """Use the bar cursor only when Textual can paint a frame atomically.""" + return self.has_focus and self._is_insert_mode() and getattr(self.app, "_sync_available", False) def _get_insert_cursor_color(self) -> str: from sqlit.domains.shell.app.themes import DEFAULT_MODE_COLORS, MODE_NORMAL_COLOR_VAR @@ -131,7 +131,8 @@ def sync_terminal_cursor(self) -> None: @property def _draw_cursor(self) -> bool: - if self._should_use_terminal_cursor(): + # A capability reply can arrive before the terminal cursor is enabled. + if self._terminal_cursor_active: return False return super()._draw_cursor diff --git a/tests/ui/keybindings/test_insert_mode.py b/tests/ui/keybindings/test_insert_mode.py index edcb0be7..b1411777 100644 --- a/tests/ui/keybindings/test_insert_mode.py +++ b/tests/ui/keybindings/test_insert_mode.py @@ -3,6 +3,7 @@ from __future__ import annotations import pytest +from textual.messages import TerminalSupportsSynchronizedOutput from sqlit.core.keymap import get_keymap from sqlit.core.vim import VimMode @@ -22,6 +23,35 @@ def _make_app() -> SSMSTUI: class TestInsertModeKeybindings: """Test keybindings in vim INSERT mode.""" + @pytest.mark.asyncio + @pytest.mark.parametrize("synchronized", [False, True]) + async def test_insert_cursor_during_typing(self, synchronized: bool, monkeypatch: pytest.MonkeyPatch) -> None: + app = _make_app() + async with app.run_test() as pilot: + app.action_focus_query() + app.query_input.cursor_blink = False + if synchronized: + app.post_message(TerminalSupportsSynchronizedOutput()) + await pilot.pause() + + writes: list[str] = [] + monkeypatch.setattr(app._driver, "write", writes.append) + await pilot.press("i", "x") + assert app.query_input.text == "x" + assert ("\x1b[?25h" in "".join(writes)) == synchronized + assert app.query_input._draw_cursor == (not synchronized) + + if not synchronized: + # A late terminal reply must not hide the cursor between frames. + app.post_message(TerminalSupportsSynchronizedOutput()) + await pilot.pause() + assert app.query_input._draw_cursor + + writes.clear() + await pilot.press("escape") + assert ("\x1b[?25l" in "".join(writes)) == synchronized + assert app.query_input._draw_cursor + @pytest.mark.asyncio async def test_enter_insert_mode_key(self): """Enter insert mode key should enter INSERT mode when in query normal mode."""