plain_text.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. from __future__ import annotations
  2. from typing import TextIO
  3. from prompt_toolkit.cursor_shapes import CursorShape
  4. from prompt_toolkit.data_structures import Size
  5. from prompt_toolkit.styles import Attrs
  6. from .base import Output
  7. from .color_depth import ColorDepth
  8. from .flush_stdout import flush_stdout
  9. __all__ = ["PlainTextOutput"]
  10. class PlainTextOutput(Output):
  11. """
  12. Output that won't include any ANSI escape sequences.
  13. Useful when stdout is not a terminal. Maybe stdout is redirected to a file.
  14. In this case, if `print_formatted_text` is used, for instance, we don't
  15. want to include formatting.
  16. (The code is mostly identical to `Vt100_Output`, but without the
  17. formatting.)
  18. """
  19. def __init__(self, stdout: TextIO) -> None:
  20. assert all(hasattr(stdout, a) for a in ("write", "flush"))
  21. self.stdout: TextIO = stdout
  22. self._buffer: list[str] = []
  23. def fileno(self) -> int:
  24. "There is no sensible default for fileno()."
  25. return self.stdout.fileno()
  26. def encoding(self) -> str:
  27. return "utf-8"
  28. def write(self, data: str) -> None:
  29. self._buffer.append(data)
  30. def write_raw(self, data: str) -> None:
  31. self._buffer.append(data)
  32. def set_title(self, title: str) -> None:
  33. pass
  34. def clear_title(self) -> None:
  35. pass
  36. def flush(self) -> None:
  37. if not self._buffer:
  38. return
  39. data = "".join(self._buffer)
  40. self._buffer = []
  41. flush_stdout(self.stdout, data)
  42. def erase_screen(self) -> None:
  43. pass
  44. def enter_alternate_screen(self) -> None:
  45. pass
  46. def quit_alternate_screen(self) -> None:
  47. pass
  48. def enable_mouse_support(self) -> None:
  49. pass
  50. def disable_mouse_support(self) -> None:
  51. pass
  52. def erase_end_of_line(self) -> None:
  53. pass
  54. def erase_down(self) -> None:
  55. pass
  56. def reset_attributes(self) -> None:
  57. pass
  58. def set_attributes(self, attrs: Attrs, color_depth: ColorDepth) -> None:
  59. pass
  60. def disable_autowrap(self) -> None:
  61. pass
  62. def enable_autowrap(self) -> None:
  63. pass
  64. def cursor_goto(self, row: int = 0, column: int = 0) -> None:
  65. pass
  66. def cursor_up(self, amount: int) -> None:
  67. pass
  68. def cursor_down(self, amount: int) -> None:
  69. self._buffer.append("\n")
  70. def cursor_forward(self, amount: int) -> None:
  71. self._buffer.append(" " * amount)
  72. def cursor_backward(self, amount: int) -> None:
  73. pass
  74. def hide_cursor(self) -> None:
  75. pass
  76. def show_cursor(self) -> None:
  77. pass
  78. def set_cursor_shape(self, cursor_shape: CursorShape) -> None:
  79. pass
  80. def reset_cursor_shape(self) -> None:
  81. pass
  82. def ask_for_cpr(self) -> None:
  83. pass
  84. def bell(self) -> None:
  85. pass
  86. def enable_bracketed_paste(self) -> None:
  87. pass
  88. def disable_bracketed_paste(self) -> None:
  89. pass
  90. def scroll_buffer_to_prompt(self) -> None:
  91. pass
  92. def get_size(self) -> Size:
  93. return Size(rows=40, columns=80)
  94. def get_rows_below_cursor_position(self) -> int:
  95. return 8
  96. def get_default_color_depth(self) -> ColorDepth:
  97. return ColorDepth.DEPTH_1_BIT