1
1
Fork 0
mirror of https://github.com/pypa/pip synced 2023-12-13 21:30:23 +01:00
pip/src/pip/_vendor/colorama/win32.py

157 lines
5.3 KiB
Python
Raw Normal View History

# Copyright Jonathan Hartley 2013. BSD 3-Clause license, see LICENSE file.
2013-08-05 04:59:47 +02:00
# from winbase.h
STDOUT = -11
STDERR = -12
try:
2014-09-11 14:46:00 +02:00
import ctypes
from ctypes import LibraryLoader
2014-04-24 14:10:36 +02:00
windll = LibraryLoader(ctypes.WinDLL)
from ctypes import wintypes
2014-04-24 14:10:36 +02:00
except (AttributeError, ImportError):
2013-08-05 04:59:47 +02:00
windll = None
SetConsoleTextAttribute = lambda *_: None
2016-01-19 00:23:50 +01:00
winapi_test = lambda *_: None
2013-08-05 04:59:47 +02:00
else:
2015-01-28 19:50:41 +01:00
from ctypes import byref, Structure, c_char, POINTER
COORD = wintypes._COORD
2013-08-05 04:59:47 +02:00
class CONSOLE_SCREEN_BUFFER_INFO(Structure):
"""struct in wincon.h."""
_fields_ = [
2015-01-28 19:50:41 +01:00
("dwSize", COORD),
("dwCursorPosition", COORD),
("wAttributes", wintypes.WORD),
("srWindow", wintypes.SMALL_RECT),
2015-01-28 19:50:41 +01:00
("dwMaximumWindowSize", COORD),
2013-08-05 04:59:47 +02:00
]
def __str__(self):
return '(%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d)' % (
self.dwSize.Y, self.dwSize.X
, self.dwCursorPosition.Y, self.dwCursorPosition.X
, self.wAttributes
, self.srWindow.Top, self.srWindow.Left, self.srWindow.Bottom, self.srWindow.Right
, self.dwMaximumWindowSize.Y, self.dwMaximumWindowSize.X
)
_GetStdHandle = windll.kernel32.GetStdHandle
_GetStdHandle.argtypes = [
wintypes.DWORD,
]
_GetStdHandle.restype = wintypes.HANDLE
_GetConsoleScreenBufferInfo = windll.kernel32.GetConsoleScreenBufferInfo
_GetConsoleScreenBufferInfo.argtypes = [
wintypes.HANDLE,
POINTER(CONSOLE_SCREEN_BUFFER_INFO),
]
_GetConsoleScreenBufferInfo.restype = wintypes.BOOL
_SetConsoleTextAttribute = windll.kernel32.SetConsoleTextAttribute
_SetConsoleTextAttribute.argtypes = [
wintypes.HANDLE,
wintypes.WORD,
]
_SetConsoleTextAttribute.restype = wintypes.BOOL
_SetConsoleCursorPosition = windll.kernel32.SetConsoleCursorPosition
_SetConsoleCursorPosition.argtypes = [
wintypes.HANDLE,
2015-01-28 19:50:41 +01:00
COORD,
]
_SetConsoleCursorPosition.restype = wintypes.BOOL
_FillConsoleOutputCharacterA = windll.kernel32.FillConsoleOutputCharacterA
_FillConsoleOutputCharacterA.argtypes = [
wintypes.HANDLE,
c_char,
wintypes.DWORD,
2015-01-28 19:50:41 +01:00
COORD,
POINTER(wintypes.DWORD),
]
_FillConsoleOutputCharacterA.restype = wintypes.BOOL
_FillConsoleOutputAttribute = windll.kernel32.FillConsoleOutputAttribute
_FillConsoleOutputAttribute.argtypes = [
wintypes.HANDLE,
wintypes.WORD,
wintypes.DWORD,
2015-01-28 19:50:41 +01:00
COORD,
POINTER(wintypes.DWORD),
]
_FillConsoleOutputAttribute.restype = wintypes.BOOL
2017-05-19 13:53:32 +02:00
_SetConsoleTitleW = windll.kernel32.SetConsoleTitleW
2015-01-28 19:50:41 +01:00
_SetConsoleTitleW.argtypes = [
2017-05-19 13:53:32 +02:00
wintypes.LPCWSTR
2015-01-28 19:50:41 +01:00
]
_SetConsoleTitleW.restype = wintypes.BOOL
handles = {
STDOUT: _GetStdHandle(STDOUT),
STDERR: _GetStdHandle(STDERR),
}
2017-05-19 13:53:32 +02:00
def _winapi_test(handle):
2016-01-19 00:23:50 +01:00
csbi = CONSOLE_SCREEN_BUFFER_INFO()
success = _GetConsoleScreenBufferInfo(
handle, byref(csbi))
return bool(success)
2017-05-19 13:53:32 +02:00
def winapi_test():
return any(_winapi_test(h) for h in handles.values())
2013-08-05 04:59:47 +02:00
def GetConsoleScreenBufferInfo(stream_id=STDOUT):
handle = handles[stream_id]
csbi = CONSOLE_SCREEN_BUFFER_INFO()
success = _GetConsoleScreenBufferInfo(
2013-08-05 04:59:47 +02:00
handle, byref(csbi))
return csbi
def SetConsoleTextAttribute(stream_id, attrs):
handle = handles[stream_id]
return _SetConsoleTextAttribute(handle, attrs)
2013-08-05 04:59:47 +02:00
2015-01-28 19:50:41 +01:00
def SetConsoleCursorPosition(stream_id, position, adjust=True):
position = COORD(*position)
2013-08-05 04:59:47 +02:00
# If the position is out of range, do nothing.
if position.Y <= 0 or position.X <= 0:
return
# Adjust for Windows' SetConsoleCursorPosition:
# 1. being 0-based, while ANSI is 1-based.
# 2. expecting (x,y), while ANSI uses (y,x).
2015-01-28 19:50:41 +01:00
adjusted_position = COORD(position.Y - 1, position.X - 1)
if adjust:
# Adjust for viewport's scroll position
sr = GetConsoleScreenBufferInfo(STDOUT).srWindow
adjusted_position.Y += sr.Top
adjusted_position.X += sr.Left
2013-08-05 04:59:47 +02:00
# Resume normal processing
handle = handles[stream_id]
return _SetConsoleCursorPosition(handle, adjusted_position)
2013-08-05 04:59:47 +02:00
def FillConsoleOutputCharacter(stream_id, char, length, start):
handle = handles[stream_id]
2015-01-28 19:50:41 +01:00
char = c_char(char.encode())
length = wintypes.DWORD(length)
num_written = wintypes.DWORD(0)
2013-08-05 04:59:47 +02:00
# Note that this is hard-coded for ANSI (vs wide) bytes.
success = _FillConsoleOutputCharacterA(
2013-08-05 04:59:47 +02:00
handle, char, length, start, byref(num_written))
return num_written.value
def FillConsoleOutputAttribute(stream_id, attr, length, start):
''' FillConsoleOutputAttribute( hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten )'''
handle = handles[stream_id]
attribute = wintypes.WORD(attr)
length = wintypes.DWORD(length)
num_written = wintypes.DWORD(0)
2013-08-05 04:59:47 +02:00
# Note that this is hard-coded for ANSI (vs wide) bytes.
return _FillConsoleOutputAttribute(
2013-08-05 04:59:47 +02:00
handle, attribute, length, start, byref(num_written))
2015-01-28 19:50:41 +01:00
def SetConsoleTitle(title):
return _SetConsoleTitleW(title)