cli_enter_burst.py 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. """Enter arriving inside a fast input burst: pasted newline or scripted submit.
  2. Paste without bracketed paste (tmux ``paste-buffer`` without ``-p``) and IME/voice dictation
  3. deliver every newline as its own Enter key right behind the text, so an Enter within
  4. ``RAPID_INPUT_ENTER_WINDOW_S`` of the last buffer change is a line break, not a submit (#10994).
  5. A script typing ``text`` + Enter in ONE write (``tmux send-keys 'text' Enter``, a PTY write of
  6. ``text\\r``) looks identical up to that Enter, except nothing follows it and no other Enter came
  7. before it. So an Enter with more keys of the same read still queued stays a newline, and so does
  8. the last Enter of a burst that already held one (the tail of a multi-line paste: input never went
  9. quiet for ``BURST_END_QUIET_S`` since that earlier Enter, however long the paste ran). The lone Enter
  10. that ends a burst inserts a provisional newline and submits once input has stayed quiet for
  11. ``BURST_END_QUIET_S``; any input in that window (the next chunk of a paste split across reads)
  12. keeps the newline.
  13. """
  14. from __future__ import annotations
  15. from time import monotonic
  16. RAPID_INPUT_ENTER_WINDOW_S = 0.05
  17. # Long enough for the next read of a paste split across reads (slow SSH links), short enough
  18. # that a scripted submit still feels immediate. Humans never reach this path: their Enter comes
  19. # more than RAPID_INPUT_ENTER_WINDOW_S after the last keystroke.
  20. BURST_END_QUIET_S = 0.25
  21. def note_text_change(cli) -> float:
  22. """Record a buffer change; input arriving inside the quiet window keeps a paste burst alive.
  23. So does input arriving while an Enter is held, however late a stalled loop reads it: it makes
  24. that Enter a pasted newline, so the paste's own trailing Enter stays one too.
  25. """
  26. now = monotonic()
  27. held = getattr(cli, "_tui_held_enter", None) is not None
  28. if held or now - getattr(cli, "_tui_paste_burst_at", float("-inf")) < BURST_END_QUIET_S:
  29. cli._tui_paste_burst_at = now
  30. cli._tui_held_enter = None
  31. return now
  32. def handle_burst_enter(cli, event, buf) -> bool:
  33. """Handle an Enter that arrived mid-burst; False when it is an ordinary (human) Enter.
  34. ``cli._tui_submit_buffer(event, buf)`` is called later when this Enter turns out to end the
  35. burst.
  36. """
  37. now = monotonic()
  38. if now - getattr(cli, "_tui_last_text_change", 0.0) >= RAPID_INPUT_ENTER_WINDOW_S:
  39. return False
  40. pasted_newline_before = now - getattr(cli, "_tui_paste_burst_at", float("-inf")) < BURST_END_QUIET_S
  41. cli._tui_paste_burst_at = now
  42. before = buf.document # the newline lands at the cursor, not necessarily at the end
  43. buf.insert_text("\n")
  44. app = event.app
  45. key_processor = getattr(app, "key_processor", None)
  46. loop = getattr(app, "loop", None)
  47. if key_processor is None or loop is None or key_processor.input_queue or pasted_newline_before:
  48. return True # inside (or the tail of) a multi-line paste
  49. with_newline = buf.document
  50. changed_at = getattr(cli, "_tui_last_text_change", 0.0)
  51. held = cli._tui_held_enter = object()
  52. def _settle() -> None:
  53. if cli._tui_held_enter is held:
  54. cli._tui_held_enter = None
  55. if not app.is_running or buf.document != with_newline or cli._tui_last_text_change != changed_at:
  56. return # more input arrived (or the buffer was edited): the newline stays
  57. buf.document = before
  58. cli._tui_submit_buffer(event, buf)
  59. app.invalidate()
  60. # Decide only after one more selector pass has run its reader callbacks: a loop stalled past
  61. # the window can reach this timer with the next chunk still unread (a signal handler that
  62. # interrupts the selector past its deadline returns no events). asyncio runs a call_soon
  63. # queued now ahead of the next pass's readers; the second hop runs after them.
  64. loop.call_later(BURST_END_QUIET_S, loop.call_soon, loop.call_soon, _settle)
  65. return True