Ejemplo n.º 1
0
 def __init__(self, gui):
     gtk.Entry.__init__(self)
     self.realm = gui.realm
     self.gui = gui
     self.tabdict = Trie()
     self.hist = CommandHistory(200)
     self.connect('key-press-event', self.key_pressed_cb)
     self.modify_font(pango.FontDescription('monospace 8'))
Ejemplo n.º 2
0
class CommandView(gtk.Entry):

    """The area where the user enters commands to be sent to the MUD."""

    def __init__(self, gui):
        gtk.Entry.__init__(self)
        self.realm = gui.realm
        self.gui = gui
        self.tabdict = Trie()
        self.hist = CommandHistory(200)
        self.connect('key-press-event', self.key_pressed_cb)
        self.modify_font(pango.FontDescription('monospace 8'))

    def key_pressed_cb(self, widget, event):
        """The user's pressed a key.

        First, this checks to see if there is a macro bound for the keychord,
        and if there is then it is run; if not, the key is handled by PyGTK.
        """
        chord = from_gtk_event(event)

        if not self.gui.realm.maybe_do_macro(chord):
            #not a macro, so keep processing.
            return False
        return True

    def history_up(self):
        """Move up (ie, back in time) one command in the history."""
        #cursor will be at the end of the line, as it has no left gravity.
        self.set_text(self.hist.advance())
        self.set_position(-1)

    def history_down(self):
        """Move down (ie, forwards in time) one command in the history."""
        self.set_text(self.hist.retreat())
        self.set_position(-1)

    def get_all_text(self):
        """Finger-saving mathod to get all the text from the buffer."""
        bytes = self.get_chars(0, -1)
        return bytes.decode('utf-8')

    def escape_pressed(self):
        """Add the current line to the list of previous commands, and clear
        the buffer.
        """
        self.hist.add_command(self.get_all_text())
        self.set_text('')

    def submit_line(self):
        """Send the current line to the MUD and clear the buffer."""
        text = self.get_all_text()
        #self.set_text('')
        self.select_region(0,-1)
        self.realm.receive_gui_line(text)
        if not self.realm.server_echo:
            self.hist.add_command(text)

    def tab_complete(self):
        """Tab-completion."""
        line = self.get_all_text()
        #cursor position as an integer from the start of the line
        ind = self.get_position()
        line, ind = self.tabdict.complete(line, ind)
        self.set_text(line)
        #move the cursor to where the tabdict wants it
        self.set_position(ind)

    def add_line_to_tabdict(self, line):
        """Add all the new words in the line to our tabdict."""
        self.tabdict.add_line(line)
Ejemplo n.º 3
0
 def setUp(self):
     self.c = CommandHistory(5)
Ejemplo n.º 4
0
class TestCommandHistory:

    def setUp(self):
        self.c = CommandHistory(5)

    def test_adds_new_command_to_buffer(self):
        self.c.add_command("foo")
        r = self.c.advance()
        assert r == 'foo', r

    def test_doesnt_add_dupes(self):
        self.c.add_command("spam")
        self.c.add_command("foo")
        self.c.add_command("foo")
        assert self.c.advance() == 'foo'
        assert self.c.advance() == 'spam'

    def test_is_LIFO_wrt_commands(self):
        self.c.add_command("foo")
        self.c.add_command('bar')
        self.c.add_command("baz")
        assert self.c.advance() == 'baz'
        assert self.c.advance() == 'bar'
        assert self.c.advance() == 'foo'

    def test_doesnt_add_blanks_to_history(self):
        self.c.add_command("foo")
        self.c.add_command(" ")
        assert self.c.advance() == 'foo'

    def test_has_maximum_buffer_size(self):
        cs = 'foo bar baz qux spam eggs'.split()
        for c in cs:
            self.c.add_command(c)
        res = [self.c.advance() for _ in range(6)]
        assert res == ['eggs', 'spam', 'qux', 'baz', 'bar', 'bar'], res

    def test_moving_back_and_forth(self):
        for c in reversed('foo bar baz spam eggs'.split()):
            self.c.add_command(c)
        assert self.c.advance() == 'foo'
        assert self.c.advance() == 'bar'
        assert self.c.advance() == 'baz'
        assert self.c.retreat() == 'bar'

    def test_goes_back_to_start_on_normal_addition(self):
        self.c.add_command("foo")
        self.c.add_command("bar")
        self.c.advance()
        self.c.add_command('baz')
        assert self.c.advance() == 'baz'

    def test_goes_back_to_start_on_blank(self):
        self.c.add_command('foo')
        self.c.add_command('baz')
        self.c.advance()
        self.c.add_command("")
        assert self.c.advance() == 'baz'

    def test_no_strange_stuff_on_bogus_retreat(self):
        self.c.add_command("foo")
        self.c.add_command("bar")
        self.c.add_command("baz")
        assert self.c.retreat() == ''

    def test_advance_with_empty_buffer_reutrns_empty_string(self):
        assert self.c.advance() == ''