output.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. """
  2. Interface for an output.
  3. """
  4. from __future__ import unicode_literals
  5. from abc import ABCMeta, abstractmethod
  6. from six import with_metaclass
  7. from prompt_toolkit.layout.screen import Size
  8. __all__ = (
  9. 'Output',
  10. )
  11. class Output(with_metaclass(ABCMeta, object)):
  12. """
  13. Base class defining the output interface for a
  14. :class:`~prompt_toolkit.renderer.Renderer`.
  15. Actual implementations are
  16. :class:`~prompt_toolkit.terminal.vt100_output.Vt100_Output` and
  17. :class:`~prompt_toolkit.terminal.win32_output.Win32Output`.
  18. """
  19. @abstractmethod
  20. def fileno(self):
  21. " Return the file descriptor to which we can write for the output. "
  22. @abstractmethod
  23. def encoding(self):
  24. """
  25. Return the encoding for this output, e.g. 'utf-8'.
  26. (This is used mainly to know which characters are supported by the
  27. output the data, so that the UI can provide alternatives, when
  28. required.)
  29. """
  30. @abstractmethod
  31. def write(self, data):
  32. " Write text (Terminal escape sequences will be removed/escaped.) "
  33. @abstractmethod
  34. def write_raw(self, data):
  35. " Write text. "
  36. @abstractmethod
  37. def set_title(self, title):
  38. " Set terminal title. "
  39. @abstractmethod
  40. def clear_title(self):
  41. " Clear title again. (or restore previous title.) "
  42. @abstractmethod
  43. def flush(self):
  44. " Write to output stream and flush. "
  45. @abstractmethod
  46. def erase_screen(self):
  47. """
  48. Erases the screen with the background colour and moves the cursor to
  49. home.
  50. """
  51. @abstractmethod
  52. def enter_alternate_screen(self):
  53. " Go to the alternate screen buffer. (For full screen applications). "
  54. @abstractmethod
  55. def quit_alternate_screen(self):
  56. " Leave the alternate screen buffer. "
  57. @abstractmethod
  58. def enable_mouse_support(self):
  59. " Enable mouse. "
  60. @abstractmethod
  61. def disable_mouse_support(self):
  62. " Disable mouse. "
  63. @abstractmethod
  64. def erase_end_of_line(self):
  65. """
  66. Erases from the current cursor position to the end of the current line.
  67. """
  68. @abstractmethod
  69. def erase_down(self):
  70. """
  71. Erases the screen from the current line down to the bottom of the
  72. screen.
  73. """
  74. @abstractmethod
  75. def reset_attributes(self):
  76. " Reset color and styling attributes. "
  77. @abstractmethod
  78. def set_attributes(self, attrs):
  79. " Set new color and styling attributes. "
  80. @abstractmethod
  81. def disable_autowrap(self):
  82. " Disable auto line wrapping. "
  83. @abstractmethod
  84. def enable_autowrap(self):
  85. " Enable auto line wrapping. "
  86. @abstractmethod
  87. def cursor_goto(self, row=0, column=0):
  88. " Move cursor position. "
  89. @abstractmethod
  90. def cursor_up(self, amount):
  91. " Move cursor `amount` place up. "
  92. @abstractmethod
  93. def cursor_down(self, amount):
  94. " Move cursor `amount` place down. "
  95. @abstractmethod
  96. def cursor_forward(self, amount):
  97. " Move cursor `amount` place forward. "
  98. @abstractmethod
  99. def cursor_backward(self, amount):
  100. " Move cursor `amount` place backward. "
  101. @abstractmethod
  102. def hide_cursor(self):
  103. " Hide cursor. "
  104. @abstractmethod
  105. def show_cursor(self):
  106. " Show cursor. "
  107. def ask_for_cpr(self):
  108. """
  109. Asks for a cursor position report (CPR).
  110. (VT100 only.)
  111. """
  112. def bell(self):
  113. " Sound bell. "
  114. def enable_bracketed_paste(self):
  115. " For vt100 only. "
  116. def disable_bracketed_paste(self):
  117. " For vt100 only. "
  118. class DummyOutput(Output):
  119. """
  120. For testing. An output class that doesn't render anything.
  121. """
  122. def fileno(self):
  123. " There is no sensible default for fileno(). "
  124. raise NotImplementedError
  125. def encoding(self):
  126. return 'utf-8'
  127. def write(self, data): pass
  128. def write_raw(self, data): pass
  129. def set_title(self, title): pass
  130. def clear_title(self): pass
  131. def flush(self): pass
  132. def erase_screen(self): pass
  133. def enter_alternate_screen(self): pass
  134. def quit_alternate_screen(self): pass
  135. def enable_mouse_support(self): pass
  136. def disable_mouse_support(self): pass
  137. def erase_end_of_line(self): pass
  138. def erase_down(self): pass
  139. def reset_attributes(self): pass
  140. def set_attributes(self, attrs): pass
  141. def disable_autowrap(self): pass
  142. def enable_autowrap(self): pass
  143. def cursor_goto(self, row=0, column=0): pass
  144. def cursor_up(self, amount): pass
  145. def cursor_down(self, amount): pass
  146. def cursor_forward(self, amount): pass
  147. def cursor_backward(self, amount): pass
  148. def hide_cursor(self): pass
  149. def show_cursor(self): pass
  150. def ask_for_cpr(self): pass
  151. def bell(self): pass
  152. def enable_bracketed_paste(self): pass
  153. def disable_bracketed_paste(self): pass
  154. def get_size(self):
  155. return Size(rows=40, columns=80)