forked from adnanaziz/EPIJudge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
platform.py
77 lines (56 loc) · 2.09 KB
/
platform.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# @library
import sys
ENABLE_COLOR_OUTPUT = True
def running_on_win():
return sys.platform == 'win32'
def use_tty_output():
if not use_tty_output.__dict__.get('cached', None):
use_tty_output.cached = sys.stdout.isatty()
return use_tty_output.cached
def use_color_output():
return use_tty_output() and ENABLE_COLOR_OUTPUT
if running_on_win():
try:
import ctypes
from ctypes import LibraryLoader
windll = LibraryLoader(ctypes.WinDLL)
from ctypes import wintypes
except (AttributeError, ImportError):
ENABLE_COLOR_OUTPUT = False
else:
from ctypes import byref, Structure, c_char, POINTER
COORD = wintypes._COORD
class ConsoleScreenBufferInfo(Structure):
"""struct in wincon.h."""
_fields_ = [
("dwSize", COORD),
("dwCursorPosition", COORD),
("wAttributes", wintypes.WORD),
("srWindow", wintypes.SMALL_RECT),
("dwMaximumWindowSize", COORD),
]
_GetStdHandle = windll.kernel32.GetStdHandle
_GetStdHandle.argtypes = [
wintypes.DWORD,
]
_GetStdHandle.restype = wintypes.HANDLE
_GetConsoleScreenBufferInfo = windll.kernel32.GetConsoleScreenBufferInfo
_GetConsoleScreenBufferInfo.argtypes = [
wintypes.HANDLE,
POINTER(ConsoleScreenBufferInfo),
]
_GetConsoleScreenBufferInfo.restype = wintypes.BOOL
_SetConsoleTextAttribute = windll.kernel32.SetConsoleTextAttribute
_SetConsoleTextAttribute.argtypes = [
wintypes.HANDLE,
wintypes.WORD,
]
_SetConsoleTextAttribute.restype = wintypes.BOOL
STDOUT = -11
stdout_handle = _GetStdHandle(STDOUT)
def get_console_screen_buffer_info():
csbi = ConsoleScreenBufferInfo()
success = _GetConsoleScreenBufferInfo(stdout_handle, byref(csbi))
return csbi
def set_console_text_attribute(attrs):
return _SetConsoleTextAttribute(stdout_handle, attrs)