def initConsole(self, consout=None, window_size_x=80, window_size_y=25, buffer_size_x=80, buffer_size_y=16000): if not consout: consout = self.getConsoleOut() self.consin = win32console.GetStdHandle(win32console.STD_INPUT_HANDLE) rect = win32console.PySMALL_RECTType(0, 0, window_size_x - 1, window_size_y - 1) consout.SetConsoleWindowInfo(True, rect) size = win32console.PyCOORDType(buffer_size_x, buffer_size_y) consout.SetConsoleScreenBufferSize(size) pos = win32console.PyCOORDType(0, 0) # Use NUL as fill char because it displays as whitespace # (if we interact() with the child) consout.FillConsoleOutputCharacter(screenbufferfillchar, size.X * size.Y, pos) consinfo = consout.GetConsoleScreenBufferInfo() self.__consSize = consinfo['Size'] logger.info('self.__consSize: ' + str(self.__consSize)) self.startCursorPos = consinfo['CursorPosition']
def _db_pixel(self, pos, char, **kwargs): if not (0 <= pos[1] < self._surface._size[1] and 0 <= pos[0] < self._surface._size[0]): return if "fg" in kwargs: if kwargs["fg"] is not None: fg, self._surface._out[pos[1]][ pos[0]][0][0] = kwargs["fg"], kwargs["fg"] else: fg = self._surface._out[pos[1]][pos[0]][0][0] else: fg = 256 if "bg" in kwargs: if kwargs["bg"] is not None: bg, self._surface._out[pos[1]][ pos[0]][0][1] = kwargs["bg"], kwargs["bg"] else: bg = self._surface._out[pos[1]][pos[0]][0][1] else: bg = 256 if char is not None: self._surface.backbuffer.WriteConsoleOutputCharacter( char, win32console.PyCOORDType(*pos)) self._surface.backbuffer.WriteConsoleOutputAttribute( (fg, bg), win32console.PyCOORDType(*pos))
def clear(self, fg="white", bg="black"): x, y = self.get_size() self.outbuf.FillConsoleOutputCharacter(u" ", x * y, win32console.PyCOORDType(0, 0)) flags = Cursor.COLOR_NAMES[fg] | (Cursor.COLOR_NAMES[bg] << 4) self.outbuf.FillConsoleOutputAttribute(flags, x * y, win32console.PyCOORDType(0, 0)) self.outbuf.SetConsoleCursorPosition(win32console.PyCOORDType(0, 0))
def toggleFullscreen(): if isWindows(): global console if not isFullScreen(): console.SetConsoleDisplayMode(win32console.CONSOLE_FULLSCREEN_MODE, win32console.PyCOORDType(0, 0)) else: console.SetConsoleDisplayMode(win32console.CONSOLE_WINDOWED_MODE, win32console.PyCOORDType(0, 0)) elif isLinux(): raise NotImplementedError
def print_on_second_last_line(self, text, color): """ Prints a text on the second last line. This can be used to print a message above the command prompt. If the command prompt spans multiple lines there will be glitches. If the printed text spans multiple lines there will also be glitches (though this could be fixed). """ if platform.system() == 'Windows': # Windows <10 doesn't understand VT100 escape codes and the colorama # also doesn't support the specific escape codes we need so we use the # native Win32 API. info = self._stdout_buf.GetConsoleScreenBufferInfo() cursor_pos = info['CursorPosition'] scroll_rect = win32console.PySMALL_RECTType( Left=0, Top=1, Right=info['Window'].Right, Bottom=cursor_pos.Y - 1) scroll_dest = win32console.PyCOORDType(scroll_rect.Left, scroll_rect.Top - 1) self._stdout_buf.ScrollConsoleScreenBuffer( scroll_rect, scroll_rect, scroll_dest, # clipping rect is same as scroll rect u' ', Logger._Win32Colors[color] ) # fill with empty cells with the desired color attributes line_start = win32console.PyCOORDType(0, cursor_pos.Y - 1) self._stdout_buf.WriteConsoleOutputCharacter(text, line_start) else: # Assume we're in a terminal that interprets VT100 escape codes. # TODO: test on macOS # Escape character sequence: # ESC 7: store cursor position # ESC 1A: move cursor up by one # ESC 1S: scroll entire viewport by one # ESC 1L: insert 1 line at cursor position # (print text) # ESC 8: restore old cursor position self._print_lock.acquire() sys.stdout.write('\x1b7\x1b[1A\x1b[1S\x1b[1L') sys.stdout.write(Logger._VT100Colors[color] + text + Logger._VT100Colors[Logger.COLOR_DEFAULT]) sys.stdout.write('\x1b8') sys.stdout.flush() self._print_lock.release()
def clear(self): zero = win32console.PyCOORDType(X=0, Y=0) self.con_stdout.SetConsoleCursorPosition(zero) dct_info = self.con_stdout.GetConsoleScreenBufferInfo() size = dct_info['Size'] length = size.X * size.Y self.con_stdout.FillConsoleOutputCharacter(u' ', length, zero)
def _ClearScreen(self): """Clears the terminal/console screen.""" if self._have_ansi_support: # ANSI escape sequence to clear screen. self._output_writer.Write('\033[2J') # ANSI escape sequence to move cursor to top left. self._output_writer.Write('\033[H') elif win32console: # This version of Windows cmd.exe does not support ANSI escape codes, thus # instead we fill the console screen buffer with spaces. The downside of # this approach is an annoying flicker. top_left_coordinate = win32console.PyCOORDType(0, 0) screen_buffer = win32console.GetStdHandle( win32api.STD_OUTPUT_HANDLE) screen_buffer_information = screen_buffer.GetConsoleScreenBufferInfo( ) screen_buffer_attributes = screen_buffer_information['Attributes'] screen_buffer_size = screen_buffer_information['Size'] console_size = screen_buffer_size.X * screen_buffer_size.Y screen_buffer.FillConsoleOutputCharacter(' ', console_size, top_left_coordinate) screen_buffer.FillConsoleOutputAttribute(screen_buffer_attributes, console_size, top_left_coordinate) screen_buffer.SetConsoleCursorPosition(top_left_coordinate)
def _print_pos(self, x, y, text): if x > self.console_size[0] or y > self.console_size[1]: return -1 pos = win32console.PyCOORDType(x, y) with self._thread_lock: self.console_output.SetConsoleCursorPosition(pos) self.console_output.WriteConsole(text)
def _ClearScreen(self): """Clears the terminal/console screen.""" if not win32console: # ANSI escape sequence to clear screen. self._output_writer.Write(b'\033[2J') # ANSI escape sequence to move cursor to top left. self._output_writer.Write(b'\033[H') else: # Windows cmd.exe does not support ANSI escape codes, thus instead we # fill the console screen buffer with spaces. top_left_coordinate = win32console.PyCOORDType(0, 0) screen_buffer = win32console.GetStdHandle( win32api.STD_OUTPUT_HANDLE) screen_buffer_information = screen_buffer.GetConsoleScreenBufferInfo( ) screen_buffer_attributes = screen_buffer_information[u'Attributes'] screen_buffer_size = screen_buffer_information[u'Size'] console_size = screen_buffer_size.X * screen_buffer_size.Y screen_buffer.FillConsoleOutputCharacter(u' ', console_size, top_left_coordinate) screen_buffer.FillConsoleOutputAttribute(screen_buffer_attributes, console_size, top_left_coordinate) screen_buffer.SetConsoleCursorPosition(top_left_coordinate)
def clear_buffer(self, buffer_number, page_end=False): # Clear a specific line self.hd.SetConsoleCursorPosition(wc.PyCOORDType(0, buffer_number)) if page_end: self.write(' ' * 79) else: self.write(' ' * 80)
def set_buffersize_to_windowsize(self): buffinfo = self.console_output.GetConsoleScreenBufferInfo() windowinfo = buffinfo['Window'] if windowinfo: sizex = windowinfo.Right - windowinfo.Left + 1 sizey = windowinfo.Bottom - windowinfo.Top + 1 self.console_output.SetConsoleScreenBufferSize( win32console.PyCOORDType(sizex, sizey))
def setXY(self, x, y): #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - """ set the cursor to (x,y) """ self.x = x self.y = y try: self.hout.SetConsoleCursorPosition(w32con.PyCOORDType(x, y)) except pywintypes.error as ex: if ex[0] == 87: raise self.CoordinateException(ex) raise self.ScreenException(ex)
def render_display(self): """Converts the display array into a string and outputs it to the screen""" write_string = '' for row in self.display_arr: for pixel in row: if pixel == 1: write_string += '██' else: write_string += ' ' self.console_buffer.WriteConsoleOutputCharacter( write_string, win32console.PyCOORDType(0, 0))
def set_window_size(self): """Sets the window and buffer size of the console""" try: window_size = self.console_buffer.GetConsoleScreenBufferInfo()['Window'] coord = win32console.PyCOORDType(X = (self.cols*2), Y = self.rows) self.console_buffer.SetConsoleScreenBufferSize(coord) window_size.Right = (self.cols*2) - 1 window_size.Bottom = self.rows - 1 self.console_buffer.SetConsoleWindowInfo( Absolute=True, ConsoleWindow=window_size) except pywintypes.error: raise Exception('Some issue occured when resizing the console, ' 'try decreasing the size or setting it back to default')
def refresh_console(self): """Clears the console after pausing the child and reading all the data currently on the console.""" orig = win32console.PyCOORDType(0, 0) self.consout.SetConsoleCursorPosition(orig) self.__currentReadCo.X = 0 self.__currentReadCo.Y = 0 writelen = self.__consSize.X * self.__consSize.Y # Use NUL as fill char because it displays as whitespace # (if we interact() with the child) self.consout.FillConsoleOutputCharacter(screenbufferfillchar, writelen, orig) self.__bufferY = 0 self.__buffer.truncate(0)
def __init__(self, path, host_pid, codepage=None, window_size_x=80, window_size_y=25, buffer_size_x=80, buffer_size_y=16000, local_echo=True, interact=False, **kwargs): """Initialize the console starts the child in it and reads the console periodically. Args: path (str): Child's executable with arguments. parent_pid (int): Parent (aka. host) process process-ID codepage (:obj:, optional): Output console code page. """ self.lastRead = 0 self.__bufferY = 0 self.lastReadData = "" self.totalRead = 0 self.__buffer = StringIO() self.__currentReadCo = win32console.PyCOORDType(0, 0) self.pipe = None self.connection = None self.consin = None self.consout = None self.local_echo = local_echo self.console_pid = os.getpid() self.host_pid = host_pid self.host_process = psutil.Process(host_pid) self.child_process = None self.child_pid = None self.enable_signal_chars = True logger.info( f'ConsoleReader started. location {os.path.abspath(__file__)}') if codepage is None: codepage = windll.kernel32.GetACP() try: logger.info("Setting console output code page to %s" % codepage) win32console.SetConsoleOutputCP(codepage) logger.info("Console output code page: %s" % ctypes.windll.kernel32.GetConsoleOutputCP()) except Exception as e: # pragma: no cover # I hope this code is unreachable... logger.error(e) try: self.create_connection(**kwargs) logger.info('Spawning %s' % path) try: self.initConsole() si = win32process.GetStartupInfo() self.__childProcess, _, self.child_pid, self.child_tid = win32process.CreateProcess( None, path, None, None, False, 0, None, None, si) self.child_process = psutil.Process(self.child_pid) logger.info( f'Child pid: {self.child_pid} Console pid: {self.console_pid}' ) except Exception: # pragma: no cover # I hope this code is unreachable... logger.error(traceback.format_exc()) return if interact: self.interact() self.interact() self.read_loop() except Exception: # pragma: no cover # I hope this code is unreachable... logger.error(traceback.format_exc()) finally: try: self.terminate_child() time.sleep(.01) self.send_to_host(self.readConsoleToCursor()) self.sendeof() time.sleep(.1) self.close_connection() logger.info('Console finished.') except Exception: # pragma: no cover # I hope this code is unreachable... logger.error(traceback.format_exc())
def getCoord(self, offset): """Converts an offset to a point represented as a tuple.""" x = offset % self.__consSize.X y = offset // self.__consSize.X return win32console.PyCOORDType(x, y)
def color_area(self, x, y, w, h, color): attrs = (color, ) * w for i in range(h): pos = win32console.PyCOORDType(x, y + i) self.console_output.WriteConsoleOutputAttribute(attrs, pos)
def color_pos(self, x, y, color): pos = win32console.PyCOORDType(x, y) self.console_output.WriteConsoleOutputAttribute((color, ), pos)
def free_focus(self, row, column): # Move cursor to a specific line&column self.hd.SetConsoleCursorPosition(wc.PyCOORDType(column, row))
def focus(self, buffer_number): # Move cursor to a specific line self.hd.SetConsoleCursorPosition(wc.PyCOORDType(0, buffer_number))
def main(): args = _parse_args() print(args) indexer = Indexer() train_data = PersonaDataset('train', args.persona, args.revision, indexer) valid_data = PersonaDataset('valid', args.persona, args.revision, indexer) test_data = PersonaDataset('test', args.persona, args.revision, indexer) counts = train_data.counts + valid_data.counts + test_data.counts counts = list(counts.values()) counts.sort(reverse=True) total = sum(counts) max_val = counts[0] max_p = max_val / total rows = 30 cols = 100 items_per_col = 204 columns = [Column(rows, i) for i in range(cols)] i = 0 while i < 300: val = counts[i] idx = int(math.floor(i / 3)) columns[idx].add_point((val/ total) / (max_p + .00001)) i+=1 sc_buf = win32console.CreateConsoleScreenBuffer() sc_buf.SetConsoleActiveScreenBuffer() # print(type(s)) print(sc_buf.GetConsoleScreenBufferInfo()) for col in columns: col.render(sc_buf) # sc_buf.SetConsoleScreenBufferSize(coord(120, 120)) sc_buf.WriteConsoleOutputCharacter("data visualization for 300 most frequent tokens", win32console.PyCOORDType(0,0)) c = msvcrt.getwch() # sc_buf.SetConsoleScreenBufferSize(s) # s = coord(100, 100) # sc_buf.SetConsoleScreenBufferSize(s) sc_buf.Close() return
def move(self, x, y): self.outbuf.SetConsoleCursorPosition(win32console.PyCOORDType(x, y))
def read_console(self, left, top, right, bottom): coord = win32console.PyCOORDType(X=left, Y=top) text = self.con_stdout.ReadConsoleOutputCharacter(Length=(right - left + 1), ReadCoord=coord) self.relay_console_update(left, top, text)
def reset_screen_buffer_size(self): # Used when program starts self.hd.SetConsoleScreenBufferSize(wc.PyCOORDType(80, 25))
raise ## only free console if one was created successfully free_console=False stdout=win32console.GetStdHandle(win32console.STD_OUTPUT_HANDLE) stdin=win32console.GetStdHandle(win32console.STD_INPUT_HANDLE) newbuffer=win32console.CreateConsoleScreenBuffer() newbuffer.SetConsoleActiveScreenBuffer() newbuffer.SetConsoleTextAttribute(win32console.FOREGROUND_RED|win32console.FOREGROUND_INTENSITY |win32console.BACKGROUND_GREEN|win32console.BACKGROUND_INTENSITY) newbuffer.WriteConsole('This is a new screen buffer\n') ## test setting screen buffer and window size ## screen buffer size cannot be smaller than window size window_size=newbuffer.GetConsoleScreenBufferInfo()['Window'] coord=win32console.PyCOORDType(X=window_size.Right+20, Y=window_size.Bottom+20) newbuffer.SetConsoleScreenBufferSize(coord) window_size.Right+=10 window_size.Bottom+=10 newbuffer.SetConsoleWindowInfo(Absolute=True,ConsoleWindow=window_size) ## write some records to the input queue x=win32console.PyINPUT_RECORDType(win32console.KEY_EVENT) x.Char='X' x.KeyDown=True x.RepeatCount=1 x.VirtualKeyCode=0x58 x.ControlKeyState=win32con.SHIFT_PRESSED z=win32console.PyINPUT_RECORDType(win32console.KEY_EVENT)
def coord(x,y): return win32console.PyCOORDType(x,y)