Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/prompt_toolkit/output/base.py: 75%

150 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-04-20 06:09 +0000

1""" 

2Interface for an output. 

3""" 

4from __future__ import annotations 

5 

6from abc import ABCMeta, abstractmethod 

7from typing import TextIO 

8 

9from prompt_toolkit.cursor_shapes import CursorShape 

10from prompt_toolkit.data_structures import Size 

11from prompt_toolkit.styles import Attrs 

12 

13from .color_depth import ColorDepth 

14 

15__all__ = [ 

16 "Output", 

17 "DummyOutput", 

18] 

19 

20 

21class Output(metaclass=ABCMeta): 

22 """ 

23 Base class defining the output interface for a 

24 :class:`~prompt_toolkit.renderer.Renderer`. 

25 

26 Actual implementations are 

27 :class:`~prompt_toolkit.output.vt100.Vt100_Output` and 

28 :class:`~prompt_toolkit.output.win32.Win32Output`. 

29 """ 

30 

31 stdout: TextIO | None = None 

32 

33 @abstractmethod 

34 def fileno(self) -> int: 

35 "Return the file descriptor to which we can write for the output." 

36 

37 @abstractmethod 

38 def encoding(self) -> str: 

39 """ 

40 Return the encoding for this output, e.g. 'utf-8'. 

41 (This is used mainly to know which characters are supported by the 

42 output the data, so that the UI can provide alternatives, when 

43 required.) 

44 """ 

45 

46 @abstractmethod 

47 def write(self, data: str) -> None: 

48 "Write text (Terminal escape sequences will be removed/escaped.)" 

49 

50 @abstractmethod 

51 def write_raw(self, data: str) -> None: 

52 "Write text." 

53 

54 @abstractmethod 

55 def set_title(self, title: str) -> None: 

56 "Set terminal title." 

57 

58 @abstractmethod 

59 def clear_title(self) -> None: 

60 "Clear title again. (or restore previous title.)" 

61 

62 @abstractmethod 

63 def flush(self) -> None: 

64 "Write to output stream and flush." 

65 

66 @abstractmethod 

67 def erase_screen(self) -> None: 

68 """ 

69 Erases the screen with the background color and moves the cursor to 

70 home. 

71 """ 

72 

73 @abstractmethod 

74 def enter_alternate_screen(self) -> None: 

75 "Go to the alternate screen buffer. (For full screen applications)." 

76 

77 @abstractmethod 

78 def quit_alternate_screen(self) -> None: 

79 "Leave the alternate screen buffer." 

80 

81 @abstractmethod 

82 def enable_mouse_support(self) -> None: 

83 "Enable mouse." 

84 

85 @abstractmethod 

86 def disable_mouse_support(self) -> None: 

87 "Disable mouse." 

88 

89 @abstractmethod 

90 def erase_end_of_line(self) -> None: 

91 """ 

92 Erases from the current cursor position to the end of the current line. 

93 """ 

94 

95 @abstractmethod 

96 def erase_down(self) -> None: 

97 """ 

98 Erases the screen from the current line down to the bottom of the 

99 screen. 

100 """ 

101 

102 @abstractmethod 

103 def reset_attributes(self) -> None: 

104 "Reset color and styling attributes." 

105 

106 @abstractmethod 

107 def set_attributes(self, attrs: Attrs, color_depth: ColorDepth) -> None: 

108 "Set new color and styling attributes." 

109 

110 @abstractmethod 

111 def disable_autowrap(self) -> None: 

112 "Disable auto line wrapping." 

113 

114 @abstractmethod 

115 def enable_autowrap(self) -> None: 

116 "Enable auto line wrapping." 

117 

118 @abstractmethod 

119 def cursor_goto(self, row: int = 0, column: int = 0) -> None: 

120 "Move cursor position." 

121 

122 @abstractmethod 

123 def cursor_up(self, amount: int) -> None: 

124 "Move cursor `amount` place up." 

125 

126 @abstractmethod 

127 def cursor_down(self, amount: int) -> None: 

128 "Move cursor `amount` place down." 

129 

130 @abstractmethod 

131 def cursor_forward(self, amount: int) -> None: 

132 "Move cursor `amount` place forward." 

133 

134 @abstractmethod 

135 def cursor_backward(self, amount: int) -> None: 

136 "Move cursor `amount` place backward." 

137 

138 @abstractmethod 

139 def hide_cursor(self) -> None: 

140 "Hide cursor." 

141 

142 @abstractmethod 

143 def show_cursor(self) -> None: 

144 "Show cursor." 

145 

146 @abstractmethod 

147 def set_cursor_shape(self, cursor_shape: CursorShape) -> None: 

148 "Set cursor shape to block, beam or underline." 

149 

150 @abstractmethod 

151 def reset_cursor_shape(self) -> None: 

152 "Reset cursor shape." 

153 

154 def ask_for_cpr(self) -> None: 

155 """ 

156 Asks for a cursor position report (CPR). 

157 (VT100 only.) 

158 """ 

159 

160 @property 

161 def responds_to_cpr(self) -> bool: 

162 """ 

163 `True` if the `Application` can expect to receive a CPR response after 

164 calling `ask_for_cpr` (this will come back through the corresponding 

165 `Input`). 

166 

167 This is used to determine the amount of available rows we have below 

168 the cursor position. In the first place, we have this so that the drop 

169 down autocompletion menus are sized according to the available space. 

170 

171 On Windows, we don't need this, there we have 

172 `get_rows_below_cursor_position`. 

173 """ 

174 return False 

175 

176 @abstractmethod 

177 def get_size(self) -> Size: 

178 "Return the size of the output window." 

179 

180 def bell(self) -> None: 

181 "Sound bell." 

182 

183 def enable_bracketed_paste(self) -> None: 

184 "For vt100 only." 

185 

186 def disable_bracketed_paste(self) -> None: 

187 "For vt100 only." 

188 

189 def reset_cursor_key_mode(self) -> None: 

190 """ 

191 For vt100 only. 

192 Put the terminal in normal cursor mode (instead of application mode). 

193 

194 See: https://vt100.net/docs/vt100-ug/chapter3.html 

195 """ 

196 

197 def scroll_buffer_to_prompt(self) -> None: 

198 "For Win32 only." 

199 

200 def get_rows_below_cursor_position(self) -> int: 

201 "For Windows only." 

202 raise NotImplementedError 

203 

204 @abstractmethod 

205 def get_default_color_depth(self) -> ColorDepth: 

206 """ 

207 Get default color depth for this output. 

208 

209 This value will be used if no color depth was explicitly passed to the 

210 `Application`. 

211 

212 .. note:: 

213 

214 If the `$PROMPT_TOOLKIT_COLOR_DEPTH` environment variable has been 

215 set, then `outputs.defaults.create_output` will pass this value to 

216 the implementation as the default_color_depth, which is returned 

217 here. (This is not used when the output corresponds to a 

218 prompt_toolkit SSH/Telnet session.) 

219 """ 

220 

221 

222class DummyOutput(Output): 

223 """ 

224 For testing. An output class that doesn't render anything. 

225 """ 

226 

227 def fileno(self) -> int: 

228 "There is no sensible default for fileno()." 

229 raise NotImplementedError 

230 

231 def encoding(self) -> str: 

232 return "utf-8" 

233 

234 def write(self, data: str) -> None: 

235 pass 

236 

237 def write_raw(self, data: str) -> None: 

238 pass 

239 

240 def set_title(self, title: str) -> None: 

241 pass 

242 

243 def clear_title(self) -> None: 

244 pass 

245 

246 def flush(self) -> None: 

247 pass 

248 

249 def erase_screen(self) -> None: 

250 pass 

251 

252 def enter_alternate_screen(self) -> None: 

253 pass 

254 

255 def quit_alternate_screen(self) -> None: 

256 pass 

257 

258 def enable_mouse_support(self) -> None: 

259 pass 

260 

261 def disable_mouse_support(self) -> None: 

262 pass 

263 

264 def erase_end_of_line(self) -> None: 

265 pass 

266 

267 def erase_down(self) -> None: 

268 pass 

269 

270 def reset_attributes(self) -> None: 

271 pass 

272 

273 def set_attributes(self, attrs: Attrs, color_depth: ColorDepth) -> None: 

274 pass 

275 

276 def disable_autowrap(self) -> None: 

277 pass 

278 

279 def enable_autowrap(self) -> None: 

280 pass 

281 

282 def cursor_goto(self, row: int = 0, column: int = 0) -> None: 

283 pass 

284 

285 def cursor_up(self, amount: int) -> None: 

286 pass 

287 

288 def cursor_down(self, amount: int) -> None: 

289 pass 

290 

291 def cursor_forward(self, amount: int) -> None: 

292 pass 

293 

294 def cursor_backward(self, amount: int) -> None: 

295 pass 

296 

297 def hide_cursor(self) -> None: 

298 pass 

299 

300 def show_cursor(self) -> None: 

301 pass 

302 

303 def set_cursor_shape(self, cursor_shape: CursorShape) -> None: 

304 pass 

305 

306 def reset_cursor_shape(self) -> None: 

307 pass 

308 

309 def ask_for_cpr(self) -> None: 

310 pass 

311 

312 def bell(self) -> None: 

313 pass 

314 

315 def enable_bracketed_paste(self) -> None: 

316 pass 

317 

318 def disable_bracketed_paste(self) -> None: 

319 pass 

320 

321 def scroll_buffer_to_prompt(self) -> None: 

322 pass 

323 

324 def get_size(self) -> Size: 

325 return Size(rows=40, columns=80) 

326 

327 def get_rows_below_cursor_position(self) -> int: 

328 return 40 

329 

330 def get_default_color_depth(self) -> ColorDepth: 

331 return ColorDepth.DEPTH_1_BIT