Mercurial > public > mercurial-scm > hg
comparison mercurial/win32.py @ 32664:2d56e6d23be7
win32: add a method to enable ANSI color code processing on Windows 10
SetConsoleMode() fails with an invalid parameter error if given this option
prior to Windows 10, so indicate that to the caller instead of doing explicit
version checks.
author | Matt Harbison <matt_harbison@yahoo.com> |
---|---|
date | Mon, 22 May 2017 22:00:56 -0400 |
parents | 5861bdbeb9a3 |
children | 4c3d9ee87382 |
comparison
equal
deleted
inserted
replaced
32663:5277d6faabb4 | 32664:2d56e6d23be7 |
---|---|
129 ('dwCursorPosition', _COORD), | 129 ('dwCursorPosition', _COORD), |
130 ('wAttributes', _WORD), | 130 ('wAttributes', _WORD), |
131 ('srWindow', _SMALL_RECT), | 131 ('srWindow', _SMALL_RECT), |
132 ('dwMaximumWindowSize', _COORD)] | 132 ('dwMaximumWindowSize', _COORD)] |
133 | 133 |
134 _STD_OUTPUT_HANDLE = _DWORD(-11).value | |
134 _STD_ERROR_HANDLE = _DWORD(-12).value | 135 _STD_ERROR_HANDLE = _DWORD(-12).value |
135 | 136 |
136 # CreateToolhelp32Snapshot, Process32First, Process32Next | 137 # CreateToolhelp32Snapshot, Process32First, Process32Next |
137 _TH32CS_SNAPPROCESS = 0x00000002 | 138 _TH32CS_SNAPPROCESS = 0x00000002 |
138 _MAX_PATH = 260 | 139 _MAX_PATH = 260 |
199 _kernel32.GetCurrentProcessId.restype = _DWORD | 200 _kernel32.GetCurrentProcessId.restype = _DWORD |
200 | 201 |
201 _SIGNAL_HANDLER = ctypes.WINFUNCTYPE(_BOOL, _DWORD) | 202 _SIGNAL_HANDLER = ctypes.WINFUNCTYPE(_BOOL, _DWORD) |
202 _kernel32.SetConsoleCtrlHandler.argtypes = [_SIGNAL_HANDLER, _BOOL] | 203 _kernel32.SetConsoleCtrlHandler.argtypes = [_SIGNAL_HANDLER, _BOOL] |
203 _kernel32.SetConsoleCtrlHandler.restype = _BOOL | 204 _kernel32.SetConsoleCtrlHandler.restype = _BOOL |
205 | |
206 _kernel32.SetConsoleMode.argtypes = [_HANDLE, _DWORD] | |
207 _kernel32.SetConsoleMode.restype = _BOOL | |
208 | |
209 _kernel32.GetConsoleMode.argtypes = [_HANDLE, ctypes.c_void_p] | |
210 _kernel32.GetConsoleMode.restype = _BOOL | |
204 | 211 |
205 _kernel32.GetStdHandle.argtypes = [_DWORD] | 212 _kernel32.GetStdHandle.argtypes = [_DWORD] |
206 _kernel32.GetStdHandle.restype = _HANDLE | 213 _kernel32.GetStdHandle.restype = _HANDLE |
207 | 214 |
208 _kernel32.GetConsoleScreenBufferInfo.argtypes = [_HANDLE, ctypes.c_void_p] | 215 _kernel32.GetConsoleScreenBufferInfo.argtypes = [_HANDLE, ctypes.c_void_p] |
370 return width, height | 377 return width, height |
371 width = csbi.srWindow.Right - csbi.srWindow.Left # don't '+ 1' | 378 width = csbi.srWindow.Right - csbi.srWindow.Left # don't '+ 1' |
372 height = csbi.srWindow.Bottom - csbi.srWindow.Top + 1 | 379 height = csbi.srWindow.Bottom - csbi.srWindow.Top + 1 |
373 return width, height | 380 return width, height |
374 | 381 |
382 def enablevtmode(): | |
383 '''Enable virtual terminal mode for the associated console. Return True if | |
384 enabled, else False.''' | |
385 | |
386 ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x4 | |
387 | |
388 handle = _kernel32.GetStdHandle(_STD_OUTPUT_HANDLE) # don't close the handle | |
389 if handle == _INVALID_HANDLE_VALUE: | |
390 return False | |
391 | |
392 mode = _DWORD(0) | |
393 | |
394 if not _kernel32.GetConsoleMode(handle, ctypes.byref(mode)): | |
395 return False | |
396 | |
397 if (mode.value & ENABLE_VIRTUAL_TERMINAL_PROCESSING) == 0: | |
398 mode.value |= ENABLE_VIRTUAL_TERMINAL_PROCESSING | |
399 | |
400 if not _kernel32.SetConsoleMode(handle, mode): | |
401 return False | |
402 | |
403 return True | |
404 | |
375 def _1stchild(pid): | 405 def _1stchild(pid): |
376 '''return the 1st found child of the given pid | 406 '''return the 1st found child of the given pid |
377 | 407 |
378 None is returned when no child is found''' | 408 None is returned when no child is found''' |
379 pe = _tagPROCESSENTRY32() | 409 pe = _tagPROCESSENTRY32() |