'Append text to Python shell
In a way, this method overrides the method \'insert_text\' when text is
inserted at the end of the text widget for a Python shell
Handles error messages and show blue underlined links
Handles ANSI color sequences
Handles ANSI FF sequence'

def append_text_to_shell(self, text, error, prompt):

    cursor = self.textCursor()
    cursor.movePosition(QTextCursor.End)
    if ('\r' in text):
        text = text.replace('\r\n', '\n')
        text = text.replace('\r', '\n')
    while True:
        index = text.find(chr(12))
        if (index == (-1)):
            break
        text = text[(index + 1):]
        self.clear()
    if error:
        is_traceback = False
        for text in text.splitlines(True):
            if (text.startswith('        File') and (not text.startswith('        File    "<'))):
                is_traceback = True
                cursor.insertText('        ', self.default_style.format)
                cursor.insertText(text[2:], self.traceback_link_style.format)
            else:
                cursor.insertText(text, self.error_style.format)
        self.exception_occurred.emit(text, is_traceback)
    elif prompt:
        insert_text_to(cursor, text, self.prompt_style.format)
    else:
        last_end = 0
        for match in self.COLOR_PATTERN.finditer(text):
            insert_text_to(cursor, text[last_end:match.start()], self.default_style.format)
            last_end = match.end()
            try:
                for code in [int(_c) for _c in match.group(1).split(';')]:
                    self.ansi_handler.set_code(code)
            except ValueError:
                pass
            self.default_style.format = self.ansi_handler.get_format()
        insert_text_to(cursor, text[last_end:], self.default_style.format)
    self.set_cursor_position('eof')
    self.setCurrentCharFormat(self.default_style.format)
