Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sqlmapproject
GitHub Repository: sqlmapproject/sqlmap
Path: blob/master/thirdparty/colorama/win32.py
2992 views
1
# Copyright Jonathan Hartley 2013. BSD 3-Clause license, see LICENSE file.
2
3
# from winbase.h
4
STDOUT = -11
5
STDERR = -12
6
7
try:
8
import ctypes
9
from ctypes import LibraryLoader
10
windll = LibraryLoader(ctypes.WinDLL)
11
from ctypes import wintypes
12
except (AttributeError, ImportError):
13
windll = None
14
SetConsoleTextAttribute = lambda *_: None
15
winapi_test = lambda *_: None
16
else:
17
from ctypes import byref, Structure, c_char, POINTER
18
19
COORD = wintypes._COORD
20
21
class CONSOLE_SCREEN_BUFFER_INFO(Structure):
22
"""struct in wincon.h."""
23
_fields_ = [
24
("dwSize", COORD),
25
("dwCursorPosition", COORD),
26
("wAttributes", wintypes.WORD),
27
("srWindow", wintypes.SMALL_RECT),
28
("dwMaximumWindowSize", COORD),
29
]
30
def __str__(self):
31
return '(%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d)' % (
32
self.dwSize.Y, self.dwSize.X
33
, self.dwCursorPosition.Y, self.dwCursorPosition.X
34
, self.wAttributes
35
, self.srWindow.Top, self.srWindow.Left, self.srWindow.Bottom, self.srWindow.Right
36
, self.dwMaximumWindowSize.Y, self.dwMaximumWindowSize.X
37
)
38
39
_GetStdHandle = windll.kernel32.GetStdHandle
40
_GetStdHandle.argtypes = [
41
wintypes.DWORD,
42
]
43
_GetStdHandle.restype = wintypes.HANDLE
44
45
_GetConsoleScreenBufferInfo = windll.kernel32.GetConsoleScreenBufferInfo
46
_GetConsoleScreenBufferInfo.argtypes = [
47
wintypes.HANDLE,
48
POINTER(CONSOLE_SCREEN_BUFFER_INFO),
49
]
50
_GetConsoleScreenBufferInfo.restype = wintypes.BOOL
51
52
_SetConsoleTextAttribute = windll.kernel32.SetConsoleTextAttribute
53
_SetConsoleTextAttribute.argtypes = [
54
wintypes.HANDLE,
55
wintypes.WORD,
56
]
57
_SetConsoleTextAttribute.restype = wintypes.BOOL
58
59
_SetConsoleCursorPosition = windll.kernel32.SetConsoleCursorPosition
60
_SetConsoleCursorPosition.argtypes = [
61
wintypes.HANDLE,
62
COORD,
63
]
64
_SetConsoleCursorPosition.restype = wintypes.BOOL
65
66
_FillConsoleOutputCharacterA = windll.kernel32.FillConsoleOutputCharacterA
67
_FillConsoleOutputCharacterA.argtypes = [
68
wintypes.HANDLE,
69
c_char,
70
wintypes.DWORD,
71
COORD,
72
POINTER(wintypes.DWORD),
73
]
74
_FillConsoleOutputCharacterA.restype = wintypes.BOOL
75
76
_FillConsoleOutputAttribute = windll.kernel32.FillConsoleOutputAttribute
77
_FillConsoleOutputAttribute.argtypes = [
78
wintypes.HANDLE,
79
wintypes.WORD,
80
wintypes.DWORD,
81
COORD,
82
POINTER(wintypes.DWORD),
83
]
84
_FillConsoleOutputAttribute.restype = wintypes.BOOL
85
86
_SetConsoleTitleW = windll.kernel32.SetConsoleTitleA
87
_SetConsoleTitleW.argtypes = [
88
wintypes.LPCSTR
89
]
90
_SetConsoleTitleW.restype = wintypes.BOOL
91
92
handles = {
93
STDOUT: _GetStdHandle(STDOUT),
94
STDERR: _GetStdHandle(STDERR),
95
}
96
97
def winapi_test():
98
handle = handles[STDOUT]
99
csbi = CONSOLE_SCREEN_BUFFER_INFO()
100
success = _GetConsoleScreenBufferInfo(
101
handle, byref(csbi))
102
return bool(success)
103
104
def GetConsoleScreenBufferInfo(stream_id=STDOUT):
105
handle = handles[stream_id]
106
csbi = CONSOLE_SCREEN_BUFFER_INFO()
107
success = _GetConsoleScreenBufferInfo(
108
handle, byref(csbi))
109
return csbi
110
111
def SetConsoleTextAttribute(stream_id, attrs):
112
handle = handles[stream_id]
113
return _SetConsoleTextAttribute(handle, attrs)
114
115
def SetConsoleCursorPosition(stream_id, position, adjust=True):
116
position = COORD(*position)
117
# If the position is out of range, do nothing.
118
if position.Y <= 0 or position.X <= 0:
119
return
120
# Adjust for Windows' SetConsoleCursorPosition:
121
# 1. being 0-based, while ANSI is 1-based.
122
# 2. expecting (x,y), while ANSI uses (y,x).
123
adjusted_position = COORD(position.Y - 1, position.X - 1)
124
if adjust:
125
# Adjust for viewport's scroll position
126
sr = GetConsoleScreenBufferInfo(STDOUT).srWindow
127
adjusted_position.Y += sr.Top
128
adjusted_position.X += sr.Left
129
# Resume normal processing
130
handle = handles[stream_id]
131
return _SetConsoleCursorPosition(handle, adjusted_position)
132
133
def FillConsoleOutputCharacter(stream_id, char, length, start):
134
handle = handles[stream_id]
135
char = c_char(char.encode())
136
length = wintypes.DWORD(length)
137
num_written = wintypes.DWORD(0)
138
# Note that this is hard-coded for ANSI (vs wide) bytes.
139
success = _FillConsoleOutputCharacterA(
140
handle, char, length, start, byref(num_written))
141
return num_written.value
142
143
def FillConsoleOutputAttribute(stream_id, attr, length, start):
144
''' FillConsoleOutputAttribute( hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten )'''
145
handle = handles[stream_id]
146
attribute = wintypes.WORD(attr)
147
length = wintypes.DWORD(length)
148
num_written = wintypes.DWORD(0)
149
# Note that this is hard-coded for ANSI (vs wide) bytes.
150
return _FillConsoleOutputAttribute(
151
handle, attribute, length, start, byref(num_written))
152
153
def SetConsoleTitle(title):
154
return _SetConsoleTitleW(title)
155
156