parent
677f7c0a6a
commit
e5d09b771a
|
@ -1,6 +1,8 @@
|
||||||
Changes between 1.3.2 and 1.3.3a1
|
Changes between 1.3.2 and 1.3.3a1
|
||||||
==================================================
|
==================================================
|
||||||
|
|
||||||
|
- fix terminal coloring for win32
|
||||||
|
(thanks Michael Foord for reporting)
|
||||||
- fix weirdness: make terminal width detection work on stdout instead of stdin
|
- fix weirdness: make terminal width detection work on stdout instead of stdin
|
||||||
(thanks Armin Ronacher for reporting)
|
(thanks Armin Ronacher for reporting)
|
||||||
|
|
||||||
|
|
|
@ -78,9 +78,11 @@ def ansi_print(text, esc, file=None, newline=True, flush=False):
|
||||||
handle = GetStdHandle(STD_ERROR_HANDLE)
|
handle = GetStdHandle(STD_ERROR_HANDLE)
|
||||||
else:
|
else:
|
||||||
handle = GetStdHandle(STD_OUTPUT_HANDLE)
|
handle = GetStdHandle(STD_OUTPUT_HANDLE)
|
||||||
|
oldcolors = GetConsoleInfo(handle).wAttributes
|
||||||
|
attr |= (oldcolors & 0x0f0)
|
||||||
SetConsoleTextAttribute(handle, attr)
|
SetConsoleTextAttribute(handle, attr)
|
||||||
file.write(text)
|
file.write(text)
|
||||||
SetConsoleTextAttribute(handle, FOREGROUND_WHITE)
|
SetConsoleTextAttribute(handle, oldcolors)
|
||||||
else:
|
else:
|
||||||
file.write(text)
|
file.write(text)
|
||||||
|
|
||||||
|
@ -183,12 +185,12 @@ class TerminalWriter(object):
|
||||||
class Win32ConsoleWriter(TerminalWriter):
|
class Win32ConsoleWriter(TerminalWriter):
|
||||||
def write(self, s, **kw):
|
def write(self, s, **kw):
|
||||||
if s:
|
if s:
|
||||||
s = self._getbytestring(s)
|
oldcolors = None
|
||||||
if self.hasmarkup:
|
|
||||||
handle = GetStdHandle(STD_OUTPUT_HANDLE)
|
|
||||||
|
|
||||||
if self.hasmarkup and kw:
|
if self.hasmarkup and kw:
|
||||||
attr = 0
|
handle = GetStdHandle(STD_OUTPUT_HANDLE)
|
||||||
|
oldcolors = GetConsoleInfo(handle).wAttributes
|
||||||
|
default_bg = oldcolors & 0x00F0
|
||||||
|
attr = default_bg
|
||||||
if kw.pop('bold', False):
|
if kw.pop('bold', False):
|
||||||
attr |= FOREGROUND_INTENSITY
|
attr |= FOREGROUND_INTENSITY
|
||||||
|
|
||||||
|
@ -199,13 +201,13 @@ class Win32ConsoleWriter(TerminalWriter):
|
||||||
elif kw.pop('green', False):
|
elif kw.pop('green', False):
|
||||||
attr |= FOREGROUND_GREEN
|
attr |= FOREGROUND_GREEN
|
||||||
else:
|
else:
|
||||||
attr |= FOREGROUND_WHITE
|
attr |= FOREGROUND_BLACK # (oldcolors & 0x0007)
|
||||||
|
|
||||||
SetConsoleTextAttribute(handle, attr)
|
SetConsoleTextAttribute(handle, attr)
|
||||||
self._file.write(s)
|
self._file.write(s)
|
||||||
self._file.flush()
|
self._file.flush()
|
||||||
if self.hasmarkup:
|
if oldcolors:
|
||||||
SetConsoleTextAttribute(handle, FOREGROUND_WHITE)
|
SetConsoleTextAttribute(handle, oldcolors)
|
||||||
|
|
||||||
def line(self, s="", **kw):
|
def line(self, s="", **kw):
|
||||||
self.write(s+"\n", **kw)
|
self.write(s+"\n", **kw)
|
||||||
|
@ -232,46 +234,50 @@ if win32_and_ctypes:
|
||||||
# ctypes access to the Windows console
|
# ctypes access to the Windows console
|
||||||
STD_OUTPUT_HANDLE = -11
|
STD_OUTPUT_HANDLE = -11
|
||||||
STD_ERROR_HANDLE = -12
|
STD_ERROR_HANDLE = -12
|
||||||
|
FOREGROUND_BLACK = 0x0000 # black text
|
||||||
FOREGROUND_BLUE = 0x0001 # text color contains blue.
|
FOREGROUND_BLUE = 0x0001 # text color contains blue.
|
||||||
FOREGROUND_GREEN = 0x0002 # text color contains green.
|
FOREGROUND_GREEN = 0x0002 # text color contains green.
|
||||||
FOREGROUND_RED = 0x0004 # text color contains red.
|
FOREGROUND_RED = 0x0004 # text color contains red.
|
||||||
FOREGROUND_WHITE = 0x0007
|
FOREGROUND_WHITE = 0x0007
|
||||||
FOREGROUND_INTENSITY = 0x0008 # text color is intensified.
|
FOREGROUND_INTENSITY = 0x0008 # text color is intensified.
|
||||||
|
BACKGROUND_BLACK = 0x0000 # background color black
|
||||||
BACKGROUND_BLUE = 0x0010 # background color contains blue.
|
BACKGROUND_BLUE = 0x0010 # background color contains blue.
|
||||||
BACKGROUND_GREEN = 0x0020 # background color contains green.
|
BACKGROUND_GREEN = 0x0020 # background color contains green.
|
||||||
BACKGROUND_RED = 0x0040 # background color contains red.
|
BACKGROUND_RED = 0x0040 # background color contains red.
|
||||||
BACKGROUND_WHITE = 0x0070
|
BACKGROUND_WHITE = 0x0070
|
||||||
BACKGROUND_INTENSITY = 0x0080 # background color is intensified.
|
BACKGROUND_INTENSITY = 0x0080 # background color is intensified.
|
||||||
|
|
||||||
|
SHORT = ctypes.c_short
|
||||||
|
class COORD(ctypes.Structure):
|
||||||
|
_fields_ = [('X', SHORT),
|
||||||
|
('Y', SHORT)]
|
||||||
|
class SMALL_RECT(ctypes.Structure):
|
||||||
|
_fields_ = [('Left', SHORT),
|
||||||
|
('Top', SHORT),
|
||||||
|
('Right', SHORT),
|
||||||
|
('Bottom', SHORT)]
|
||||||
|
class CONSOLE_SCREEN_BUFFER_INFO(ctypes.Structure):
|
||||||
|
_fields_ = [('dwSize', COORD),
|
||||||
|
('dwCursorPosition', COORD),
|
||||||
|
('wAttributes', wintypes.WORD),
|
||||||
|
('srWindow', SMALL_RECT),
|
||||||
|
('dwMaximumWindowSize', COORD)]
|
||||||
|
|
||||||
def GetStdHandle(kind):
|
def GetStdHandle(kind):
|
||||||
return ctypes.windll.kernel32.GetStdHandle(kind)
|
return ctypes.windll.kernel32.GetStdHandle(kind)
|
||||||
|
|
||||||
def SetConsoleTextAttribute(handle, attr):
|
SetConsoleTextAttribute = \
|
||||||
ctypes.windll.kernel32.SetConsoleTextAttribute(
|
ctypes.windll.kernel32.SetConsoleTextAttribute
|
||||||
handle, attr)
|
|
||||||
|
def GetConsoleInfo(handle):
|
||||||
|
info = CONSOLE_SCREEN_BUFFER_INFO()
|
||||||
|
ctypes.windll.kernel32.GetConsoleScreenBufferInfo(\
|
||||||
|
handle, ctypes.byref(info))
|
||||||
|
return info
|
||||||
|
|
||||||
def _getdimensions():
|
def _getdimensions():
|
||||||
|
|
||||||
SHORT = ctypes.c_short
|
|
||||||
class COORD(ctypes.Structure):
|
|
||||||
_fields_ = [('X', SHORT),
|
|
||||||
('Y', SHORT)]
|
|
||||||
class SMALL_RECT(ctypes.Structure):
|
|
||||||
_fields_ = [('Left', SHORT),
|
|
||||||
('Top', SHORT),
|
|
||||||
('Right', SHORT),
|
|
||||||
('Bottom', SHORT)]
|
|
||||||
class CONSOLE_SCREEN_BUFFER_INFO(ctypes.Structure):
|
|
||||||
_fields_ = [('dwSize', COORD),
|
|
||||||
('dwCursorPosition', COORD),
|
|
||||||
('wAttributes', wintypes.WORD),
|
|
||||||
('srWindow', SMALL_RECT),
|
|
||||||
('dwMaximumWindowSize', COORD)]
|
|
||||||
STD_OUTPUT_HANDLE = -11
|
|
||||||
handle = GetStdHandle(STD_OUTPUT_HANDLE)
|
handle = GetStdHandle(STD_OUTPUT_HANDLE)
|
||||||
info = CONSOLE_SCREEN_BUFFER_INFO()
|
info = GetConsoleInfo(handle)
|
||||||
ctypes.windll.kernel32.GetConsoleScreenBufferInfo(
|
|
||||||
handle, ctypes.byref(info))
|
|
||||||
# Substract one from the width, otherwise the cursor wraps
|
# Substract one from the width, otherwise the cursor wraps
|
||||||
# and the ending \n causes an empty line to display.
|
# and the ending \n causes an empty line to display.
|
||||||
return info.dwSize.Y, info.dwSize.X - 1
|
return info.dwSize.Y, info.dwSize.X - 1
|
||||||
|
|
Loading…
Reference in New Issue