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: 4 additions & 3 deletions sqlit/shared/ui/widgets_text_area.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
30 changes: 30 additions & 0 deletions tests/ui/keybindings/test_insert_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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."""
Expand Down