OLD | NEW |
| 1 # Copyright Jonathan Hartley 2013. BSD 3-Clause license, see LICENSE file. |
1 | 2 |
2 # from winbase.h | 3 # from winbase.h |
3 STDOUT = -11 | 4 STDOUT = -11 |
4 STDERR = -12 | 5 STDERR = -12 |
5 | 6 |
6 try: | 7 try: |
7 from ctypes import windll | 8 from ctypes import windll |
| 9 from ctypes import wintypes |
8 except ImportError: | 10 except ImportError: |
9 windll = None | 11 windll = None |
10 SetConsoleTextAttribute = lambda *_: None | 12 SetConsoleTextAttribute = lambda *_: None |
11 else: | 13 else: |
12 from ctypes import ( | 14 from ctypes import ( |
13 byref, Structure, c_char, c_short, c_uint32, c_ushort | 15 byref, Structure, c_char, c_short, c_uint32, c_ushort, POINTER |
14 ) | 16 ) |
15 | 17 |
16 handles = { | |
17 STDOUT: windll.kernel32.GetStdHandle(STDOUT), | |
18 STDERR: windll.kernel32.GetStdHandle(STDERR), | |
19 } | |
20 | |
21 SHORT = c_short | |
22 WORD = c_ushort | |
23 DWORD = c_uint32 | |
24 TCHAR = c_char | |
25 | |
26 class COORD(Structure): | |
27 """struct in wincon.h""" | |
28 _fields_ = [ | |
29 ('X', SHORT), | |
30 ('Y', SHORT), | |
31 ] | |
32 | |
33 class SMALL_RECT(Structure): | |
34 """struct in wincon.h.""" | |
35 _fields_ = [ | |
36 ("Left", SHORT), | |
37 ("Top", SHORT), | |
38 ("Right", SHORT), | |
39 ("Bottom", SHORT), | |
40 ] | |
41 | |
42 class CONSOLE_SCREEN_BUFFER_INFO(Structure): | 18 class CONSOLE_SCREEN_BUFFER_INFO(Structure): |
43 """struct in wincon.h.""" | 19 """struct in wincon.h.""" |
44 _fields_ = [ | 20 _fields_ = [ |
45 ("dwSize", COORD), | 21 ("dwSize", wintypes._COORD), |
46 ("dwCursorPosition", COORD), | 22 ("dwCursorPosition", wintypes._COORD), |
47 ("wAttributes", WORD), | 23 ("wAttributes", wintypes.WORD), |
48 ("srWindow", SMALL_RECT), | 24 ("srWindow", wintypes.SMALL_RECT), |
49 ("dwMaximumWindowSize", COORD), | 25 ("dwMaximumWindowSize", wintypes._COORD), |
50 ] | 26 ] |
51 def __str__(self): | 27 def __str__(self): |
52 return '(%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d)' % ( | 28 return '(%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d)' % ( |
53 self.dwSize.Y, self.dwSize.X | 29 self.dwSize.Y, self.dwSize.X |
54 , self.dwCursorPosition.Y, self.dwCursorPosition.X | 30 , self.dwCursorPosition.Y, self.dwCursorPosition.X |
55 , self.wAttributes | 31 , self.wAttributes |
56 , self.srWindow.Top, self.srWindow.Left, self.srWindow.Bottom, s
elf.srWindow.Right | 32 , self.srWindow.Top, self.srWindow.Left, self.srWindow.Bottom, s
elf.srWindow.Right |
57 , self.dwMaximumWindowSize.Y, self.dwMaximumWindowSize.X | 33 , self.dwMaximumWindowSize.Y, self.dwMaximumWindowSize.X |
58 ) | 34 ) |
59 | 35 |
| 36 _GetStdHandle = windll.kernel32.GetStdHandle |
| 37 _GetStdHandle.argtypes = [ |
| 38 wintypes.DWORD, |
| 39 ] |
| 40 _GetStdHandle.restype = wintypes.HANDLE |
| 41 |
| 42 _GetConsoleScreenBufferInfo = windll.kernel32.GetConsoleScreenBufferInfo |
| 43 _GetConsoleScreenBufferInfo.argtypes = [ |
| 44 wintypes.HANDLE, |
| 45 POINTER(CONSOLE_SCREEN_BUFFER_INFO), |
| 46 ] |
| 47 _GetConsoleScreenBufferInfo.restype = wintypes.BOOL |
| 48 |
| 49 _SetConsoleTextAttribute = windll.kernel32.SetConsoleTextAttribute |
| 50 _SetConsoleTextAttribute.argtypes = [ |
| 51 wintypes.HANDLE, |
| 52 wintypes.WORD, |
| 53 ] |
| 54 _SetConsoleTextAttribute.restype = wintypes.BOOL |
| 55 |
| 56 _SetConsoleCursorPosition = windll.kernel32.SetConsoleCursorPosition |
| 57 _SetConsoleCursorPosition.argtypes = [ |
| 58 wintypes.HANDLE, |
| 59 wintypes._COORD, |
| 60 ] |
| 61 _SetConsoleCursorPosition.restype = wintypes.BOOL |
| 62 |
| 63 _FillConsoleOutputCharacterA = windll.kernel32.FillConsoleOutputCharacterA |
| 64 _FillConsoleOutputCharacterA.argtypes = [ |
| 65 wintypes.HANDLE, |
| 66 c_char, |
| 67 wintypes.DWORD, |
| 68 wintypes._COORD, |
| 69 POINTER(wintypes.DWORD), |
| 70 ] |
| 71 _FillConsoleOutputCharacterA.restype = wintypes.BOOL |
| 72 |
| 73 _FillConsoleOutputAttribute = windll.kernel32.FillConsoleOutputAttribute |
| 74 _FillConsoleOutputAttribute.argtypes = [ |
| 75 wintypes.HANDLE, |
| 76 wintypes.WORD, |
| 77 wintypes.DWORD, |
| 78 wintypes._COORD, |
| 79 POINTER(wintypes.DWORD), |
| 80 ] |
| 81 _FillConsoleOutputAttribute.restype = wintypes.BOOL |
| 82 |
| 83 handles = { |
| 84 STDOUT: _GetStdHandle(STDOUT), |
| 85 STDERR: _GetStdHandle(STDERR), |
| 86 } |
| 87 |
60 def GetConsoleScreenBufferInfo(stream_id=STDOUT): | 88 def GetConsoleScreenBufferInfo(stream_id=STDOUT): |
61 handle = handles[stream_id] | 89 handle = handles[stream_id] |
62 csbi = CONSOLE_SCREEN_BUFFER_INFO() | 90 csbi = CONSOLE_SCREEN_BUFFER_INFO() |
63 success = windll.kernel32.GetConsoleScreenBufferInfo( | 91 success = _GetConsoleScreenBufferInfo( |
64 handle, byref(csbi)) | 92 handle, byref(csbi)) |
65 return csbi | 93 return csbi |
66 | 94 |
67 | |
68 def SetConsoleTextAttribute(stream_id, attrs): | 95 def SetConsoleTextAttribute(stream_id, attrs): |
69 handle = handles[stream_id] | 96 handle = handles[stream_id] |
70 return windll.kernel32.SetConsoleTextAttribute(handle, attrs) | 97 return _SetConsoleTextAttribute(handle, attrs) |
71 | |
72 | 98 |
73 def SetConsoleCursorPosition(stream_id, position): | 99 def SetConsoleCursorPosition(stream_id, position): |
74 position = COORD(*position) | 100 position = wintypes._COORD(*position) |
75 # If the position is out of range, do nothing. | 101 # If the position is out of range, do nothing. |
76 if position.Y <= 0 or position.X <= 0: | 102 if position.Y <= 0 or position.X <= 0: |
77 return | 103 return |
78 # Adjust for Windows' SetConsoleCursorPosition: | 104 # Adjust for Windows' SetConsoleCursorPosition: |
79 # 1. being 0-based, while ANSI is 1-based. | 105 # 1. being 0-based, while ANSI is 1-based. |
80 # 2. expecting (x,y), while ANSI uses (y,x). | 106 # 2. expecting (x,y), while ANSI uses (y,x). |
81 adjusted_position = COORD(position.Y - 1, position.X - 1) | 107 adjusted_position = wintypes._COORD(position.Y - 1, position.X - 1) |
82 # Adjust for viewport's scroll position | 108 # Adjust for viewport's scroll position |
83 sr = GetConsoleScreenBufferInfo(STDOUT).srWindow | 109 sr = GetConsoleScreenBufferInfo(STDOUT).srWindow |
84 adjusted_position.Y += sr.Top | 110 adjusted_position.Y += sr.Top |
85 adjusted_position.X += sr.Left | 111 adjusted_position.X += sr.Left |
86 # Resume normal processing | 112 # Resume normal processing |
87 handle = handles[stream_id] | 113 handle = handles[stream_id] |
88 return windll.kernel32.SetConsoleCursorPosition(handle, adjusted_positio
n) | 114 return _SetConsoleCursorPosition(handle, adjusted_position) |
89 | 115 |
90 def FillConsoleOutputCharacter(stream_id, char, length, start): | 116 def FillConsoleOutputCharacter(stream_id, char, length, start): |
91 handle = handles[stream_id] | 117 handle = handles[stream_id] |
92 char = TCHAR(char) | 118 char = c_char(char) |
93 length = DWORD(length) | 119 length = wintypes.DWORD(length) |
94 num_written = DWORD(0) | 120 num_written = wintypes.DWORD(0) |
95 # Note that this is hard-coded for ANSI (vs wide) bytes. | 121 # Note that this is hard-coded for ANSI (vs wide) bytes. |
96 success = windll.kernel32.FillConsoleOutputCharacterA( | 122 success = _FillConsoleOutputCharacterA( |
97 handle, char, length, start, byref(num_written)) | 123 handle, char, length, start, byref(num_written)) |
98 return num_written.value | 124 return num_written.value |
99 | 125 |
100 def FillConsoleOutputAttribute(stream_id, attr, length, start): | 126 def FillConsoleOutputAttribute(stream_id, attr, length, start): |
101 ''' FillConsoleOutputAttribute( hConsole, csbi.wAttributes, dwConSize, c
oordScreen, &cCharsWritten )''' | 127 ''' FillConsoleOutputAttribute( hConsole, csbi.wAttributes, dwConSize, c
oordScreen, &cCharsWritten )''' |
102 handle = handles[stream_id] | 128 handle = handles[stream_id] |
103 attribute = WORD(attr) | 129 attribute = wintypes.WORD(attr) |
104 length = DWORD(length) | 130 length = wintypes.DWORD(length) |
105 num_written = DWORD(0) | 131 num_written = wintypes.DWORD(0) |
106 # Note that this is hard-coded for ANSI (vs wide) bytes. | 132 # Note that this is hard-coded for ANSI (vs wide) bytes. |
107 return windll.kernel32.FillConsoleOutputAttribute( | 133 return _FillConsoleOutputAttribute( |
108 handle, attribute, length, start, byref(num_written)) | 134 handle, attribute, length, start, byref(num_written)) |
109 | |
OLD | NEW |