_win32_console.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. """Light wrapper around the Win32 Console API - this module should only be imported on Windows
  2. The API that this module wraps is documented at https://docs.microsoft.com/en-us/windows/console/console-functions
  3. """
  4. import ctypes
  5. import sys
  6. from typing import Any
  7. windll: Any = None
  8. if sys.platform == "win32":
  9. windll = ctypes.LibraryLoader(ctypes.WinDLL)
  10. else:
  11. raise ImportError(f"{__name__} can only be imported on Windows")
  12. import time
  13. from ctypes import Structure, byref, wintypes
  14. from typing import IO, NamedTuple, Type, cast
  15. from rich.color import ColorSystem
  16. from rich.style import Style
  17. STDOUT = -11
  18. ENABLE_VIRTUAL_TERMINAL_PROCESSING = 4
  19. COORD = wintypes._COORD
  20. class LegacyWindowsError(Exception):
  21. pass
  22. class WindowsCoordinates(NamedTuple):
  23. """Coordinates in the Windows Console API are (y, x), not (x, y).
  24. This class is intended to prevent that confusion.
  25. Rows and columns are indexed from 0.
  26. This class can be used in place of wintypes._COORD in arguments and argtypes.
  27. """
  28. row: int
  29. col: int
  30. @classmethod
  31. def from_param(cls, value: "WindowsCoordinates") -> COORD:
  32. """Converts a WindowsCoordinates into a wintypes _COORD structure.
  33. This classmethod is internally called by ctypes to perform the conversion.
  34. Args:
  35. value (WindowsCoordinates): The input coordinates to convert.
  36. Returns:
  37. wintypes._COORD: The converted coordinates struct.
  38. """
  39. return COORD(value.col, value.row)
  40. class CONSOLE_SCREEN_BUFFER_INFO(Structure):
  41. _fields_ = [
  42. ("dwSize", COORD),
  43. ("dwCursorPosition", COORD),
  44. ("wAttributes", wintypes.WORD),
  45. ("srWindow", wintypes.SMALL_RECT),
  46. ("dwMaximumWindowSize", COORD),
  47. ]
  48. class CONSOLE_CURSOR_INFO(ctypes.Structure):
  49. _fields_ = [("dwSize", wintypes.DWORD), ("bVisible", wintypes.BOOL)]
  50. _GetStdHandle = windll.kernel32.GetStdHandle
  51. _GetStdHandle.argtypes = [
  52. wintypes.DWORD,
  53. ]
  54. _GetStdHandle.restype = wintypes.HANDLE
  55. def GetStdHandle(handle: int = STDOUT) -> wintypes.HANDLE:
  56. """Retrieves a handle to the specified standard device (standard input, standard output, or standard error).
  57. Args:
  58. handle (int): Integer identifier for the handle. Defaults to -11 (stdout).
  59. Returns:
  60. wintypes.HANDLE: The handle
  61. """
  62. return cast(wintypes.HANDLE, _GetStdHandle(handle))
  63. _GetConsoleMode = windll.kernel32.GetConsoleMode
  64. _GetConsoleMode.argtypes = [wintypes.HANDLE, wintypes.LPDWORD]
  65. _GetConsoleMode.restype = wintypes.BOOL
  66. def GetConsoleMode(std_handle: wintypes.HANDLE) -> int:
  67. """Retrieves the current input mode of a console's input buffer
  68. or the current output mode of a console screen buffer.
  69. Args:
  70. std_handle (wintypes.HANDLE): A handle to the console input buffer or the console screen buffer.
  71. Raises:
  72. LegacyWindowsError: If any error occurs while calling the Windows console API.
  73. Returns:
  74. int: Value representing the current console mode as documented at
  75. https://docs.microsoft.com/en-us/windows/console/getconsolemode#parameters
  76. """
  77. console_mode = wintypes.DWORD()
  78. success = bool(_GetConsoleMode(std_handle, console_mode))
  79. if not success:
  80. raise LegacyWindowsError("Unable to get legacy Windows Console Mode")
  81. return console_mode.value
  82. _FillConsoleOutputCharacterW = windll.kernel32.FillConsoleOutputCharacterW
  83. _FillConsoleOutputCharacterW.argtypes = [
  84. wintypes.HANDLE,
  85. ctypes.c_char,
  86. wintypes.DWORD,
  87. cast(Type[COORD], WindowsCoordinates),
  88. ctypes.POINTER(wintypes.DWORD),
  89. ]
  90. _FillConsoleOutputCharacterW.restype = wintypes.BOOL
  91. def FillConsoleOutputCharacter(
  92. std_handle: wintypes.HANDLE,
  93. char: str,
  94. length: int,
  95. start: WindowsCoordinates,
  96. ) -> int:
  97. """Writes a character to the console screen buffer a specified number of times, beginning at the specified coordinates.
  98. Args:
  99. std_handle (wintypes.HANDLE): A handle to the console input buffer or the console screen buffer.
  100. char (str): The character to write. Must be a string of length 1.
  101. length (int): The number of times to write the character.
  102. start (WindowsCoordinates): The coordinates to start writing at.
  103. Returns:
  104. int: The number of characters written.
  105. """
  106. character = ctypes.c_char(char.encode())
  107. num_characters = wintypes.DWORD(length)
  108. num_written = wintypes.DWORD(0)
  109. _FillConsoleOutputCharacterW(
  110. std_handle,
  111. character,
  112. num_characters,
  113. start,
  114. byref(num_written),
  115. )
  116. return num_written.value
  117. _FillConsoleOutputAttribute = windll.kernel32.FillConsoleOutputAttribute
  118. _FillConsoleOutputAttribute.argtypes = [
  119. wintypes.HANDLE,
  120. wintypes.WORD,
  121. wintypes.DWORD,
  122. cast(Type[COORD], WindowsCoordinates),
  123. ctypes.POINTER(wintypes.DWORD),
  124. ]
  125. _FillConsoleOutputAttribute.restype = wintypes.BOOL
  126. def FillConsoleOutputAttribute(
  127. std_handle: wintypes.HANDLE,
  128. attributes: int,
  129. length: int,
  130. start: WindowsCoordinates,
  131. ) -> int:
  132. """Sets the character attributes for a specified number of character cells,
  133. beginning at the specified coordinates in a screen buffer.
  134. Args:
  135. std_handle (wintypes.HANDLE): A handle to the console input buffer or the console screen buffer.
  136. attributes (int): Integer value representing the foreground and background colours of the cells.
  137. length (int): The number of cells to set the output attribute of.
  138. start (WindowsCoordinates): The coordinates of the first cell whose attributes are to be set.
  139. Returns:
  140. int: The number of cells whose attributes were actually set.
  141. """
  142. num_cells = wintypes.DWORD(length)
  143. style_attrs = wintypes.WORD(attributes)
  144. num_written = wintypes.DWORD(0)
  145. _FillConsoleOutputAttribute(
  146. std_handle, style_attrs, num_cells, start, byref(num_written)
  147. )
  148. return num_written.value
  149. _SetConsoleTextAttribute = windll.kernel32.SetConsoleTextAttribute
  150. _SetConsoleTextAttribute.argtypes = [
  151. wintypes.HANDLE,
  152. wintypes.WORD,
  153. ]
  154. _SetConsoleTextAttribute.restype = wintypes.BOOL
  155. def SetConsoleTextAttribute(
  156. std_handle: wintypes.HANDLE, attributes: wintypes.WORD
  157. ) -> bool:
  158. """Set the colour attributes for all text written after this function is called.
  159. Args:
  160. std_handle (wintypes.HANDLE): A handle to the console input buffer or the console screen buffer.
  161. attributes (int): Integer value representing the foreground and background colours.
  162. Returns:
  163. bool: True if the attribute was set successfully, otherwise False.
  164. """
  165. return bool(_SetConsoleTextAttribute(std_handle, attributes))
  166. _GetConsoleScreenBufferInfo = windll.kernel32.GetConsoleScreenBufferInfo
  167. _GetConsoleScreenBufferInfo.argtypes = [
  168. wintypes.HANDLE,
  169. ctypes.POINTER(CONSOLE_SCREEN_BUFFER_INFO),
  170. ]
  171. _GetConsoleScreenBufferInfo.restype = wintypes.BOOL
  172. def GetConsoleScreenBufferInfo(
  173. std_handle: wintypes.HANDLE,
  174. ) -> CONSOLE_SCREEN_BUFFER_INFO:
  175. """Retrieves information about the specified console screen buffer.
  176. Args:
  177. std_handle (wintypes.HANDLE): A handle to the console input buffer or the console screen buffer.
  178. Returns:
  179. CONSOLE_SCREEN_BUFFER_INFO: A CONSOLE_SCREEN_BUFFER_INFO ctype struct contain information about
  180. screen size, cursor position, colour attributes, and more."""
  181. console_screen_buffer_info = CONSOLE_SCREEN_BUFFER_INFO()
  182. _GetConsoleScreenBufferInfo(std_handle, byref(console_screen_buffer_info))
  183. return console_screen_buffer_info
  184. _SetConsoleCursorPosition = windll.kernel32.SetConsoleCursorPosition
  185. _SetConsoleCursorPosition.argtypes = [
  186. wintypes.HANDLE,
  187. cast(Type[COORD], WindowsCoordinates),
  188. ]
  189. _SetConsoleCursorPosition.restype = wintypes.BOOL
  190. def SetConsoleCursorPosition(
  191. std_handle: wintypes.HANDLE, coords: WindowsCoordinates
  192. ) -> bool:
  193. """Set the position of the cursor in the console screen
  194. Args:
  195. std_handle (wintypes.HANDLE): A handle to the console input buffer or the console screen buffer.
  196. coords (WindowsCoordinates): The coordinates to move the cursor to.
  197. Returns:
  198. bool: True if the function succeeds, otherwise False.
  199. """
  200. return bool(_SetConsoleCursorPosition(std_handle, coords))
  201. _GetConsoleCursorInfo = windll.kernel32.GetConsoleCursorInfo
  202. _GetConsoleCursorInfo.argtypes = [
  203. wintypes.HANDLE,
  204. ctypes.POINTER(CONSOLE_CURSOR_INFO),
  205. ]
  206. _GetConsoleCursorInfo.restype = wintypes.BOOL
  207. def GetConsoleCursorInfo(
  208. std_handle: wintypes.HANDLE, cursor_info: CONSOLE_CURSOR_INFO
  209. ) -> bool:
  210. """Get the cursor info - used to get cursor visibility and width
  211. Args:
  212. std_handle (wintypes.HANDLE): A handle to the console input buffer or the console screen buffer.
  213. cursor_info (CONSOLE_CURSOR_INFO): CONSOLE_CURSOR_INFO ctype struct that receives information
  214. about the console's cursor.
  215. Returns:
  216. bool: True if the function succeeds, otherwise False.
  217. """
  218. return bool(_GetConsoleCursorInfo(std_handle, byref(cursor_info)))
  219. _SetConsoleCursorInfo = windll.kernel32.SetConsoleCursorInfo
  220. _SetConsoleCursorInfo.argtypes = [
  221. wintypes.HANDLE,
  222. ctypes.POINTER(CONSOLE_CURSOR_INFO),
  223. ]
  224. _SetConsoleCursorInfo.restype = wintypes.BOOL
  225. def SetConsoleCursorInfo(
  226. std_handle: wintypes.HANDLE, cursor_info: CONSOLE_CURSOR_INFO
  227. ) -> bool:
  228. """Set the cursor info - used for adjusting cursor visibility and width
  229. Args:
  230. std_handle (wintypes.HANDLE): A handle to the console input buffer or the console screen buffer.
  231. cursor_info (CONSOLE_CURSOR_INFO): CONSOLE_CURSOR_INFO ctype struct containing the new cursor info.
  232. Returns:
  233. bool: True if the function succeeds, otherwise False.
  234. """
  235. return bool(_SetConsoleCursorInfo(std_handle, byref(cursor_info)))
  236. _SetConsoleTitle = windll.kernel32.SetConsoleTitleW
  237. _SetConsoleTitle.argtypes = [wintypes.LPCWSTR]
  238. _SetConsoleTitle.restype = wintypes.BOOL
  239. def SetConsoleTitle(title: str) -> bool:
  240. """Sets the title of the current console window
  241. Args:
  242. title (str): The new title of the console window.
  243. Returns:
  244. bool: True if the function succeeds, otherwise False.
  245. """
  246. return bool(_SetConsoleTitle(title))
  247. class LegacyWindowsTerm:
  248. """This class allows interaction with the legacy Windows Console API. It should only be used in the context
  249. of environments where virtual terminal processing is not available. However, if it is used in a Windows environment,
  250. the entire API should work.
  251. Args:
  252. file (IO[str]): The file which the Windows Console API HANDLE is retrieved from, defaults to sys.stdout.
  253. """
  254. BRIGHT_BIT = 8
  255. # Indices are ANSI color numbers, values are the corresponding Windows Console API color numbers
  256. ANSI_TO_WINDOWS = [
  257. 0, # black The Windows colours are defined in wincon.h as follows:
  258. 4, # red define FOREGROUND_BLUE 0x0001 -- 0000 0001
  259. 2, # green define FOREGROUND_GREEN 0x0002 -- 0000 0010
  260. 6, # yellow define FOREGROUND_RED 0x0004 -- 0000 0100
  261. 1, # blue define FOREGROUND_INTENSITY 0x0008 -- 0000 1000
  262. 5, # magenta define BACKGROUND_BLUE 0x0010 -- 0001 0000
  263. 3, # cyan define BACKGROUND_GREEN 0x0020 -- 0010 0000
  264. 7, # white define BACKGROUND_RED 0x0040 -- 0100 0000
  265. 8, # bright black (grey) define BACKGROUND_INTENSITY 0x0080 -- 1000 0000
  266. 12, # bright red
  267. 10, # bright green
  268. 14, # bright yellow
  269. 9, # bright blue
  270. 13, # bright magenta
  271. 11, # bright cyan
  272. 15, # bright white
  273. ]
  274. def __init__(self, file: "IO[str]") -> None:
  275. handle = GetStdHandle(STDOUT)
  276. self._handle = handle
  277. default_text = GetConsoleScreenBufferInfo(handle).wAttributes
  278. self._default_text = default_text
  279. self._default_fore = default_text & 7
  280. self._default_back = (default_text >> 4) & 7
  281. self._default_attrs = self._default_fore | (self._default_back << 4)
  282. self._file = file
  283. self.write = file.write
  284. self.flush = file.flush
  285. @property
  286. def cursor_position(self) -> WindowsCoordinates:
  287. """Returns the current position of the cursor (0-based)
  288. Returns:
  289. WindowsCoordinates: The current cursor position.
  290. """
  291. coord: COORD = GetConsoleScreenBufferInfo(self._handle).dwCursorPosition
  292. return WindowsCoordinates(row=coord.Y, col=coord.X)
  293. @property
  294. def screen_size(self) -> WindowsCoordinates:
  295. """Returns the current size of the console screen buffer, in character columns and rows
  296. Returns:
  297. WindowsCoordinates: The width and height of the screen as WindowsCoordinates.
  298. """
  299. screen_size: COORD = GetConsoleScreenBufferInfo(self._handle).dwSize
  300. return WindowsCoordinates(row=screen_size.Y, col=screen_size.X)
  301. def write_text(self, text: str) -> None:
  302. """Write text directly to the terminal without any modification of styles
  303. Args:
  304. text (str): The text to write to the console
  305. """
  306. self.write(text)
  307. self.flush()
  308. def write_styled(self, text: str, style: Style) -> None:
  309. """Write styled text to the terminal.
  310. Args:
  311. text (str): The text to write
  312. style (Style): The style of the text
  313. """
  314. color = style.color
  315. bgcolor = style.bgcolor
  316. if style.reverse:
  317. color, bgcolor = bgcolor, color
  318. if color:
  319. fore = color.downgrade(ColorSystem.WINDOWS).number
  320. fore = fore if fore is not None else 7 # Default to ANSI 7: White
  321. if style.bold:
  322. fore = fore | self.BRIGHT_BIT
  323. if style.dim:
  324. fore = fore & ~self.BRIGHT_BIT
  325. fore = self.ANSI_TO_WINDOWS[fore]
  326. else:
  327. fore = self._default_fore
  328. if bgcolor:
  329. back = bgcolor.downgrade(ColorSystem.WINDOWS).number
  330. back = back if back is not None else 0 # Default to ANSI 0: Black
  331. back = self.ANSI_TO_WINDOWS[back]
  332. else:
  333. back = self._default_back
  334. assert fore is not None
  335. assert back is not None
  336. SetConsoleTextAttribute(
  337. self._handle, attributes=ctypes.c_ushort(fore | (back << 4))
  338. )
  339. self.write_text(text)
  340. SetConsoleTextAttribute(self._handle, attributes=self._default_text)
  341. def move_cursor_to(self, new_position: WindowsCoordinates) -> None:
  342. """Set the position of the cursor
  343. Args:
  344. new_position (WindowsCoordinates): The WindowsCoordinates representing the new position of the cursor.
  345. """
  346. if new_position.col < 0 or new_position.row < 0:
  347. return
  348. SetConsoleCursorPosition(self._handle, coords=new_position)
  349. def erase_line(self) -> None:
  350. """Erase all content on the line the cursor is currently located at"""
  351. screen_size = self.screen_size
  352. cursor_position = self.cursor_position
  353. cells_to_erase = screen_size.col
  354. start_coordinates = WindowsCoordinates(row=cursor_position.row, col=0)
  355. FillConsoleOutputCharacter(
  356. self._handle, " ", length=cells_to_erase, start=start_coordinates
  357. )
  358. FillConsoleOutputAttribute(
  359. self._handle,
  360. self._default_attrs,
  361. length=cells_to_erase,
  362. start=start_coordinates,
  363. )
  364. def erase_end_of_line(self) -> None:
  365. """Erase all content from the cursor position to the end of that line"""
  366. cursor_position = self.cursor_position
  367. cells_to_erase = self.screen_size.col - cursor_position.col
  368. FillConsoleOutputCharacter(
  369. self._handle, " ", length=cells_to_erase, start=cursor_position
  370. )
  371. FillConsoleOutputAttribute(
  372. self._handle,
  373. self._default_attrs,
  374. length=cells_to_erase,
  375. start=cursor_position,
  376. )
  377. def erase_start_of_line(self) -> None:
  378. """Erase all content from the cursor position to the start of that line"""
  379. row, col = self.cursor_position
  380. start = WindowsCoordinates(row, 0)
  381. FillConsoleOutputCharacter(self._handle, " ", length=col, start=start)
  382. FillConsoleOutputAttribute(
  383. self._handle, self._default_attrs, length=col, start=start
  384. )
  385. def move_cursor_up(self) -> None:
  386. """Move the cursor up a single cell"""
  387. cursor_position = self.cursor_position
  388. SetConsoleCursorPosition(
  389. self._handle,
  390. coords=WindowsCoordinates(
  391. row=cursor_position.row - 1, col=cursor_position.col
  392. ),
  393. )
  394. def move_cursor_down(self) -> None:
  395. """Move the cursor down a single cell"""
  396. cursor_position = self.cursor_position
  397. SetConsoleCursorPosition(
  398. self._handle,
  399. coords=WindowsCoordinates(
  400. row=cursor_position.row + 1,
  401. col=cursor_position.col,
  402. ),
  403. )
  404. def move_cursor_forward(self) -> None:
  405. """Move the cursor forward a single cell. Wrap to the next line if required."""
  406. row, col = self.cursor_position
  407. if col == self.screen_size.col - 1:
  408. row += 1
  409. col = 0
  410. else:
  411. col += 1
  412. SetConsoleCursorPosition(
  413. self._handle, coords=WindowsCoordinates(row=row, col=col)
  414. )
  415. def move_cursor_to_column(self, column: int) -> None:
  416. """Move cursor to the column specified by the zero-based column index, staying on the same row
  417. Args:
  418. column (int): The zero-based column index to move the cursor to.
  419. """
  420. row, _ = self.cursor_position
  421. SetConsoleCursorPosition(self._handle, coords=WindowsCoordinates(row, column))
  422. def move_cursor_backward(self) -> None:
  423. """Move the cursor backward a single cell. Wrap to the previous line if required."""
  424. row, col = self.cursor_position
  425. if col == 0:
  426. row -= 1
  427. col = self.screen_size.col - 1
  428. else:
  429. col -= 1
  430. SetConsoleCursorPosition(
  431. self._handle, coords=WindowsCoordinates(row=row, col=col)
  432. )
  433. def hide_cursor(self) -> None:
  434. """Hide the cursor"""
  435. current_cursor_size = self._get_cursor_size()
  436. invisible_cursor = CONSOLE_CURSOR_INFO(dwSize=current_cursor_size, bVisible=0)
  437. SetConsoleCursorInfo(self._handle, cursor_info=invisible_cursor)
  438. def show_cursor(self) -> None:
  439. """Show the cursor"""
  440. current_cursor_size = self._get_cursor_size()
  441. visible_cursor = CONSOLE_CURSOR_INFO(dwSize=current_cursor_size, bVisible=1)
  442. SetConsoleCursorInfo(self._handle, cursor_info=visible_cursor)
  443. def set_title(self, title: str) -> None:
  444. """Set the title of the terminal window
  445. Args:
  446. title (str): The new title of the console window
  447. """
  448. assert len(title) < 255, "Console title must be less than 255 characters"
  449. SetConsoleTitle(title)
  450. def _get_cursor_size(self) -> int:
  451. """Get the percentage of the character cell that is filled by the cursor"""
  452. cursor_info = CONSOLE_CURSOR_INFO()
  453. GetConsoleCursorInfo(self._handle, cursor_info=cursor_info)
  454. return int(cursor_info.dwSize)
  455. if __name__ == "__main__":
  456. handle = GetStdHandle()
  457. from rich.console import Console
  458. console = Console()
  459. term = LegacyWindowsTerm(sys.stdout)
  460. term.set_title("Win32 Console Examples")
  461. style = Style(color="black", bgcolor="red")
  462. heading = Style.parse("black on green")
  463. # Check colour output
  464. console.rule("Checking colour output")
  465. console.print("[on red]on red!")
  466. console.print("[blue]blue!")
  467. console.print("[yellow]yellow!")
  468. console.print("[bold yellow]bold yellow!")
  469. console.print("[bright_yellow]bright_yellow!")
  470. console.print("[dim bright_yellow]dim bright_yellow!")
  471. console.print("[italic cyan]italic cyan!")
  472. console.print("[bold white on blue]bold white on blue!")
  473. console.print("[reverse bold white on blue]reverse bold white on blue!")
  474. console.print("[bold black on cyan]bold black on cyan!")
  475. console.print("[black on green]black on green!")
  476. console.print("[blue on green]blue on green!")
  477. console.print("[white on black]white on black!")
  478. console.print("[black on white]black on white!")
  479. console.print("[#1BB152 on #DA812D]#1BB152 on #DA812D!")
  480. # Check cursor movement
  481. console.rule("Checking cursor movement")
  482. console.print()
  483. term.move_cursor_backward()
  484. term.move_cursor_backward()
  485. term.write_text("went back and wrapped to prev line")
  486. time.sleep(1)
  487. term.move_cursor_up()
  488. term.write_text("we go up")
  489. time.sleep(1)
  490. term.move_cursor_down()
  491. term.write_text("and down")
  492. time.sleep(1)
  493. term.move_cursor_up()
  494. term.move_cursor_backward()
  495. term.move_cursor_backward()
  496. term.write_text("we went up and back 2")
  497. time.sleep(1)
  498. term.move_cursor_down()
  499. term.move_cursor_backward()
  500. term.move_cursor_backward()
  501. term.write_text("we went down and back 2")
  502. time.sleep(1)
  503. # Check erasing of lines
  504. term.hide_cursor()
  505. console.print()
  506. console.rule("Checking line erasing")
  507. console.print("\n...Deleting to the start of the line...")
  508. term.write_text("The red arrow shows the cursor location, and direction of erase")
  509. time.sleep(1)
  510. term.move_cursor_to_column(16)
  511. term.write_styled("<", Style.parse("black on red"))
  512. term.move_cursor_backward()
  513. time.sleep(1)
  514. term.erase_start_of_line()
  515. time.sleep(1)
  516. console.print("\n\n...And to the end of the line...")
  517. term.write_text("The red arrow shows the cursor location, and direction of erase")
  518. time.sleep(1)
  519. term.move_cursor_to_column(16)
  520. term.write_styled(">", Style.parse("black on red"))
  521. time.sleep(1)
  522. term.erase_end_of_line()
  523. time.sleep(1)
  524. console.print("\n\n...Now the whole line will be erased...")
  525. term.write_styled("I'm going to disappear!", style=Style.parse("black on cyan"))
  526. time.sleep(1)
  527. term.erase_line()
  528. term.show_cursor()
  529. print("\n")