def write_char_with_color(char, fg_col, bg_col):
set_color(fg_col, bg_col)
if char == '\n':
fill_to_eol_with_bg_color() # insure bg_col covers rest of line
if is_windows and char != '\n':
cbuf = CONSOLE_SCREEN_BUFFER_INFO()
stdout_handle = ctypes.windll.kernel32.GetStdHandle(ctypes.c_ulong(-11))
ctypes.windll.kernel32.GetConsoleScreenBufferInfo(stdout_handle, ctypes.byref(cbuf))
cursor = cbuf.dwCursorPosition
# we only write on the left for status, so not touching cursor is fine
written = ctypes.c_uint(0)
char_attr = ctypes.c_uint16(cbuf.wAttributes)
ctypes.windll.kernel32.WriteConsoleOutputAttribute(stdout_handle,
ctypes.byref(char_attr),
1,
cursor,
ctypes.byref(written))
ctypes.windll.kernel32.WriteConsoleOutputCharacterA(stdout_handle,
ctypes.c_char_p(char),
1,
cursor,
ctypes.byref(written))
if cursor.X < cbuf.srWindow.Right - 1:
cursor.X += 1
ctypes.windll.kernel32.SetConsoleCursorPosition(stdout_handle, cursor)
else:
sys.stdout.write(char)
评论列表
文章目录