text
stringlengths 1
93.6k
|
|---|
ctypes.windll.kernel32.SetConsoleWindowInfo(stdout_handle, True, byref(info))
|
def scroll_to_quarter(line):
|
"""Scroll up so that the specified line is at least 25% of the screen deep"""
|
lines = line - get_viewport()[1]
|
viewport_height = get_viewport()[3] - get_viewport()[1]
|
if lines < viewport_height / 4:
|
scroll_buffer(lines - viewport_height / 4)
|
def read_input():
|
"""Read one input event from the console input buffer"""
|
while True:
|
record = stdin_handle.ReadConsoleInput(1)[0]
|
if record.EventType == KEY_EVENT and record.KeyDown:
|
return record
|
def write_input(key_code, char, control_state):
|
"""Emulate a key press with the given key code and control key mask"""
|
record = PyINPUT_RECORDType(KEY_EVENT)
|
record.KeyDown = True
|
record.VirtualKeyCode = key_code
|
record.Char = char
|
record.ControlKeyState = control_state
|
stdin_handle.WriteConsoleInput([record])
|
def write_str(s):
|
"""
|
Output s to stdout, while processing the color sequences
|
"""
|
def write_with_sane_cursor(s):
|
"""
|
Under Win10, write() no longer advances the cursor to the next line after writing in the last column; so we
|
use a custom function to restore that behavior when needed
|
"""
|
buffer_width = get_buffer_size()[0]
|
cursor_before = get_cursor()[0]
|
sys.__stdout__.write(s)
|
sys.__stdout__.flush()
|
cursor_after = get_cursor()[0]
|
if (buffer_width > 0
|
and (cursor_before + len(s)) % buffer_width == 0
|
and cursor_after > 0):
|
# We have written over until the last column, but the cursor is NOT pushed to the next line; so we push it
|
# ourselves
|
sys.__stdout__.write(' \r')
|
i = 0
|
buf = ''
|
attr = get_text_attributes()
|
while i < len(s):
|
c = s[i]
|
if c == chr(27):
|
if buf:
|
# We have some characters, apply attributes and write them out
|
set_text_attributes(attr)
|
write_with_sane_cursor(buf)
|
buf = ''
|
# Process color commands to compute and set new attributes
|
target = s[i + 1]
|
command = s[i + 2]
|
component = s[i + 3]
|
i += 3
|
# Escape sequence format is [ESC][TGT][OP][COMP], where:
|
# * ESC is the Escape character: chr(27)
|
# * TGT is the target: 'F' for foreground, 'B' for background
|
# * OP is the operation: 'S' (set), 'C' (clear), 'T' (toggle) a component
|
# * COMP is the color component: 'R', 'G', 'B' or 'X' (bright)
|
if target == 'F':
|
name_prefix = 'FOREGROUND'
|
else:
|
name_prefix = 'BACKGROUND'
|
if component == 'R':
|
name_suffix = 'RED'
|
elif component == 'G':
|
name_suffix = 'GREEN'
|
elif component == 'B':
|
name_suffix = 'BLUE'
|
else:
|
name_suffix = 'BRIGHT'
|
if command == 'S':
|
operator = lambda x, y: x | y
|
elif command == 'C':
|
operator = lambda x, y: x & ~y
|
else:
|
operator = lambda x, y: x ^ y
|
import console
|
# We use the bit masks defined at the end of console.py by computing
|
# the name and accessing the module's dictionary (FOREGROUND_RED,
|
# BACKGROUND_BRIGHT etc)
|
bit_mask = console.__dict__[name_prefix + '_' + name_suffix]
|
attr = operator(attr, bit_mask)
|
else:
|
# Regular character, just append to the buffer
|
buf += c
|
i += 1
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.