Example #1
0
class CommandLine(Edit, CommandsMixin):
    signals = ['change', 'show_autocomplete']
    def __init__(self, board, client, *args, **kwargs):
        super(CommandLine, self).__init__(*args, **kwargs)
        CommandsMixin.__init__(self, client)

        self.board = board
        self.set_caption('%s> ' % self.client.caption)

        self.history = History()
        self.mapping = {
            'up': self.previous,
            'down': self.next,
            'ctrl r': self.search,
            'tab': self.autocomplete,
            'enter': self.do,
        }

    def keypress(self, size, key):
        #self.board.base_widget.set_text(key)
        return self.mapping.get(key, lambda: super(CommandLine, self).keypress(size, key))()

    def previous(self):
        self.set_edit_text(self.history.up() or '')
        self.set_edit_pos(len(self.edit_text))
    def next(self):
        self.set_edit_text(self.history.down() or '')
        self.set_edit_pos(len(self.edit_text))

    def search(self):
        position = self.edit_pos
        query = self.edit_text[:self.edit_pos]
        result = self.history.search(query)

        if result:
            self.edit_text = result
            self.edit_pos = position

    def autocomplete(self):
        matches = self.complete(self.edit_text, self.edit_pos)
        if len(matches) == 1:
            match = matches[0]
            word = self.edit_text[:self.edit_pos].rsplit(' ')[-1]
            self.insert_text(match.keyword[0 if match.type == 'operator' else len(word):])
        elif matches:
            self._emit('show_autocomplete', matches)

    def do(self):
        line = self.edit_text
        result = self.onecmd(line)

        self.board.base_widget.set_text(result)

        self.set_edit_text(u'')
        self.history.append(line)
Example #2
0
    def __init__(self, board, client, *args, **kwargs):
        super(CommandLine, self).__init__(*args, **kwargs)
        CommandsMixin.__init__(self, client)

        self.board = board
        self.set_caption('%s> ' % self.client.caption)

        self.history = History()
        self.mapping = {
            'up': self.previous,
            'down': self.next,
            'ctrl r': self.search,
            'tab': self.autocomplete,
            'enter': self.do,
        }