<s> STDOUT = - <NUM_LIT:11> <EOL> STDERR = - <NUM_LIT:12> <EOL> try : <EOL> import ctypes <EOL> from ctypes import LibraryLoader <EOL> windll = LibraryLoader ( ctypes . WinDLL ) <EOL> from ctypes import wintypes <EOL> except ( AttributeError , ImportError ) : <EOL> windll = None <EOL> SetConsoleTextAttribute = lambda * _ : None <EOL> else : <EOL> from ctypes import byref , Structure , c_char , POINTER <EOL> COORD = wintypes . _COORD <EOL> class CONSOLE_SCREEN_BUFFER_INFO ( Structure ) : <EOL> """<STR_LIT>""" <EOL> _fields_ = [ <EOL> ( "<STR_LIT>" , COORD ) , <EOL> ( "<STR_LIT>" , COORD ) , <EOL> ( "<STR_LIT>" , wintypes . WORD ) , <EOL> ( "<STR_LIT>" , wintypes . SMALL_RECT ) , <EOL> ( "<STR_LIT>" , COORD ) , <EOL> ] <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' % ( <EOL> self . dwSize . Y , self . dwSize . X <EOL> , self . dwCursorPosition . Y , self . dwCursorPosition . X <EOL> , self . wAttributes <EOL> , self . srWindow . Top , self . srWindow . Left , self . srWindow . Bottom , self . srWindow . Right <EOL> , self . dwMaximumWindowSize . Y , self . dwMaximumWindowSize . X <EOL> ) <EOL> _GetStdHandle = windll . kernel32 . GetStdHandle <EOL> _GetStdHandle . argtypes = [ <EOL> wintypes . DWORD , <EOL> ] <EOL> _GetStdHandle . restype = wintypes . HANDLE <EOL> _GetConsoleScreenBufferInfo = windll . kernel32 . GetConsoleScreenBufferInfo <EOL> _GetConsoleScreenBufferInfo . argtypes = [ <EOL> wintypes . HANDLE , <EOL> POINTER ( CONSOLE_SCREEN_BUFFER_INFO ) , <EOL> ] <EOL> _GetConsoleScreenBufferInfo . restype = wintypes . BOOL <EOL> _SetConsoleTextAttribute = windll . kernel32 . SetConsoleTextAttribute <EOL> _SetConsoleTextAttribute . argtypes = [ <EOL> wintypes . HANDLE , <EOL> wintypes . WORD , <EOL> ] <EOL> _SetConsoleTextAttribute . restype = wintypes . BOOL <EOL> _SetConsoleCursorPosition = windll . kernel32 . SetConsoleCursorPosition <EOL> _SetConsoleCursorPosition . argtypes = [ <EOL> wintypes . HANDLE , <EOL> COORD , <EOL> ] <EOL> _SetConsoleCursorPosition . restype = wintypes . BOOL <EOL> _FillConsoleOutputCharacterA = windll . kernel32 . FillConsoleOutputCharacterA <EOL> _FillConsoleOutputCharacterA . argtypes = [ <EOL> wintypes . HANDLE , <EOL> c_char , <EOL> wintypes . DWORD , <EOL> COORD , <EOL> POINTER ( wintypes . DWORD ) , <EOL> ] <EOL> _FillConsoleOutputCharacterA . restype = wintypes . BOOL <EOL> _FillConsoleOutputAttribute = windll . kernel32 . FillConsoleOutputAttribute <EOL> _FillConsoleOutputAttribute . argtypes = [ <EOL> wintypes . HANDLE , <EOL> wintypes . WORD , <EOL> wintypes . DWORD , <EOL> COORD , <EOL> POINTER ( wintypes . DWORD ) , <EOL> ] <EOL> _FillConsoleOutputAttribute . restype = wintypes . BOOL <EOL> _SetConsoleTitleW = windll . kernel32 . SetConsoleTitleA <EOL> _SetConsoleTitleW . argtypes = [ <EOL> wintypes . LPCSTR <EOL> ] <EOL> _SetConsoleTitleW . restype = wintypes . BOOL <EOL> handles = { <EOL> STDOUT : _GetStdHandle ( STDOUT ) , <EOL> STDERR : _GetStdHandle ( STDERR ) , <EOL> } <EOL> def GetConsoleScreenBufferInfo ( stream_id = STDOUT ) : <EOL> handle = handles [ stream_id ] <EOL> csbi = CONSOLE_SCREEN_BUFFER_INFO ( ) <EOL> success = _GetConsoleScreenBufferInfo ( <EOL> handle , byref ( csbi ) ) <EOL> return csbi <EOL> def SetConsoleTextAttribute ( stream_id , attrs ) : <EOL> handle = handles [ stream_id ] <EOL> return _SetConsoleTextAttribute ( handle , attrs ) <EOL> def SetConsoleCursorPosition ( stream_id , position , adjust = True ) : <EOL> position = COORD ( * position ) <EOL> if position . Y <= <NUM_LIT:0> or position . X <= <NUM_LIT:0> : <EOL> return <EOL> adjusted_position = COORD ( position . Y - <NUM_LIT:1> , position . X - <NUM_LIT:1> ) <EOL> if adjust : <EOL> sr = GetConsoleScreenBufferInfo ( STDOUT ) . srWindow <EOL> adjusted_position . Y += sr . Top <EOL> adjusted_position . X += sr . Left <EOL> handle = handles [ stream_id ] <EOL> return _SetConsoleCursorPosition ( handle , adjusted_position ) <EOL> def FillConsoleOutputCharacter ( stream_id , char , length , start ) : <EOL> handle = handles [ stream_id ] <EOL> char = c_char ( char . encode ( ) ) <EOL> length = wintypes . DWORD ( length ) <EOL> num_written = wintypes . DWORD ( <NUM_LIT:0> ) <EOL> success = _FillConsoleOutputCharacterA ( <EOL> handle , char , length , start , byref ( num_written ) ) <EOL> return num_written . value <EOL> def FillConsoleOutputAttribute ( stream_id , attr , length , start ) : <EOL> '''<STR_LIT>''' <EOL> handle = handles [ stream_id ] <EOL> attribute = wintypes . WORD ( attr ) <EOL> length = wintypes . DWORD ( length ) <EOL> num_written = wintypes . DWORD ( <NUM_LIT:0> ) <EOL> return _FillConsoleOutputAttribute ( <EOL> handle , attribute , length , start , byref ( num_written ) ) <EOL> def SetConsoleTitle ( title ) : <EOL> return _SetConsoleTitleW ( title ) </s>
