mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-19 17:14:43 +04:00
Merge pull request #517 from hackedd/windows-color
Windows: enable ANSI escape support in console
This commit is contained in:
commit
1be4e4f82e
3 changed files with 58 additions and 0 deletions
|
|
@ -8,6 +8,7 @@ omit =
|
||||||
setup.py
|
setup.py
|
||||||
# Don't complain if non-runnable code isn't run
|
# Don't complain if non-runnable code isn't run
|
||||||
*/__main__.py
|
*/__main__.py
|
||||||
|
pre_commit/color_windows.py
|
||||||
|
|
||||||
[report]
|
[report]
|
||||||
show_missing = True
|
show_missing = True
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,15 @@
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
if os.name == 'nt': # pragma: no cover (windows)
|
||||||
|
from pre_commit.color_windows import enable_virtual_terminal_processing
|
||||||
|
try:
|
||||||
|
enable_virtual_terminal_processing()
|
||||||
|
except WindowsError:
|
||||||
|
pass
|
||||||
|
|
||||||
RED = '\033[41m'
|
RED = '\033[41m'
|
||||||
GREEN = '\033[42m'
|
GREEN = '\033[42m'
|
||||||
YELLOW = '\033[43;30m'
|
YELLOW = '\033[43;30m'
|
||||||
|
|
|
||||||
49
pre_commit/color_windows.py
Normal file
49
pre_commit/color_windows.py
Normal file
|
|
@ -0,0 +1,49 @@
|
||||||
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
from ctypes import POINTER
|
||||||
|
from ctypes import windll
|
||||||
|
from ctypes import WinError
|
||||||
|
from ctypes import WINFUNCTYPE
|
||||||
|
from ctypes.wintypes import BOOL
|
||||||
|
from ctypes.wintypes import DWORD
|
||||||
|
from ctypes.wintypes import HANDLE
|
||||||
|
|
||||||
|
STD_OUTPUT_HANDLE = -11
|
||||||
|
ENABLE_VIRTUAL_TERMINAL_PROCESSING = 4
|
||||||
|
|
||||||
|
|
||||||
|
def bool_errcheck(result, func, args):
|
||||||
|
if not result:
|
||||||
|
raise WinError()
|
||||||
|
return args
|
||||||
|
|
||||||
|
|
||||||
|
GetStdHandle = WINFUNCTYPE(HANDLE, DWORD)(
|
||||||
|
("GetStdHandle", windll.kernel32),
|
||||||
|
((1, "nStdHandle"), )
|
||||||
|
)
|
||||||
|
|
||||||
|
GetConsoleMode = WINFUNCTYPE(BOOL, HANDLE, POINTER(DWORD))(
|
||||||
|
("GetConsoleMode", windll.kernel32),
|
||||||
|
((1, "hConsoleHandle"), (2, "lpMode"))
|
||||||
|
)
|
||||||
|
GetConsoleMode.errcheck = bool_errcheck
|
||||||
|
|
||||||
|
SetConsoleMode = WINFUNCTYPE(BOOL, HANDLE, DWORD)(
|
||||||
|
("SetConsoleMode", windll.kernel32),
|
||||||
|
((1, "hConsoleHandle"), (1, "dwMode"))
|
||||||
|
)
|
||||||
|
SetConsoleMode.errcheck = bool_errcheck
|
||||||
|
|
||||||
|
|
||||||
|
def enable_virtual_terminal_processing():
|
||||||
|
"""As of Windows 10, the Windows console supports (some) ANSI escape
|
||||||
|
sequences, but it needs to be enabled using `SetConsoleMode` first.
|
||||||
|
|
||||||
|
More info on the escape sequences supported:
|
||||||
|
https://msdn.microsoft.com/en-us/library/windows/desktop/mt638032(v=vs.85).aspx
|
||||||
|
|
||||||
|
"""
|
||||||
|
stdout = GetStdHandle(STD_OUTPUT_HANDLE)
|
||||||
|
flags = GetConsoleMode(stdout)
|
||||||
|
SetConsoleMode(stdout, flags | ENABLE_VIRTUAL_TERMINAL_PROCESSING)
|
||||||
Loading…
Add table
Add a link
Reference in a new issue