cursor_shapes.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. from __future__ import annotations
  2. from abc import ABC, abstractmethod
  3. from enum import Enum
  4. from typing import TYPE_CHECKING, Any, Callable, Union
  5. from prompt_toolkit.enums import EditingMode
  6. from prompt_toolkit.key_binding.vi_state import InputMode
  7. if TYPE_CHECKING:
  8. from .application import Application
  9. __all__ = [
  10. "CursorShape",
  11. "CursorShapeConfig",
  12. "SimpleCursorShapeConfig",
  13. "ModalCursorShapeConfig",
  14. "DynamicCursorShapeConfig",
  15. "to_cursor_shape_config",
  16. ]
  17. class CursorShape(Enum):
  18. # Default value that should tell the output implementation to never send
  19. # cursor shape escape sequences. This is the default right now, because
  20. # before this `CursorShape` functionality was introduced into
  21. # prompt_toolkit itself, people had workarounds to send cursor shapes
  22. # escapes into the terminal, by monkey patching some of prompt_toolkit's
  23. # internals. We don't want the default prompt_toolkit implementation to
  24. # interfere with that. E.g., IPython patches the `ViState.input_mode`
  25. # property. See: https://github.com/ipython/ipython/pull/13501/files
  26. _NEVER_CHANGE = "_NEVER_CHANGE"
  27. BLOCK = "BLOCK"
  28. BEAM = "BEAM"
  29. UNDERLINE = "UNDERLINE"
  30. BLINKING_BLOCK = "BLINKING_BLOCK"
  31. BLINKING_BEAM = "BLINKING_BEAM"
  32. BLINKING_UNDERLINE = "BLINKING_UNDERLINE"
  33. class CursorShapeConfig(ABC):
  34. @abstractmethod
  35. def get_cursor_shape(self, application: Application[Any]) -> CursorShape:
  36. """
  37. Return the cursor shape to be used in the current state.
  38. """
  39. AnyCursorShapeConfig = Union[CursorShape, CursorShapeConfig, None]
  40. class SimpleCursorShapeConfig(CursorShapeConfig):
  41. """
  42. Always show the given cursor shape.
  43. """
  44. def __init__(self, cursor_shape: CursorShape = CursorShape._NEVER_CHANGE) -> None:
  45. self.cursor_shape = cursor_shape
  46. def get_cursor_shape(self, application: Application[Any]) -> CursorShape:
  47. return self.cursor_shape
  48. class ModalCursorShapeConfig(CursorShapeConfig):
  49. """
  50. Show cursor shape according to the current input mode.
  51. """
  52. def get_cursor_shape(self, application: Application[Any]) -> CursorShape:
  53. if application.editing_mode == EditingMode.VI:
  54. if application.vi_state.input_mode == InputMode.INSERT:
  55. return CursorShape.BEAM
  56. if application.vi_state.input_mode == InputMode.REPLACE:
  57. return CursorShape.UNDERLINE
  58. # Default
  59. return CursorShape.BLOCK
  60. class DynamicCursorShapeConfig(CursorShapeConfig):
  61. def __init__(
  62. self, get_cursor_shape_config: Callable[[], AnyCursorShapeConfig]
  63. ) -> None:
  64. self.get_cursor_shape_config = get_cursor_shape_config
  65. def get_cursor_shape(self, application: Application[Any]) -> CursorShape:
  66. return to_cursor_shape_config(self.get_cursor_shape_config()).get_cursor_shape(
  67. application
  68. )
  69. def to_cursor_shape_config(value: AnyCursorShapeConfig) -> CursorShapeConfig:
  70. """
  71. Take a `CursorShape` instance or `CursorShapeConfig` and turn it into a
  72. `CursorShapeConfig`.
  73. """
  74. if value is None:
  75. return SimpleCursorShapeConfig()
  76. if isinstance(value, CursorShape):
  77. return SimpleCursorShapeConfig(value)
  78. return value