Design Pattern Examples
Overview of object-oriented design patterns
enablevtmode.py
Go to the documentation of this file.
16
17
18STD_OUTPUT_HANDLE = -11
19
21ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004
22
23
25originalConsoleMode = None
26
27# Try to import ctypes and get the kernel32 DLL. If neither is possible, the
28# rest of the code in this module does nothing.
29try:
30 import ctypes
31 kernel32 = ctypes.windll.kernel32
32except:
33 # ctypes does not exist, assume not on Windows
34 kernel32 = None
35
36
39def _SetVTMode() -> None:
40 if kernel32:
41 global originalConsoleMode
42 stdOutput = kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
43 oldMode = ctypes.c_uint32(0)
44 if kernel32.GetConsoleMode(stdOutput, ctypes.byref(oldMode)):
45 originalConsoleMode = oldMode.value
46 newMode = originalConsoleMode | ENABLE_VIRTUAL_TERMINAL_PROCESSING
47 kernel32.SetConsoleMode(stdOutput, newMode)
48
49
50
52def _RestoreVTMode() -> None:
53 if kernel32 and originalConsoleMode is not None:
54 stdOutput = kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
55 kernel32.SetConsoleMode(stdOutput, originalConsoleMode)
56
57
58
66def EnableVTMode(function : callable):
68 try:
69 return function()
70 finally:
None _SetVTMode()
If we are on Windows (as indicated by the presence of the Kernel32 DLL), set the virtual terminal pro...
Definition: enablevtmode.py:39
None _RestoreVTMode()
If we are on Windows (as indicated by the presence of the Kernel32 DLL), restore the standard output ...
Definition: enablevtmode.py:52