OLD | NEW |
(Empty) | |
| 1 |
| 2 # from winbase.h |
| 3 STDOUT = -11 |
| 4 STDERR = -12 |
| 5 |
| 6 try: |
| 7 from ctypes import windll |
| 8 except ImportError: |
| 9 windll = None |
| 10 SetConsoleTextAttribute = lambda *_: None |
| 11 else: |
| 12 from ctypes import ( |
| 13 byref, Structure, c_char, c_short, c_uint32, c_ushort |
| 14 ) |
| 15 |
| 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): |
| 43 """struct in wincon.h.""" |
| 44 _fields_ = [ |
| 45 ("dwSize", COORD), |
| 46 ("dwCursorPosition", COORD), |
| 47 ("wAttributes", WORD), |
| 48 ("srWindow", SMALL_RECT), |
| 49 ("dwMaximumWindowSize", COORD), |
| 50 ] |
| 51 def __str__(self): |
| 52 return '(%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d)' % ( |
| 53 self.dwSize.Y, self.dwSize.X |
| 54 , self.dwCursorPosition.Y, self.dwCursorPosition.X |
| 55 , self.wAttributes |
| 56 , self.srWindow.Top, self.srWindow.Left, self.srWindow.Bottom, s
elf.srWindow.Right |
| 57 , self.dwMaximumWindowSize.Y, self.dwMaximumWindowSize.X |
| 58 ) |
| 59 |
| 60 def GetConsoleScreenBufferInfo(stream_id=STDOUT): |
| 61 handle = handles[stream_id] |
| 62 csbi = CONSOLE_SCREEN_BUFFER_INFO() |
| 63 success = windll.kernel32.GetConsoleScreenBufferInfo( |
| 64 handle, byref(csbi)) |
| 65 return csbi |
| 66 |
| 67 |
| 68 def SetConsoleTextAttribute(stream_id, attrs): |
| 69 handle = handles[stream_id] |
| 70 return windll.kernel32.SetConsoleTextAttribute(handle, attrs) |
| 71 |
| 72 |
| 73 def SetConsoleCursorPosition(stream_id, position): |
| 74 position = COORD(*position) |
| 75 # If the position is out of range, do nothing. |
| 76 if position.Y <= 0 or position.X <= 0: |
| 77 return |
| 78 # Adjust for Windows' SetConsoleCursorPosition: |
| 79 # 1. being 0-based, while ANSI is 1-based. |
| 80 # 2. expecting (x,y), while ANSI uses (y,x). |
| 81 adjusted_position = COORD(position.Y - 1, position.X - 1) |
| 82 # Adjust for viewport's scroll position |
| 83 sr = GetConsoleScreenBufferInfo(STDOUT).srWindow |
| 84 adjusted_position.Y += sr.Top |
| 85 adjusted_position.X += sr.Left |
| 86 # Resume normal processing |
| 87 handle = handles[stream_id] |
| 88 return windll.kernel32.SetConsoleCursorPosition(handle, adjusted_positio
n) |
| 89 |
| 90 def FillConsoleOutputCharacter(stream_id, char, length, start): |
| 91 handle = handles[stream_id] |
| 92 char = TCHAR(char) |
| 93 length = DWORD(length) |
| 94 num_written = DWORD(0) |
| 95 # Note that this is hard-coded for ANSI (vs wide) bytes. |
| 96 success = windll.kernel32.FillConsoleOutputCharacterA( |
| 97 handle, char, length, start, byref(num_written)) |
| 98 return num_written.value |
| 99 |
| 100 def FillConsoleOutputAttribute(stream_id, attr, length, start): |
| 101 ''' FillConsoleOutputAttribute( hConsole, csbi.wAttributes, dwConSize, c
oordScreen, &cCharsWritten )''' |
| 102 handle = handles[stream_id] |
| 103 attribute = WORD(attr) |
| 104 length = DWORD(length) |
| 105 num_written = DWORD(0) |
| 106 # Note that this is hard-coded for ANSI (vs wide) bytes. |
| 107 return windll.kernel32.FillConsoleOutputAttribute( |
| 108 handle, attribute, length, start, byref(num_written)) |
| 109 |
OLD | NEW |