'write text at current cursor position while watching for scrolling.
If the window scrolls because you are at the bottom of the screen
buffer, all positions that you are storing will be shifted by the
scroll amount. For example, I remember the cursor position of the
prompt so that I can redraw the line but if the window scrolls,
the remembered position is off.
This variant of write tries to keep track of the cursor position
so that it will know when the screen buffer is scrolled. It
returns the number of lines that the buffer scrolled.'

def write_scrolling(self, text, attr=None):

    (x, y) = self.pos()
    (w, h) = self.size()
    scroll = 0
    chunks = self.motion_char_re.split(text)
    for chunk in chunks:
        log(('C:' + chunk))
        n = self.write_color(chunk, attr)
        if (len(chunk) == 1):
            if (chunk[0] == '\n'):
                x = 0
                y += 1
            elif (chunk[0] == '\r'):
                x = 0
            elif (chunk[0] == ' DCTB '):
                x = (8 * (int((x / 8)) + 1))
                if (x > w):
                    x -= w
                    y += 1
            elif (chunk[0] == '\x07'):
                pass
            elif (chunk[0] == '\x08'):
                x -= 1
                if (x < 0):
                    y -= 1
            else:
                x += 1
            if (x == w):
                x = 0
                y += 1
            if (y == h):
                scroll += 1
                y = (h - 1)
        else:
            x += n
            l = int((x / w))
            x = (x % w)
            y += l
            if (y >= h):
                scroll += ((y - h) + 1)
                y = (h - 1)
    return scroll
