def _insert_plain_text(self, cursor, text, flush=False):
        """ Inserts plain text using the specified cursor, processing ANSI codes
            if enabled.
        """
        should_autoscroll = self._viewport_at_end()
        # maximumBlockCount() can be different from self.buffer_size in
        # case input prompt is active.
        buffer_size = self._control.document().maximumBlockCount()

        if (self._executing and not flush and
                self._pending_text_flush_interval.isActive() and
                cursor.position() == self._get_end_pos()):
            # Queue the text to insert in case it is being inserted at end
            self._pending_insert_text.append(text)
            if buffer_size > 0:
                self._pending_insert_text = self._get_last_lines_from_list(
                                        self._pending_insert_text, buffer_size)
            return

        if self._executing and not self._pending_text_flush_interval.isActive():
            self._pending_text_flush_interval.start()

        # Clip the text to last `buffer_size` lines.
        if buffer_size > 0:
            text = self._get_last_lines(text, buffer_size)

        cursor.beginEditBlock()
        if self.ansi_codes:
            for substring in self._ansi_processor.split_string(text):
                for act in self._ansi_processor.actions:

                    # Unlike real terminal emulators, we don't distinguish
                    # between the screen and the scrollback buffer. A screen
                    # erase request clears everything.
                    if act.action == 'erase':
                        remove = False
                        fill = False
                        if act.area == 'screen':
                            cursor.select(QtGui.QTextCursor.Document)
                            remove = True
                        if act.area == 'line':
                            if act.erase_to == 'all': 
                                cursor.select(QtGui.QTextCursor.LineUnderCursor)
                                remove = True
                            elif act.erase_to == 'start':
                                cursor.movePosition(
                                    QtGui.QTextCursor.StartOfLine,
                                    QtGui.QTextCursor.KeepAnchor)
                                remove = True
                                fill = True
                            elif act.erase_to == 'end':
                                cursor.movePosition(
                                    QtGui.QTextCursor.EndOfLine,
                                    QtGui.QTextCursor.KeepAnchor)
                                remove = True
                        if remove: 
                            nspace=cursor.selectionEnd()-cursor.selectionStart() if fill else 0
                            cursor.removeSelectedText()
                            if nspace>0: cursor.insertText(' '*nspace) # replace text by space, to keep cursor position as specified

                    # Simulate a form feed by scrolling just past the last line.
                    elif act.action == 'scroll' and act.unit == 'page':
                        cursor.insertText('\n')
                        cursor.endEditBlock()
                        self._set_top_cursor(cursor)
                        cursor.joinPreviousEditBlock()
                        cursor.deletePreviousChar()

                        if os.name == 'nt':
                            cursor.select(QtGui.QTextCursor.Document)
                            cursor.removeSelectedText()

                    elif act.action == 'carriage-return':
                        cursor.movePosition(
                            QtGui.QTextCursor.StartOfLine,
                            QtGui.QTextCursor.MoveAnchor)

                    elif act.action == 'beep':
                        QtWidgets.QApplication.instance().beep()

                    elif act.action == 'backspace':
                        if not cursor.atBlockStart():
                            cursor.movePosition(
                                QtGui.QTextCursor.PreviousCharacter,
                                QtGui.QTextCursor.MoveAnchor)

                    elif act.action == 'newline':
                        cursor.movePosition(QtGui.QTextCursor.EndOfLine)

                # simulate replacement mode
                if substring is not None:
                    format = self._ansi_processor.get_format()
                    if not (hasattr(cursor,'_insert_mode') and cursor._insert_mode):
                        pos = cursor.position()
                        cursor2 = QtGui.QTextCursor(cursor)  # self._get_line_end_pos() is the previous line, don't use it
                        cursor2.movePosition(QtGui.QTextCursor.EndOfLine)
                        remain = cursor2.position() - pos    # number of characters until end of line
                        n=len(substring)
                        swallow = min(n, remain)             # number of character to swallow
                        cursor.setPosition(pos+swallow,QtGui.QTextCursor.KeepAnchor)
                    cursor.insertText(substring,format)
        else:
            cursor.insertText(text)
        cursor.endEditBlock()

        if should_autoscroll:
            self._scroll_to_end()
 Inserts plain text using the specified cursor, processing ANSI codes
            if enabled.
        
angrmanagement/vendor/qtconsole/console_widget.py