<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def write(self, text):
 """Parses text and prints proper output to the terminal This method will extract escape codes from the text and handle them as well as possible for whichever platform is being used. At the moment only the display escape codes are supported. """

        escape_parts = re.compile('\x01?\x1b\\[([0-9;]*)m\x02?')
        chunks = escape_parts.split(text)
        i = 0
        for chunk in chunks:
            if chunk != '':
                if i % 2 == 0:
                    self.stream.write(chunk)
                else:
                    c = chunk.split(';')
                    r = Magic.rdisplay(c)
                    self.display(**r) #see caveat 0
                self.flush()
            i += 1