cpr.py 786 B

123456789101112131415161718192021222324252627282930
  1. from __future__ import annotations
  2. from prompt_toolkit.key_binding.key_processor import KeyPressEvent
  3. from prompt_toolkit.keys import Keys
  4. from ..key_bindings import KeyBindings
  5. __all__ = [
  6. "load_cpr_bindings",
  7. ]
  8. E = KeyPressEvent
  9. def load_cpr_bindings() -> KeyBindings:
  10. key_bindings = KeyBindings()
  11. @key_bindings.add(Keys.CPRResponse, save_before=lambda e: False)
  12. def _(event: E) -> None:
  13. """
  14. Handle incoming Cursor-Position-Request response.
  15. """
  16. # The incoming data looks like u'\x1b[35;1R'
  17. # Parse row/col information.
  18. row, col = map(int, event.data[2:-1].split(";"))
  19. # Report absolute cursor position to the renderer.
  20. event.app.renderer.report_absolute_cursor_row(row)
  21. return key_bindings