windows10.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. from __future__ import annotations
  2. import sys
  3. assert sys.platform == "win32"
  4. from ctypes import byref, windll
  5. from ctypes.wintypes import DWORD, HANDLE
  6. from typing import Any, TextIO
  7. from prompt_toolkit.data_structures import Size
  8. from prompt_toolkit.win32_types import STD_OUTPUT_HANDLE
  9. from .base import Output
  10. from .color_depth import ColorDepth
  11. from .vt100 import Vt100_Output
  12. from .win32 import Win32Output
  13. __all__ = [
  14. "Windows10_Output",
  15. ]
  16. # See: https://msdn.microsoft.com/pl-pl/library/windows/desktop/ms686033(v=vs.85).aspx
  17. ENABLE_PROCESSED_INPUT = 0x0001
  18. ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004
  19. class Windows10_Output:
  20. """
  21. Windows 10 output abstraction. This enables and uses vt100 escape sequences.
  22. """
  23. def __init__(
  24. self, stdout: TextIO, default_color_depth: ColorDepth | None = None
  25. ) -> None:
  26. self.default_color_depth = default_color_depth
  27. self.win32_output = Win32Output(stdout, default_color_depth=default_color_depth)
  28. self.vt100_output = Vt100_Output(
  29. stdout, lambda: Size(0, 0), default_color_depth=default_color_depth
  30. )
  31. self._hconsole = HANDLE(windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE))
  32. def flush(self) -> None:
  33. """
  34. Write to output stream and flush.
  35. """
  36. original_mode = DWORD(0)
  37. # Remember the previous console mode.
  38. windll.kernel32.GetConsoleMode(self._hconsole, byref(original_mode))
  39. # Enable processing of vt100 sequences.
  40. windll.kernel32.SetConsoleMode(
  41. self._hconsole,
  42. DWORD(ENABLE_PROCESSED_INPUT | ENABLE_VIRTUAL_TERMINAL_PROCESSING),
  43. )
  44. try:
  45. self.vt100_output.flush()
  46. finally:
  47. # Restore console mode.
  48. windll.kernel32.SetConsoleMode(self._hconsole, original_mode)
  49. @property
  50. def responds_to_cpr(self) -> bool:
  51. return False # We don't need this on Windows.
  52. def __getattr__(self, name: str) -> Any:
  53. if name in (
  54. "get_size",
  55. "get_rows_below_cursor_position",
  56. "enable_mouse_support",
  57. "disable_mouse_support",
  58. "scroll_buffer_to_prompt",
  59. "get_win32_screen_buffer_info",
  60. "enable_bracketed_paste",
  61. "disable_bracketed_paste",
  62. ):
  63. return getattr(self.win32_output, name)
  64. else:
  65. return getattr(self.vt100_output, name)
  66. def get_default_color_depth(self) -> ColorDepth:
  67. """
  68. Return the default color depth for a windows terminal.
  69. Contrary to the Vt100 implementation, this doesn't depend on a $TERM
  70. variable.
  71. """
  72. if self.default_color_depth is not None:
  73. return self.default_color_depth
  74. # Previously, we used `DEPTH_4_BIT`, even on Windows 10. This was
  75. # because true color support was added after "Console Virtual Terminal
  76. # Sequences" support was added, and there was no good way to detect
  77. # what support was given.
  78. # 24bit color support was added in 2016, so let's assume it's safe to
  79. # take that as a default:
  80. # https://devblogs.microsoft.com/commandline/24-bit-color-in-the-windows-console/
  81. return ColorDepth.TRUE_COLOR
  82. Output.register(Windows10_Output)
  83. def is_win_vt100_enabled() -> bool:
  84. """
  85. Returns True when we're running Windows and VT100 escape sequences are
  86. supported.
  87. """
  88. if sys.platform != "win32":
  89. return False
  90. hconsole = HANDLE(windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE))
  91. # Get original console mode.
  92. original_mode = DWORD(0)
  93. windll.kernel32.GetConsoleMode(hconsole, byref(original_mode))
  94. try:
  95. # Try to enable VT100 sequences.
  96. result: int = windll.kernel32.SetConsoleMode(
  97. hconsole, DWORD(ENABLE_PROCESSED_INPUT | ENABLE_VIRTUAL_TERMINAL_PROCESSING)
  98. )
  99. return result == 1
  100. finally:
  101. windll.kernel32.SetConsoleMode(hconsole, original_mode)