js/src/tests/lib/terminal_win.py

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/tests/lib/terminal_win.py	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,109 @@
     1.4 +"""
     1.5 +From Andre Burgaud's Blog, from the CTypes Wiki:
     1.6 +http://www.burgaud.com/bring-colors-to-the-windows-console-with-python/
     1.7 +
     1.8 +Colors text in console mode application (win32).
     1.9 +Uses ctypes and Win32 methods SetConsoleTextAttribute and
    1.10 +GetConsoleScreenBufferInfo.
    1.11 +
    1.12 +$Id: color_console.py 534 2009-05-10 04:00:59Z andre $
    1.13 +"""
    1.14 +
    1.15 +from ctypes import windll, Structure, c_short, c_ushort, byref
    1.16 +
    1.17 +SHORT = c_short
    1.18 +WORD = c_ushort
    1.19 +
    1.20 +class COORD(Structure):
    1.21 +  """struct in wincon.h."""
    1.22 +  _fields_ = [
    1.23 +    ("X", SHORT),
    1.24 +    ("Y", SHORT)]
    1.25 +
    1.26 +class SMALL_RECT(Structure):
    1.27 +  """struct in wincon.h."""
    1.28 +  _fields_ = [
    1.29 +    ("Left", SHORT),
    1.30 +    ("Top", SHORT),
    1.31 +    ("Right", SHORT),
    1.32 +    ("Bottom", SHORT)]
    1.33 +
    1.34 +class CONSOLE_SCREEN_BUFFER_INFO(Structure):
    1.35 +  """struct in wincon.h."""
    1.36 +  _fields_ = [
    1.37 +    ("dwSize", COORD),
    1.38 +    ("dwCursorPosition", COORD),
    1.39 +    ("wAttributes", WORD),
    1.40 +    ("srWindow", SMALL_RECT),
    1.41 +    ("dwMaximumWindowSize", COORD)]
    1.42 +
    1.43 +# winbase.h
    1.44 +STD_INPUT_HANDLE = -10
    1.45 +STD_OUTPUT_HANDLE = -11
    1.46 +STD_ERROR_HANDLE = -12
    1.47 +
    1.48 +# wincon.h
    1.49 +FOREGROUND_BLACK     = 0x0000
    1.50 +FOREGROUND_BLUE      = 0x0001
    1.51 +FOREGROUND_GREEN     = 0x0002
    1.52 +FOREGROUND_CYAN      = 0x0003
    1.53 +FOREGROUND_RED       = 0x0004
    1.54 +FOREGROUND_MAGENTA   = 0x0005
    1.55 +FOREGROUND_YELLOW    = 0x0006
    1.56 +FOREGROUND_GREY      = 0x0007
    1.57 +FOREGROUND_INTENSITY = 0x0008 # foreground color is intensified.
    1.58 +
    1.59 +BACKGROUND_BLACK     = 0x0000
    1.60 +BACKGROUND_BLUE      = 0x0010
    1.61 +BACKGROUND_GREEN     = 0x0020
    1.62 +BACKGROUND_CYAN      = 0x0030
    1.63 +BACKGROUND_RED       = 0x0040
    1.64 +BACKGROUND_MAGENTA   = 0x0050
    1.65 +BACKGROUND_YELLOW    = 0x0060
    1.66 +BACKGROUND_GREY      = 0x0070
    1.67 +BACKGROUND_INTENSITY = 0x0080 # background color is intensified.
    1.68 +
    1.69 +stdout_handle = windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
    1.70 +SetConsoleTextAttribute = windll.kernel32.SetConsoleTextAttribute
    1.71 +GetConsoleScreenBufferInfo = windll.kernel32.GetConsoleScreenBufferInfo
    1.72 +
    1.73 +def get_text_attr():
    1.74 +  csbi = CONSOLE_SCREEN_BUFFER_INFO()
    1.75 +  GetConsoleScreenBufferInfo(stdout_handle, byref(csbi))
    1.76 +  return csbi.wAttributes
    1.77 +
    1.78 +DEFAULT_COLORS = get_text_attr()
    1.79 +
    1.80 +class Terminal(object):
    1.81 +    COLOR = {
    1.82 +        'black': 0x0000,
    1.83 +        'blue': 0x0001,
    1.84 +        'green': 0x0002,
    1.85 +        'cyan': 0x0003,
    1.86 +        'red': 0x0004,
    1.87 +        'magenta': 0x0005,
    1.88 +        'yellow': 0x0006,
    1.89 +        'gray': 0x0007
    1.90 +    }
    1.91 +    BRIGHT_INTENSITY = 0x0008
    1.92 +    BACKGROUND_SHIFT = 4
    1.93 +
    1.94 +    @classmethod
    1.95 +    def set_color(cls, color):
    1.96 +        """
    1.97 +        color: str - color definition string
    1.98 +        """
    1.99 +        color_code = 0
   1.100 +        if color.startswith('bright'):
   1.101 +            color_code |= cls.BRIGHT_INTENSITY
   1.102 +            color = color[len('bright'):]
   1.103 +        color_code |= Terminal.COLOR[color]
   1.104 +        SetConsoleTextAttribute(stdout_handle, color_code)
   1.105 +
   1.106 +    @classmethod
   1.107 +    def reset_color(cls):
   1.108 +        SetConsoleTextAttribute(stdout_handle, DEFAULT_COLORS)
   1.109 +
   1.110 +    @classmethod
   1.111 +    def clear_right(cls):
   1.112 +        pass

mercurial