codekingpro/portable-devtools
114k
1import sys2from dataclasses import dataclass3 4 5@dataclass6class WindowsConsoleFeatures:7 """Windows features available."""8 9 vt: bool = False10 """The console supports VT codes."""11 truecolor: bool = False12 """The console supports truecolor."""13 14 15try:16 import ctypes17 from ctypes import LibraryLoader18 19 if sys.platform == "win32":20 windll = LibraryLoader(ctypes.WinDLL)21 else:22 windll = None23 raise ImportError("Not windows")24 25 from rich._win32_console import (26 ENABLE_VIRTUAL_TERMINAL_PROCESSING,27 GetConsoleMode,28 GetStdHandle,29 LegacyWindowsError,30 )31 32except (AttributeError, ImportError, ValueError):33 # Fallback if we can't load the Windows DLL34 def get_windows_console_features() -> WindowsConsoleFeatures:35 features = WindowsConsoleFeatures()36 return features37 38else:39 40 def get_windows_console_features() -> WindowsConsoleFeatures:41 """Get windows console features.42 43 Returns:44 WindowsConsoleFeatures: An instance of WindowsConsoleFeatures.45 """46 handle = GetStdHandle()47 try:48 console_mode = GetConsoleMode(handle)49 success = True50 except LegacyWindowsError:51 console_mode = 052 success = False53 vt = bool(success and console_mode & ENABLE_VIRTUAL_TERMINAL_PROCESSING)54 truecolor = False55 if vt:56 win_version = sys.getwindowsversion()57 truecolor = win_version.major > 10 or (58 win_version.major == 10 and win_version.build >= 1506359 )60 features = WindowsConsoleFeatures(vt=vt, truecolor=truecolor)61 return features62 63 64if __name__ == "__main__":65 import platform66 67 features = get_windows_console_features()68 from rich import print69 70 print(f'platform="{platform.system()}"')71 print(repr(features))72 