tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

terminal_win.py (2794B)


      1 """
      2 From Andre Burgaud's Blog, from the CTypes Wiki:
      3 http://www.burgaud.com/bring-colors-to-the-windows-console-with-python/
      4 
      5 Colors text in console mode application (win32).
      6 Uses ctypes and Win32 methods SetConsoleTextAttribute and
      7 GetConsoleScreenBufferInfo.
      8 
      9 $Id: color_console.py 534 2009-05-10 04:00:59Z andre $
     10 """
     11 
     12 from ctypes import Structure, byref, c_short, c_ushort, windll
     13 
     14 SHORT = c_short
     15 WORD = c_ushort
     16 
     17 
     18 class COORD(Structure):
     19    """struct in wincon.h."""
     20 
     21    _fields_ = [("X", SHORT), ("Y", SHORT)]
     22 
     23 
     24 class SMALL_RECT(Structure):
     25    """struct in wincon.h."""
     26 
     27    _fields_ = [("Left", SHORT), ("Top", SHORT), ("Right", SHORT), ("Bottom", SHORT)]
     28 
     29 
     30 class CONSOLE_SCREEN_BUFFER_INFO(Structure):
     31    """struct in wincon.h."""
     32 
     33    _fields_ = [
     34        ("dwSize", COORD),
     35        ("dwCursorPosition", COORD),
     36        ("wAttributes", WORD),
     37        ("srWindow", SMALL_RECT),
     38        ("dwMaximumWindowSize", COORD),
     39    ]
     40 
     41 
     42 # winbase.h
     43 STD_INPUT_HANDLE = -10
     44 STD_OUTPUT_HANDLE = -11
     45 STD_ERROR_HANDLE = -12
     46 
     47 # wincon.h
     48 FOREGROUND_BLACK = 0x0000
     49 FOREGROUND_BLUE = 0x0001
     50 FOREGROUND_GREEN = 0x0002
     51 FOREGROUND_CYAN = 0x0003
     52 FOREGROUND_RED = 0x0004
     53 FOREGROUND_MAGENTA = 0x0005
     54 FOREGROUND_YELLOW = 0x0006
     55 FOREGROUND_GREY = 0x0007
     56 FOREGROUND_INTENSITY = 0x0008  # foreground color is intensified.
     57 
     58 BACKGROUND_BLACK = 0x0000
     59 BACKGROUND_BLUE = 0x0010
     60 BACKGROUND_GREEN = 0x0020
     61 BACKGROUND_CYAN = 0x0030
     62 BACKGROUND_RED = 0x0040
     63 BACKGROUND_MAGENTA = 0x0050
     64 BACKGROUND_YELLOW = 0x0060
     65 BACKGROUND_GREY = 0x0070
     66 BACKGROUND_INTENSITY = 0x0080  # background color is intensified.
     67 
     68 stdout_handle = windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
     69 SetConsoleTextAttribute = windll.kernel32.SetConsoleTextAttribute
     70 GetConsoleScreenBufferInfo = windll.kernel32.GetConsoleScreenBufferInfo
     71 
     72 
     73 def get_text_attr():
     74    csbi = CONSOLE_SCREEN_BUFFER_INFO()
     75    GetConsoleScreenBufferInfo(stdout_handle, byref(csbi))
     76    return csbi.wAttributes
     77 
     78 
     79 DEFAULT_COLORS = get_text_attr()
     80 
     81 
     82 class Terminal:
     83    COLOR = {
     84        "black": 0x0000,
     85        "blue": 0x0001,
     86        "green": 0x0002,
     87        "cyan": 0x0003,
     88        "red": 0x0004,
     89        "magenta": 0x0005,
     90        "yellow": 0x0006,
     91        "gray": 0x0007,
     92    }
     93    BRIGHT_INTENSITY = 0x0008
     94    BACKGROUND_SHIFT = 4
     95 
     96    @classmethod
     97    def set_color(cls, color):
     98        """
     99        color: str - color definition string
    100        """
    101        color_code = 0
    102        if color.startswith("bright"):
    103            color_code |= cls.BRIGHT_INTENSITY
    104            color = color[len("bright") :]
    105        color_code |= Terminal.COLOR[color]
    106        SetConsoleTextAttribute(stdout_handle, color_code)
    107 
    108    @classmethod
    109    def reset_color(cls):
    110        SetConsoleTextAttribute(stdout_handle, DEFAULT_COLORS)
    111 
    112    @classmethod
    113    def clear_right(cls):
    114        pass