cursor_shapes.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 in {
  55. InputMode.NAVIGATION,
  56. }:
  57. return CursorShape.BLOCK
  58. if application.vi_state.input_mode in {
  59. InputMode.INSERT,
  60. InputMode.INSERT_MULTIPLE,
  61. }:
  62. return CursorShape.BEAM
  63. if application.vi_state.input_mode in {
  64. InputMode.REPLACE,
  65. InputMode.REPLACE_SINGLE,
  66. }:
  67. return CursorShape.UNDERLINE
  68. elif application.editing_mode == EditingMode.EMACS:
  69. # like vi's INSERT
  70. return CursorShape.BEAM
  71. # Default
  72. return CursorShape.BLOCK
  73. class DynamicCursorShapeConfig(CursorShapeConfig):
  74. def __init__(
  75. self, get_cursor_shape_config: Callable[[], AnyCursorShapeConfig]
  76. ) -> None:
  77. self.get_cursor_shape_config = get_cursor_shape_config
  78. def get_cursor_shape(self, application: Application[Any]) -> CursorShape:
  79. return to_cursor_shape_config(self.get_cursor_shape_config()).get_cursor_shape(
  80. application
  81. )
  82. def to_cursor_shape_config(value: AnyCursorShapeConfig) -> CursorShapeConfig:
  83. """
  84. Take a `CursorShape` instance or `CursorShapeConfig` and turn it into a
  85. `CursorShapeConfig`.
  86. """
  87. if value is None:
  88. return SimpleCursorShapeConfig()
  89. if isinstance(value, CursorShape):
  90. return SimpleCursorShapeConfig(value)
  91. return value