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