2013-11-10 05:32:50 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# encoding: utf-8
|
|
|
|
|
|
|
|
"""
|
|
|
|
Emulate a vt100 terminal in cmd.exe
|
|
|
|
|
|
|
|
By wrapping sys.stdout / sys.stderr with Ansiterm,
|
|
|
|
the vt100 escape characters will be interpreted and
|
|
|
|
the equivalent actions will be performed with Win32
|
|
|
|
console commands.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2014-11-20 21:20:46 +01:00
|
|
|
import os, re, sys
|
2015-03-12 19:24:02 +01:00
|
|
|
from waflib import Utils
|
2014-01-05 01:39:32 +01:00
|
|
|
|
2015-03-12 19:24:02 +01:00
|
|
|
wlock = Utils.threading.Lock()
|
2013-11-10 05:32:50 +01:00
|
|
|
|
2011-09-10 11:13:51 +02:00
|
|
|
try:
|
2024-04-01 16:57:49 +02:00
|
|
|
from ctypes import Structure, WinDLL, c_short, c_ushort, c_ulong, c_int, byref, c_wchar, POINTER, c_long
|
2014-01-04 17:20:19 +01:00
|
|
|
except ImportError:
|
2014-01-05 11:23:49 +01:00
|
|
|
|
2014-01-05 01:39:32 +01:00
|
|
|
class AnsiTerm(object):
|
|
|
|
def __init__(self, stream):
|
|
|
|
self.stream = stream
|
2014-09-28 22:01:46 +02:00
|
|
|
try:
|
|
|
|
self.errors = self.stream.errors
|
|
|
|
except AttributeError:
|
|
|
|
pass # python 2.5
|
2014-01-05 01:39:32 +01:00
|
|
|
self.encoding = self.stream.encoding
|
|
|
|
|
|
|
|
def write(self, txt):
|
|
|
|
try:
|
|
|
|
wlock.acquire()
|
|
|
|
self.stream.write(txt)
|
|
|
|
self.stream.flush()
|
|
|
|
finally:
|
|
|
|
wlock.release()
|
|
|
|
|
|
|
|
def fileno(self):
|
|
|
|
return self.stream.fileno()
|
|
|
|
|
|
|
|
def flush(self):
|
|
|
|
self.stream.flush()
|
|
|
|
|
|
|
|
def isatty(self):
|
|
|
|
return self.stream.isatty()
|
2014-01-04 17:20:19 +01:00
|
|
|
else:
|
2014-01-04 23:20:17 +01:00
|
|
|
|
2011-09-10 11:13:51 +02:00
|
|
|
class COORD(Structure):
|
|
|
|
_fields_ = [("X", c_short), ("Y", c_short)]
|
|
|
|
|
|
|
|
class SMALL_RECT(Structure):
|
|
|
|
_fields_ = [("Left", c_short), ("Top", c_short), ("Right", c_short), ("Bottom", c_short)]
|
|
|
|
|
|
|
|
class CONSOLE_SCREEN_BUFFER_INFO(Structure):
|
2014-01-05 20:53:13 +01:00
|
|
|
_fields_ = [("Size", COORD), ("CursorPosition", COORD), ("Attributes", c_ushort), ("Window", SMALL_RECT), ("MaximumWindowSize", COORD)]
|
2011-09-10 11:13:51 +02:00
|
|
|
|
|
|
|
class CONSOLE_CURSOR_INFO(Structure):
|
2014-01-05 20:53:13 +01:00
|
|
|
_fields_ = [('dwSize', c_ulong), ('bVisible', c_int)]
|
2011-09-10 11:13:51 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
_type = unicode
|
2012-02-11 13:43:47 +01:00
|
|
|
except NameError:
|
2011-09-10 11:13:51 +02:00
|
|
|
_type = str
|
|
|
|
|
|
|
|
to_int = lambda number, default: number and int(number) or default
|
|
|
|
|
|
|
|
STD_OUTPUT_HANDLE = -11
|
|
|
|
STD_ERROR_HANDLE = -12
|
|
|
|
|
2024-04-01 16:57:49 +02:00
|
|
|
kernel32 = WinDLL('kernel32')
|
|
|
|
kernel32.GetStdHandle.argtypes = [c_ulong]
|
|
|
|
kernel32.GetStdHandle.restype = c_ulong
|
|
|
|
kernel32.GetConsoleScreenBufferInfo.argtypes = [c_ulong, POINTER(CONSOLE_SCREEN_BUFFER_INFO)]
|
|
|
|
kernel32.GetConsoleScreenBufferInfo.restype = c_long
|
|
|
|
kernel32.SetConsoleTextAttribute.argtypes = [c_ulong, c_ushort]
|
|
|
|
kernel32.SetConsoleTextAttribute.restype = c_long
|
|
|
|
kernel32.FillConsoleOutputCharacterW.argtypes = [c_ulong, c_wchar, c_ulong, POINTER(COORD), POINTER(c_ulong)]
|
|
|
|
kernel32.FillConsoleOutputCharacterW.restype = c_long
|
|
|
|
kernel32.FillConsoleOutputAttribute.argtypes = [c_ulong, c_ushort, c_ulong, POINTER(COORD), POINTER(c_ulong) ]
|
|
|
|
kernel32.FillConsoleOutputAttribute.restype = c_long
|
|
|
|
kernel32.SetConsoleCursorPosition.argtypes = [c_ulong, POINTER(COORD) ]
|
|
|
|
kernel32.SetConsoleCursorPosition.restype = c_long
|
|
|
|
kernel32.SetConsoleCursorInfo.argtypes = [c_ulong, POINTER(CONSOLE_CURSOR_INFO)]
|
|
|
|
kernel32.SetConsoleCursorInfo.restype = c_long
|
2014-01-05 21:30:52 +01:00
|
|
|
|
2011-09-10 11:13:51 +02:00
|
|
|
class AnsiTerm(object):
|
|
|
|
"""
|
2014-03-14 21:47:19 +01:00
|
|
|
emulate a vt100 terminal in cmd.exe
|
2011-09-10 11:13:51 +02:00
|
|
|
"""
|
2014-01-04 23:20:17 +01:00
|
|
|
def __init__(self, s):
|
|
|
|
self.stream = s
|
2014-09-28 22:01:46 +02:00
|
|
|
try:
|
|
|
|
self.errors = s.errors
|
|
|
|
except AttributeError:
|
|
|
|
pass # python2.5
|
2014-01-04 23:20:17 +01:00
|
|
|
self.encoding = s.encoding
|
2014-01-04 17:20:19 +01:00
|
|
|
self.cursor_history = []
|
|
|
|
|
2014-01-04 23:20:17 +01:00
|
|
|
handle = (s.fileno() == 2) and STD_ERROR_HANDLE or STD_OUTPUT_HANDLE
|
2024-04-01 16:57:49 +02:00
|
|
|
self.hconsole = kernel32.GetStdHandle(handle)
|
2014-01-04 17:20:19 +01:00
|
|
|
|
|
|
|
self._sbinfo = CONSOLE_SCREEN_BUFFER_INFO()
|
|
|
|
|
|
|
|
self._csinfo = CONSOLE_CURSOR_INFO()
|
2024-04-01 16:57:49 +02:00
|
|
|
kernel32.GetConsoleCursorInfo(self.hconsole, byref(self._csinfo))
|
2014-01-04 17:20:19 +01:00
|
|
|
|
|
|
|
# just to double check that the console is usable
|
|
|
|
self._orig_sbinfo = CONSOLE_SCREEN_BUFFER_INFO()
|
2024-04-01 16:57:49 +02:00
|
|
|
r = kernel32.GetConsoleScreenBufferInfo(self.hconsole, byref(self._orig_sbinfo))
|
2014-01-04 17:20:19 +01:00
|
|
|
self._isatty = r == 1
|
2011-09-10 11:13:51 +02:00
|
|
|
|
|
|
|
def screen_buffer_info(self):
|
2014-01-04 23:20:17 +01:00
|
|
|
"""
|
|
|
|
Updates self._sbinfo and returns it
|
|
|
|
"""
|
2024-04-01 16:57:49 +02:00
|
|
|
kernel32.GetConsoleScreenBufferInfo(self.hconsole, byref(self._sbinfo))
|
2014-01-04 17:20:19 +01:00
|
|
|
return self._sbinfo
|
2011-09-10 11:13:51 +02:00
|
|
|
|
|
|
|
def clear_line(self, param):
|
|
|
|
mode = param and int(param) or 0
|
|
|
|
sbinfo = self.screen_buffer_info()
|
2017-02-16 07:03:43 +01:00
|
|
|
if mode == 1: # Clear from beginning of line to cursor position
|
2011-09-10 11:13:51 +02:00
|
|
|
line_start = COORD(0, sbinfo.CursorPosition.Y)
|
|
|
|
line_length = sbinfo.Size.X
|
|
|
|
elif mode == 2: # Clear entire line
|
|
|
|
line_start = COORD(sbinfo.CursorPosition.X, sbinfo.CursorPosition.Y)
|
|
|
|
line_length = sbinfo.Size.X - sbinfo.CursorPosition.X
|
|
|
|
else: # Clear from cursor position to end of line
|
|
|
|
line_start = sbinfo.CursorPosition
|
|
|
|
line_length = sbinfo.Size.X - sbinfo.CursorPosition.X
|
2014-01-06 01:14:21 +01:00
|
|
|
chars_written = c_ulong()
|
2024-04-01 16:57:49 +02:00
|
|
|
kernel32.FillConsoleOutputCharacterW(self.hconsole, c_wchar(' '), line_length, line_start, byref(chars_written))
|
|
|
|
kernel32.FillConsoleOutputAttribute(self.hconsole, sbinfo.Attributes, line_length, line_start, byref(chars_written))
|
2011-09-10 11:13:51 +02:00
|
|
|
|
|
|
|
def clear_screen(self, param):
|
|
|
|
mode = to_int(param, 0)
|
|
|
|
sbinfo = self.screen_buffer_info()
|
2017-02-16 07:03:43 +01:00
|
|
|
if mode == 1: # Clear from beginning of screen to cursor position
|
2011-09-10 11:13:51 +02:00
|
|
|
clear_start = COORD(0, 0)
|
|
|
|
clear_length = sbinfo.CursorPosition.X * sbinfo.CursorPosition.Y
|
|
|
|
elif mode == 2: # Clear entire screen and return cursor to home
|
|
|
|
clear_start = COORD(0, 0)
|
|
|
|
clear_length = sbinfo.Size.X * sbinfo.Size.Y
|
2024-04-01 16:57:49 +02:00
|
|
|
kernel32.SetConsoleCursorPosition(self.hconsole, clear_start)
|
2011-09-10 11:13:51 +02:00
|
|
|
else: # Clear from cursor position to end of screen
|
|
|
|
clear_start = sbinfo.CursorPosition
|
|
|
|
clear_length = ((sbinfo.Size.X - sbinfo.CursorPosition.X) + sbinfo.Size.X * (sbinfo.Size.Y - sbinfo.CursorPosition.Y))
|
2014-01-06 01:14:21 +01:00
|
|
|
chars_written = c_ulong()
|
2024-04-01 16:57:49 +02:00
|
|
|
kernel32.FillConsoleOutputCharacterW(self.hconsole, c_wchar(' '), clear_length, clear_start, byref(chars_written))
|
|
|
|
kernel32.FillConsoleOutputAttribute(self.hconsole, sbinfo.Attributes, clear_length, clear_start, byref(chars_written))
|
2011-09-10 11:13:51 +02:00
|
|
|
|
|
|
|
def push_cursor(self, param):
|
|
|
|
sbinfo = self.screen_buffer_info()
|
|
|
|
self.cursor_history.append(sbinfo.CursorPosition)
|
|
|
|
|
|
|
|
def pop_cursor(self, param):
|
|
|
|
if self.cursor_history:
|
|
|
|
old_pos = self.cursor_history.pop()
|
2024-04-01 16:57:49 +02:00
|
|
|
kernel32.SetConsoleCursorPosition(self.hconsole, old_pos)
|
2011-09-10 11:13:51 +02:00
|
|
|
|
|
|
|
def set_cursor(self, param):
|
|
|
|
y, sep, x = param.partition(';')
|
|
|
|
x = to_int(x, 1) - 1
|
|
|
|
y = to_int(y, 1) - 1
|
|
|
|
sbinfo = self.screen_buffer_info()
|
|
|
|
new_pos = COORD(
|
|
|
|
min(max(0, x), sbinfo.Size.X),
|
|
|
|
min(max(0, y), sbinfo.Size.Y)
|
|
|
|
)
|
2024-04-01 16:57:49 +02:00
|
|
|
kernel32.SetConsoleCursorPosition(self.hconsole, new_pos)
|
2011-09-10 11:13:51 +02:00
|
|
|
|
|
|
|
def set_column(self, param):
|
|
|
|
x = to_int(param, 1) - 1
|
|
|
|
sbinfo = self.screen_buffer_info()
|
|
|
|
new_pos = COORD(
|
|
|
|
min(max(0, x), sbinfo.Size.X),
|
|
|
|
sbinfo.CursorPosition.Y
|
|
|
|
)
|
2024-04-01 16:57:49 +02:00
|
|
|
kernel32.SetConsoleCursorPosition(self.hconsole, new_pos)
|
2011-09-10 11:13:51 +02:00
|
|
|
|
|
|
|
def move_cursor(self, x_offset=0, y_offset=0):
|
|
|
|
sbinfo = self.screen_buffer_info()
|
|
|
|
new_pos = COORD(
|
|
|
|
min(max(0, sbinfo.CursorPosition.X + x_offset), sbinfo.Size.X),
|
|
|
|
min(max(0, sbinfo.CursorPosition.Y + y_offset), sbinfo.Size.Y)
|
|
|
|
)
|
2024-04-01 16:57:49 +02:00
|
|
|
kernel32.SetConsoleCursorPosition(self.hconsole, new_pos)
|
2011-09-10 11:13:51 +02:00
|
|
|
|
|
|
|
def move_up(self, param):
|
|
|
|
self.move_cursor(y_offset = -to_int(param, 1))
|
|
|
|
|
|
|
|
def move_down(self, param):
|
|
|
|
self.move_cursor(y_offset = to_int(param, 1))
|
|
|
|
|
|
|
|
def move_left(self, param):
|
|
|
|
self.move_cursor(x_offset = -to_int(param, 1))
|
|
|
|
|
|
|
|
def move_right(self, param):
|
|
|
|
self.move_cursor(x_offset = to_int(param, 1))
|
|
|
|
|
|
|
|
def next_line(self, param):
|
|
|
|
sbinfo = self.screen_buffer_info()
|
|
|
|
self.move_cursor(
|
|
|
|
x_offset = -sbinfo.CursorPosition.X,
|
|
|
|
y_offset = to_int(param, 1)
|
|
|
|
)
|
|
|
|
|
|
|
|
def prev_line(self, param):
|
|
|
|
sbinfo = self.screen_buffer_info()
|
|
|
|
self.move_cursor(
|
|
|
|
x_offset = -sbinfo.CursorPosition.X,
|
|
|
|
y_offset = -to_int(param, 1)
|
|
|
|
)
|
|
|
|
|
|
|
|
def rgb2bgr(self, c):
|
|
|
|
return ((c&1) << 2) | (c&2) | ((c&4)>>2)
|
|
|
|
|
|
|
|
def set_color(self, param):
|
|
|
|
cols = param.split(';')
|
2014-01-04 17:20:19 +01:00
|
|
|
sbinfo = self.screen_buffer_info()
|
2011-09-10 11:13:51 +02:00
|
|
|
attr = sbinfo.Attributes
|
|
|
|
for c in cols:
|
2014-01-04 23:20:17 +01:00
|
|
|
c = to_int(c, 0)
|
2014-01-05 21:47:21 +01:00
|
|
|
if 29 < c < 38: # fgcolor
|
|
|
|
attr = (attr & 0xfff0) | self.rgb2bgr(c - 30)
|
|
|
|
elif 39 < c < 48: # bgcolor
|
|
|
|
attr = (attr & 0xff0f) | (self.rgb2bgr(c - 40) << 4)
|
2011-09-10 11:13:51 +02:00
|
|
|
elif c == 0: # reset
|
2014-01-04 17:20:19 +01:00
|
|
|
attr = self._orig_sbinfo.Attributes
|
2011-09-10 11:13:51 +02:00
|
|
|
elif c == 1: # strong
|
|
|
|
attr |= 0x08
|
|
|
|
elif c == 4: # blink not available -> bg intensity
|
|
|
|
attr |= 0x80
|
|
|
|
elif c == 7: # negative
|
|
|
|
attr = (attr & 0xff88) | ((attr & 0x70) >> 4) | ((attr & 0x07) << 4)
|
2014-01-05 20:53:13 +01:00
|
|
|
|
2024-04-01 16:57:49 +02:00
|
|
|
kernel32.SetConsoleTextAttribute(self.hconsole, attr)
|
2011-09-10 11:13:51 +02:00
|
|
|
|
|
|
|
def show_cursor(self,param):
|
2014-01-04 17:20:19 +01:00
|
|
|
self._csinfo.bVisible = 1
|
2024-04-01 16:57:49 +02:00
|
|
|
kernel32.SetConsoleCursorInfo(self.hconsole, byref(self._csinfo))
|
2011-09-10 11:13:51 +02:00
|
|
|
|
|
|
|
def hide_cursor(self,param):
|
2014-01-04 17:20:19 +01:00
|
|
|
self._csinfo.bVisible = 0
|
2024-04-01 16:57:49 +02:00
|
|
|
kernel32.SetConsoleCursorInfo(self.hconsole, byref(self._csinfo))
|
2011-09-10 11:13:51 +02:00
|
|
|
|
|
|
|
ansi_command_table = {
|
|
|
|
'A': move_up,
|
|
|
|
'B': move_down,
|
|
|
|
'C': move_right,
|
|
|
|
'D': move_left,
|
|
|
|
'E': next_line,
|
|
|
|
'F': prev_line,
|
|
|
|
'G': set_column,
|
|
|
|
'H': set_cursor,
|
|
|
|
'f': set_cursor,
|
|
|
|
'J': clear_screen,
|
|
|
|
'K': clear_line,
|
|
|
|
'h': show_cursor,
|
|
|
|
'l': hide_cursor,
|
|
|
|
'm': set_color,
|
|
|
|
's': push_cursor,
|
|
|
|
'u': pop_cursor,
|
|
|
|
}
|
|
|
|
# Match either the escape sequence or text not containing escape sequence
|
2019-01-05 12:02:42 +01:00
|
|
|
ansi_tokens = re.compile(r'(?:\x1b\[([0-9?;]*)([a-zA-Z])|([^\x1b]+))')
|
2011-09-10 11:13:51 +02:00
|
|
|
def write(self, text):
|
|
|
|
try:
|
|
|
|
wlock.acquire()
|
2014-01-04 23:20:17 +01:00
|
|
|
if self._isatty:
|
|
|
|
for param, cmd, txt in self.ansi_tokens.findall(text):
|
|
|
|
if cmd:
|
2014-01-04 17:20:19 +01:00
|
|
|
cmd_func = self.ansi_command_table.get(cmd)
|
|
|
|
if cmd_func:
|
|
|
|
cmd_func(self, param)
|
2014-01-04 23:20:17 +01:00
|
|
|
else:
|
2014-03-14 21:47:19 +01:00
|
|
|
self.writeconsole(txt)
|
2014-01-04 23:20:17 +01:00
|
|
|
else:
|
2014-01-05 01:39:32 +01:00
|
|
|
# no support for colors in the console, just output the text:
|
|
|
|
# eclipse or msys may be able to interpret the escape sequences
|
2014-01-04 23:20:17 +01:00
|
|
|
self.stream.write(text)
|
2011-09-10 11:13:51 +02:00
|
|
|
finally:
|
|
|
|
wlock.release()
|
|
|
|
|
2014-03-14 21:47:19 +01:00
|
|
|
def writeconsole(self, txt):
|
2015-05-05 07:33:47 +02:00
|
|
|
chars_written = c_ulong()
|
2024-04-01 16:57:49 +02:00
|
|
|
writeconsole = kernel32.WriteConsoleA
|
2014-03-14 21:47:19 +01:00
|
|
|
if isinstance(txt, _type):
|
2024-04-01 16:57:49 +02:00
|
|
|
writeconsole = kernel32.WriteConsoleW
|
2014-03-14 21:47:19 +01:00
|
|
|
|
|
|
|
# MSDN says that there is a shared buffer of 64 KB for the console
|
|
|
|
# writes. Attempt to not get ERROR_NOT_ENOUGH_MEMORY, see waf issue #746
|
|
|
|
done = 0
|
|
|
|
todo = len(txt)
|
|
|
|
chunk = 32<<10
|
|
|
|
while todo != 0:
|
|
|
|
doing = min(chunk, todo)
|
|
|
|
buf = txt[done:done+doing]
|
|
|
|
r = writeconsole(self.hconsole, buf, doing, byref(chars_written), None)
|
|
|
|
if r == 0:
|
|
|
|
chunk >>= 1
|
|
|
|
continue
|
|
|
|
done += doing
|
|
|
|
todo -= doing
|
|
|
|
|
|
|
|
|
2014-01-01 15:11:32 +01:00
|
|
|
def fileno(self):
|
|
|
|
return self.stream.fileno()
|
|
|
|
|
2011-09-10 11:13:51 +02:00
|
|
|
def flush(self):
|
2014-03-14 21:47:19 +01:00
|
|
|
pass
|
2011-09-10 11:13:51 +02:00
|
|
|
|
|
|
|
def isatty(self):
|
2014-01-04 17:20:19 +01:00
|
|
|
return self._isatty
|
2011-09-10 11:13:51 +02:00
|
|
|
|
2014-01-05 11:23:49 +01:00
|
|
|
if sys.stdout.isatty() or sys.stderr.isatty():
|
|
|
|
handle = sys.stdout.isatty() and STD_OUTPUT_HANDLE or STD_ERROR_HANDLE
|
2024-04-01 16:57:49 +02:00
|
|
|
console = kernel32.GetStdHandle(handle)
|
2014-01-05 11:23:49 +01:00
|
|
|
sbinfo = CONSOLE_SCREEN_BUFFER_INFO()
|
|
|
|
def get_term_cols():
|
2024-04-01 16:57:49 +02:00
|
|
|
kernel32.GetConsoleScreenBufferInfo(console, byref(sbinfo))
|
2015-12-25 17:50:13 +01:00
|
|
|
# Issue 1401 - the progress bar cannot reach the last character
|
2014-01-25 20:26:11 +01:00
|
|
|
return sbinfo.Size.X - 1
|
2014-01-05 11:23:49 +01:00
|
|
|
|
|
|
|
# just try and see
|
|
|
|
try:
|
|
|
|
import struct, fcntl, termios
|
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
else:
|
2014-11-20 21:20:46 +01:00
|
|
|
if (sys.stdout.isatty() or sys.stderr.isatty()) and os.environ.get('TERM', '') not in ('dumb', 'emacs'):
|
2014-01-05 11:23:49 +01:00
|
|
|
FD = sys.stdout.isatty() and sys.stdout.fileno() or sys.stderr.fileno()
|
|
|
|
def fun():
|
|
|
|
return struct.unpack("HHHH", fcntl.ioctl(FD, termios.TIOCGWINSZ, struct.pack("HHHH", 0, 0, 0, 0)))[1]
|
|
|
|
try:
|
|
|
|
fun()
|
|
|
|
except Exception as e:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
get_term_cols = fun
|
2011-09-10 11:13:51 +02:00
|
|
|
|