text
stringlengths
1
93.6k
def get_text_attributes():
"""Get the current foreground/background RGB components"""
buffer_info = CONSOLE_SCREEN_BUFFER_INFO()
ctypes.windll.kernel32.GetConsoleScreenBufferInfo(stdout_handle, pointer(buffer_info))
return buffer_info.attributes
def set_text_attributes(color):
"""Set foreground/background RGB components for the text to write"""
ctypes.windll.kernel32.SetConsoleTextAttribute(stdout_handle, color)
def get_buffer_attributes(x, y, n):
"""Get the fg/bg/attributes for the n chars in the buffer starting at (x, y)"""
colors = (n * WORD)()
coord = COORD(x, y)
read = DWORD(0)
sys.__stdout__.flush()
ctypes.windll.kernel32.ReadConsoleOutputAttribute(stdout_handle, colors, n, coord, pointer(read))
return colors
def set_buffer_attributes(x, y, colors):
"""Set the fg/bg attributes for the n chars in the the buffer starting at (x, y)"""
coord = COORD(x, y)
written = DWORD(0)
ctypes.windll.kernel32.WriteConsoleOutputAttribute(stdout_handle, colors, len(colors), coord, pointer(written))
def visual_bell():
"""Flash the screen for brief moment to notify the user"""
l, t, r, b = get_viewport()
count = (r - l + 1) * (b - t + 1)
colors = get_buffer_attributes(l, t, count)
reverted_colors = (count * WORD)(*tuple([c ^ BACKGROUND_BRIGHT for c in colors]))
set_buffer_attributes(l, t, reverted_colors)
time.sleep(0.15)
set_buffer_attributes(l, t, colors)
def set_console_title(title):
"""Set the title of the current console"""
ctypes.windll.kernel32.SetConsoleTitleA(title.encode(sys.stdout.encoding))
def move_cursor(x, y):
"""Move the cursor to the specified location"""
location = COORD(x, y)
ctypes.windll.kernel32.SetConsoleCursorPosition(stdout_handle, location)
def get_cursor():
"""Get the current cursor position"""
buffer_info = CONSOLE_SCREEN_BUFFER_INFO()
ctypes.windll.kernel32.GetConsoleScreenBufferInfo(stdout_handle, pointer(buffer_info))
return (buffer_info.cursorPosition.X, buffer_info.cursorPosition.Y)
def count_chars(start, end):
return (end[1] - start[1]) * get_buffer_size()[0] + (end[0] - start[0])
def erase_to(end):
from pycmd_public import color
to_erase = count_chars(get_cursor(), end)
sys.stdout.write(color.Fore.DEFAULT + color.Back.DEFAULT + ' ' * to_erase)
cursor_backward(to_erase)
def get_buffer_size():
"""Get the size of the text buffer"""
buffer_info = CONSOLE_SCREEN_BUFFER_INFO()
ctypes.windll.kernel32.GetConsoleScreenBufferInfo(stdout_handle, pointer(buffer_info))
return (buffer_info.size.X, buffer_info.size.Y)
def get_viewport():
"""Get the current viewport position"""
buffer_info = CONSOLE_SCREEN_BUFFER_INFO()
ctypes.windll.kernel32.GetConsoleScreenBufferInfo(stdout_handle, pointer(buffer_info))
return (buffer_info.window.Left, buffer_info.window.Top, buffer_info.window.Right, buffer_info.window.Bottom)
def set_cursor_attributes(size, visibility):
"""Set the cursor size and visibility"""
cursor_info = CONSOLE_CURSOR_INFO(size, visibility)
ctypes.windll.kernel32.SetConsoleCursorInfo(stdout_handle, pointer(cursor_info))
def cursor_backward(count):
"""Move cursor backward with the given number of positions"""
(x, y) = get_cursor()
while count > 0:
x -= 1
if x < 0:
y -= 1
(x, _) = get_buffer_size()
x -= 1
count -= 1
move_cursor(x, y)
def scroll_buffer(lines):
"""Scroll vertically with the given (positive or negative) number of lines"""
global scroll_mark
(l, t, r, b) = get_viewport()
(w, h) = get_buffer_size()
if t + lines < 0:
lines = -t # Scroll up to beginning
elif b + lines > h:
lines = h - b - 1 # Scroll down to end
if (lines < 0 and t >= lines or lines > 0 and b + lines <= h):
info = SMALL_RECT(l, t + lines, r, b + lines)