consoleLog / src / consoleLog / slot.py

# -*- coding: utf-8 -*-
#
#  slot.py
#  consoleLog
# 
#  Created by Lars Yencken on 03-05-2010.
#  Copyright 2010 Lars Yencken. All rights reserved.
#

"""
"""

import sys

import shellColor

class Slot(object):
    """
    A generic text slot which can be overwritten repeatedly.
    """
    def __init__(self, text=u'', stream=sys.stdout):
        if u'\n' in text:
            raise Exception('slots cannot undo newlines')
        self.stream = stream
        self.last_text = None
        self.is_finished = False
        if text:
            self.update(text)

    def update(self, text):
        if self.is_finished:
            raise Exception("slot already finalized, cannot update it")

        if isinstance(text, unicode):
            text = text.encode('utf8')

        if self.last_text:
            last_len = shellColor.realLen(self.last_text)
            self.stream.write('\b' * shellColor.realLen(self.last_text))
            this_len = shellColor.realLen(text)
            if this_len < last_len:
                text = text + ' ' * (last_len - this_len)

        self.stream.write(text)
        self.stream.flush()
        self.last_text = text

    def finish(self, text=None):
        if text:
            self.update(text)
        print >> self.stream
        self.is_finished = True

# vim: ts=4 sw=4 sts=4 et tw=78:
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.