codekingpro/portable-devtools
114k
1from typing import Iterable, Sequence, Tuple, cast2 3from rich._win32_console import LegacyWindowsTerm, WindowsCoordinates4from rich.segment import ControlCode, ControlType, Segment5 6 7def legacy_windows_render(buffer: Iterable[Segment], term: LegacyWindowsTerm) -> None:8 """Makes appropriate Windows Console API calls based on the segments in the buffer.9 10 Args:11 buffer (Iterable[Segment]): Iterable of Segments to convert to Win32 API calls.12 term (LegacyWindowsTerm): Used to call the Windows Console API.13 """14 for text, style, control in buffer:15 if not control:16 if style:17 term.write_styled(text, style)18 else:19 term.write_text(text)20 else:21 control_codes: Sequence[ControlCode] = control22 for control_code in control_codes:23 control_type = control_code[0]24 if control_type == ControlType.CURSOR_MOVE_TO:25 _, x, y = cast(Tuple[ControlType, int, int], control_code)26 term.move_cursor_to(WindowsCoordinates(row=y - 1, col=x - 1))27 elif control_type == ControlType.CARRIAGE_RETURN:28 term.write_text("\r")29 elif control_type == ControlType.HOME:30 term.move_cursor_to(WindowsCoordinates(0, 0))31 elif control_type == ControlType.CURSOR_UP:32 term.move_cursor_up()33 elif control_type == ControlType.CURSOR_DOWN:34 term.move_cursor_down()35 elif control_type == ControlType.CURSOR_FORWARD:36 term.move_cursor_forward()37 elif control_type == ControlType.CURSOR_BACKWARD:38 term.move_cursor_backward()39 elif control_type == ControlType.CURSOR_MOVE_TO_COLUMN:40 _, column = cast(Tuple[ControlType, int], control_code)41 term.move_cursor_to_column(column - 1)42 elif control_type == ControlType.HIDE_CURSOR:43 term.hide_cursor()44 elif control_type == ControlType.SHOW_CURSOR:45 term.show_cursor()46 elif control_type == ControlType.ERASE_IN_LINE:47 _, mode = cast(Tuple[ControlType, int], control_code)48 if mode == 0:49 term.erase_end_of_line()50 elif mode == 1:51 term.erase_start_of_line()52 elif mode == 2:53 term.erase_line()54 elif control_type == ControlType.SET_WINDOW_TITLE:55 _, title = cast(Tuple[ControlType, str], control_code)56 term.set_title(title)57 