comparison env/lib/python3.9/site-packages/pip/_vendor/colorama/win32.py @ 0:4f3585e2f14b draft default tip

"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author shellac
date Mon, 22 Mar 2021 18:12:50 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4f3585e2f14b
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.SetConsoleTitleW
87 _SetConsoleTitleW.argtypes = [
88 wintypes.LPCWSTR
89 ]
90 _SetConsoleTitleW.restype = wintypes.BOOL
91
92 def _winapi_test(handle):
93 csbi = CONSOLE_SCREEN_BUFFER_INFO()
94 success = _GetConsoleScreenBufferInfo(
95 handle, byref(csbi))
96 return bool(success)
97
98 def winapi_test():
99 return any(_winapi_test(h) for h in
100 (_GetStdHandle(STDOUT), _GetStdHandle(STDERR)))
101
102 def GetConsoleScreenBufferInfo(stream_id=STDOUT):
103 handle = _GetStdHandle(stream_id)
104 csbi = CONSOLE_SCREEN_BUFFER_INFO()
105 success = _GetConsoleScreenBufferInfo(
106 handle, byref(csbi))
107 return csbi
108
109 def SetConsoleTextAttribute(stream_id, attrs):
110 handle = _GetStdHandle(stream_id)
111 return _SetConsoleTextAttribute(handle, attrs)
112
113 def SetConsoleCursorPosition(stream_id, position, adjust=True):
114 position = COORD(*position)
115 # If the position is out of range, do nothing.
116 if position.Y <= 0 or position.X <= 0:
117 return
118 # Adjust for Windows' SetConsoleCursorPosition:
119 # 1. being 0-based, while ANSI is 1-based.
120 # 2. expecting (x,y), while ANSI uses (y,x).
121 adjusted_position = COORD(position.Y - 1, position.X - 1)
122 if adjust:
123 # Adjust for viewport's scroll position
124 sr = GetConsoleScreenBufferInfo(STDOUT).srWindow
125 adjusted_position.Y += sr.Top
126 adjusted_position.X += sr.Left
127 # Resume normal processing
128 handle = _GetStdHandle(stream_id)
129 return _SetConsoleCursorPosition(handle, adjusted_position)
130
131 def FillConsoleOutputCharacter(stream_id, char, length, start):
132 handle = _GetStdHandle(stream_id)
133 char = c_char(char.encode())
134 length = wintypes.DWORD(length)
135 num_written = wintypes.DWORD(0)
136 # Note that this is hard-coded for ANSI (vs wide) bytes.
137 success = _FillConsoleOutputCharacterA(
138 handle, char, length, start, byref(num_written))
139 return num_written.value
140
141 def FillConsoleOutputAttribute(stream_id, attr, length, start):
142 ''' FillConsoleOutputAttribute( hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten )'''
143 handle = _GetStdHandle(stream_id)
144 attribute = wintypes.WORD(attr)
145 length = wintypes.DWORD(length)
146 num_written = wintypes.DWORD(0)
147 # Note that this is hard-coded for ANSI (vs wide) bytes.
148 return _FillConsoleOutputAttribute(
149 handle, attribute, length, start, byref(num_written))
150
151 def SetConsoleTitle(title):
152 return _SetConsoleTitleW(title)