text
stringlengths
1
93.6k
# Apply the last attributes and write the remaining chars (if any)
set_text_attributes(attr)
if buf:
write_with_sane_cursor(buf)
def remove_escape_sequences(s):
"""
Remove color escape sequences from the given string
"""
from pycmd_public import color
escape_sequences_fore = [v for (k, v) in chain(color.Fore.__dict__.items(),
color.Back.__dict__.items())
if not k in ['__dict__', '__doc__', '__weakref__', '__module__']]
return reduce(lambda x, y: x.replace(y, ''),
escape_sequences_fore,
s)
def get_current_foreground():
"""Get the current foreground setting as a color string"""
color = ''
attr = get_text_attributes()
letters = ['B', 'G', 'R', 'X']
for i in range(4):
if attr & 1 << i:
color += chr(27) + 'FS' + letters[i]
else:
color += chr(27) + 'FC' + letters[i]
return color
def get_current_background():
"""Get the current background setting as a color string"""
color = ''
attr = get_text_attributes()
letters = ['B', 'G', 'R', 'X']
for i in range(4):
if attr & 1 << (i + 4):
color += chr(27) + 'BS' + letters[i]
else:
color += chr(27) + 'BC' + letters[i]
return color
def is_ctrl_pressed(record):
"""Check whether the Ctrl key is pressed"""
return record.ControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED) != 0
def is_alt_pressed(record):
"""Check whether the Alt key is pressed"""
return record.ControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED) != 0
def is_shift_pressed(record):
"""Check whether the Shift key is pressed"""
return record.ControlKeyState & SHIFT_PRESSED != 0
def is_control_only(record):
"""
Check whether this is a control-key-only press, i.e. just a modifier
key w/out an "actual" key
"""
return record.VirtualKeyCode in [16, 17, 18]
# Initialization
FOREGROUND_BLUE = 0x01
FOREGROUND_GREEN = 0x02
FOREGROUND_RED = 0x04
FOREGROUND_WHITE = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED
FOREGROUND_BRIGHT = 0x08
BACKGROUND_BLUE = 0x10
BACKGROUND_GREEN = 0x20
BACKGROUND_RED = 0x40
BACKGROUND_BRIGHT = 0x80
BACKGROUND_WHITE = BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED
stdin_handle = GetStdHandle(STD_INPUT_HANDLE)
stdout_handle = ctypes.windll.kernel32.GetStdHandle(-11)
class ColorOutputStream:
"""
We install a custom sys.stdout that handles our color sequences
Note that this requires sys.stdout be only imported _after_ console;
not doing so will bring the original stdout in the current scope!
"""
def write(self, str):
"""Dispatch printing to our enhanced write function"""
write_str(str)
def __getattr__(self, name):
return getattr(sys.__stdout__, name)
# Install our custom output stream
sys.stdout = ColorOutputStream()
# <FILESEP>
# Licensed to the Apache Software Foundation (ASF) under one