base.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. """
  2. Interface for an output.
  3. """
  4. from __future__ import annotations
  5. from abc import ABCMeta, abstractmethod
  6. from typing import TextIO
  7. from prompt_toolkit.cursor_shapes import CursorShape
  8. from prompt_toolkit.data_structures import Size
  9. from prompt_toolkit.styles import Attrs
  10. from .color_depth import ColorDepth
  11. __all__ = [
  12. "Output",
  13. "DummyOutput",
  14. ]
  15. class Output(metaclass=ABCMeta):
  16. """
  17. Base class defining the output interface for a
  18. :class:`~prompt_toolkit.renderer.Renderer`.
  19. Actual implementations are
  20. :class:`~prompt_toolkit.output.vt100.Vt100_Output` and
  21. :class:`~prompt_toolkit.output.win32.Win32Output`.
  22. """
  23. stdout: TextIO | None = None
  24. @abstractmethod
  25. def fileno(self) -> int:
  26. "Return the file descriptor to which we can write for the output."
  27. @abstractmethod
  28. def encoding(self) -> str:
  29. """
  30. Return the encoding for this output, e.g. 'utf-8'.
  31. (This is used mainly to know which characters are supported by the
  32. output the data, so that the UI can provide alternatives, when
  33. required.)
  34. """
  35. @abstractmethod
  36. def write(self, data: str) -> None:
  37. "Write text (Terminal escape sequences will be removed/escaped.)"
  38. @abstractmethod
  39. def write_raw(self, data: str) -> None:
  40. "Write text."
  41. @abstractmethod
  42. def set_title(self, title: str) -> None:
  43. "Set terminal title."
  44. @abstractmethod
  45. def clear_title(self) -> None:
  46. "Clear title again. (or restore previous title.)"
  47. @abstractmethod
  48. def flush(self) -> None:
  49. "Write to output stream and flush."
  50. @abstractmethod
  51. def erase_screen(self) -> None:
  52. """
  53. Erases the screen with the background color and moves the cursor to
  54. home.
  55. """
  56. @abstractmethod
  57. def enter_alternate_screen(self) -> None:
  58. "Go to the alternate screen buffer. (For full screen applications)."
  59. @abstractmethod
  60. def quit_alternate_screen(self) -> None:
  61. "Leave the alternate screen buffer."
  62. @abstractmethod
  63. def enable_mouse_support(self) -> None:
  64. "Enable mouse."
  65. @abstractmethod
  66. def disable_mouse_support(self) -> None:
  67. "Disable mouse."
  68. @abstractmethod
  69. def erase_end_of_line(self) -> None:
  70. """
  71. Erases from the current cursor position to the end of the current line.
  72. """
  73. @abstractmethod
  74. def erase_down(self) -> None:
  75. """
  76. Erases the screen from the current line down to the bottom of the
  77. screen.
  78. """
  79. @abstractmethod
  80. def reset_attributes(self) -> None:
  81. "Reset color and styling attributes."
  82. @abstractmethod
  83. def set_attributes(self, attrs: Attrs, color_depth: ColorDepth) -> None:
  84. "Set new color and styling attributes."
  85. @abstractmethod
  86. def disable_autowrap(self) -> None:
  87. "Disable auto line wrapping."
  88. @abstractmethod
  89. def enable_autowrap(self) -> None:
  90. "Enable auto line wrapping."
  91. @abstractmethod
  92. def cursor_goto(self, row: int = 0, column: int = 0) -> None:
  93. "Move cursor position."
  94. @abstractmethod
  95. def cursor_up(self, amount: int) -> None:
  96. "Move cursor `amount` place up."
  97. @abstractmethod
  98. def cursor_down(self, amount: int) -> None:
  99. "Move cursor `amount` place down."
  100. @abstractmethod
  101. def cursor_forward(self, amount: int) -> None:
  102. "Move cursor `amount` place forward."
  103. @abstractmethod
  104. def cursor_backward(self, amount: int) -> None:
  105. "Move cursor `amount` place backward."
  106. @abstractmethod
  107. def hide_cursor(self) -> None:
  108. "Hide cursor."
  109. @abstractmethod
  110. def show_cursor(self) -> None:
  111. "Show cursor."
  112. @abstractmethod
  113. def set_cursor_shape(self, cursor_shape: CursorShape) -> None:
  114. "Set cursor shape to block, beam or underline."
  115. @abstractmethod
  116. def reset_cursor_shape(self) -> None:
  117. "Reset cursor shape."
  118. def ask_for_cpr(self) -> None:
  119. """
  120. Asks for a cursor position report (CPR).
  121. (VT100 only.)
  122. """
  123. @property
  124. def responds_to_cpr(self) -> bool:
  125. """
  126. `True` if the `Application` can expect to receive a CPR response after
  127. calling `ask_for_cpr` (this will come back through the corresponding
  128. `Input`).
  129. This is used to determine the amount of available rows we have below
  130. the cursor position. In the first place, we have this so that the drop
  131. down autocompletion menus are sized according to the available space.
  132. On Windows, we don't need this, there we have
  133. `get_rows_below_cursor_position`.
  134. """
  135. return False
  136. @abstractmethod
  137. def get_size(self) -> Size:
  138. "Return the size of the output window."
  139. def bell(self) -> None:
  140. "Sound bell."
  141. def enable_bracketed_paste(self) -> None:
  142. "For vt100 only."
  143. def disable_bracketed_paste(self) -> None:
  144. "For vt100 only."
  145. def reset_cursor_key_mode(self) -> None:
  146. """
  147. For vt100 only.
  148. Put the terminal in normal cursor mode (instead of application mode).
  149. See: https://vt100.net/docs/vt100-ug/chapter3.html
  150. """
  151. def scroll_buffer_to_prompt(self) -> None:
  152. "For Win32 only."
  153. def get_rows_below_cursor_position(self) -> int:
  154. "For Windows only."
  155. raise NotImplementedError
  156. @abstractmethod
  157. def get_default_color_depth(self) -> ColorDepth:
  158. """
  159. Get default color depth for this output.
  160. This value will be used if no color depth was explicitly passed to the
  161. `Application`.
  162. .. note::
  163. If the `$PROMPT_TOOLKIT_COLOR_DEPTH` environment variable has been
  164. set, then `outputs.defaults.create_output` will pass this value to
  165. the implementation as the default_color_depth, which is returned
  166. here. (This is not used when the output corresponds to a
  167. prompt_toolkit SSH/Telnet session.)
  168. """
  169. class DummyOutput(Output):
  170. """
  171. For testing. An output class that doesn't render anything.
  172. """
  173. def fileno(self) -> int:
  174. "There is no sensible default for fileno()."
  175. raise NotImplementedError
  176. def encoding(self) -> str:
  177. return "utf-8"
  178. def write(self, data: str) -> None:
  179. pass
  180. def write_raw(self, data: str) -> None:
  181. pass
  182. def set_title(self, title: str) -> None:
  183. pass
  184. def clear_title(self) -> None:
  185. pass
  186. def flush(self) -> None:
  187. pass
  188. def erase_screen(self) -> None:
  189. pass
  190. def enter_alternate_screen(self) -> None:
  191. pass
  192. def quit_alternate_screen(self) -> None:
  193. pass
  194. def enable_mouse_support(self) -> None:
  195. pass
  196. def disable_mouse_support(self) -> None:
  197. pass
  198. def erase_end_of_line(self) -> None:
  199. pass
  200. def erase_down(self) -> None:
  201. pass
  202. def reset_attributes(self) -> None:
  203. pass
  204. def set_attributes(self, attrs: Attrs, color_depth: ColorDepth) -> None:
  205. pass
  206. def disable_autowrap(self) -> None:
  207. pass
  208. def enable_autowrap(self) -> None:
  209. pass
  210. def cursor_goto(self, row: int = 0, column: int = 0) -> None:
  211. pass
  212. def cursor_up(self, amount: int) -> None:
  213. pass
  214. def cursor_down(self, amount: int) -> None:
  215. pass
  216. def cursor_forward(self, amount: int) -> None:
  217. pass
  218. def cursor_backward(self, amount: int) -> None:
  219. pass
  220. def hide_cursor(self) -> None:
  221. pass
  222. def show_cursor(self) -> None:
  223. pass
  224. def set_cursor_shape(self, cursor_shape: CursorShape) -> None:
  225. pass
  226. def reset_cursor_shape(self) -> None:
  227. pass
  228. def ask_for_cpr(self) -> None:
  229. pass
  230. def bell(self) -> None:
  231. pass
  232. def enable_bracketed_paste(self) -> None:
  233. pass
  234. def disable_bracketed_paste(self) -> None:
  235. pass
  236. def scroll_buffer_to_prompt(self) -> None:
  237. pass
  238. def get_size(self) -> Size:
  239. return Size(rows=40, columns=80)
  240. def get_rows_below_cursor_position(self) -> int:
  241. return 40
  242. def get_default_color_depth(self) -> ColorDepth:
  243. return ColorDepth.DEPTH_1_BIT