Exemple #1
0
    def __init__(self):
        self.model = Model()
        self.view = View()

        self.socket = UDSClient()
        self.socket.register(Protocol.FULL_HISTORY, self.model.history.set)
        self.socket.register(Protocol.NEW_ENTRY, self.model.history.add)

        self.commands = {"q": self._quit,
                         "j": lambda: self.view.change_pointer(1),
                         "k": lambda: self.view.change_pointer(-1),
                         "y": lambda: self._copy_pointed(None),
                         "u": lambda: self._copy_pointed("lower"),
                         "U": lambda: self._copy_pointed("upper"),
                         "v|V": self._toggle_details_view,
                         "\t": self._toggle_match_url,
                         r'\+': lambda: self.view.resize(1),
                         r'\-': lambda: self.view.resize(-1),
                         r'/(.*)[^\]]$': self._search}

        self.running = True
Exemple #2
0
class Controller(object):
    """ Window/keyboard controller. """
    def __init__(self):
        self.model = Model()
        self.view = View()

        self.socket = UDSClient()
        self.socket.register(Protocol.FULL_HISTORY, self.model.history.set)
        self.socket.register(Protocol.NEW_ENTRY, self.model.history.add)

        self.commands = {"q": self._quit,
                         "j": lambda: self.view.change_pointer(1),
                         "k": lambda: self.view.change_pointer(-1),
                         "y": lambda: self._copy_pointed(None),
                         "u": lambda: self._copy_pointed("lower"),
                         "U": lambda: self._copy_pointed("upper"),
                         "v|V": self._toggle_details_view,
                         "\t": self._toggle_match_url,
                         r'\+': lambda: self.view.resize(1),
                         r'\-': lambda: self.view.resize(-1),
                         r'/(.*)[^\]]$': self._search}

        self.running = True

    def _search(self):
        "Prepare to filter items by search term."
        self.model.set_search()
        self.view.highlighted_item = 0
        self.view.pointer = 0

    def _quit(self):
        "Update state so that everyone knows the program should close."
        self.running = False

    def _clear_buffer(self, how_much=None):
        "Clear keyboard buffer."
        if how_much is None:
            how_much = len(self.model.kb_buffer)
        self.model.kb_buffer = self.model.kb_buffer[:-how_much]

    def _toggle_details_view(self):
        "Show/hide details view."
        self.view.details_window_show = not self.view.details_window_show

    def _toggle_match_url(self):
        "Toggle the match url toggle."
        self.model.match_url = not self.model.match_url
        self.view.highlighted_item = 0
        self.view.pointer = 0

    def _copy_pointed(self, mode=""):
        """Copy an item into the clipboard.
           Can copy as all upper or all lower, depending on mode chosen."""
        pointed = self.model.filtered_items[self.view.pointer].encode("utf-8")

        write_text = pointed.upper() if mode == "upper" else \
                     pointed.lower() if mode == "lower" else \
                     pointed

        ClipboardFactory.create().put_text(write_text)

    def update(self):
        "Update the visible items and draw."
        self.model.filter()
        self.view.update(self.model)

    def wait_for_key_and_process(self):
        "Wait for a key press and process it."
        self.update()

        key = self.view.get_key()
        if key in (None, Keys.LEFT, Keys.RIGHT):
            return

        if key == Keys.ENTER:
            self._clear_buffer()
        elif key == Keys.ESCAPE:
            self.model.search_text = ""
            self.model.kb_buffer = ""
        elif key == Keys.BACKSPACE:
            self._clear_buffer(1)
        elif key == Keys.UP:
            self.view.change_pointer(-1)
        elif key == Keys.DOWN:
            self.view.change_pointer(1)
        else:
            self.model.kb_buffer += key

            for key, value in self.commands.iteritems():
                regex = re.compile(r'\A' + key + r'\Z')
                match = regex.match(self.model.kb_buffer)

                if match is not None:
                    value()
                    break

            if len(self.model.kb_buffer) == 0 or self.model.kb_buffer[0] != "/":
                self._clear_buffer()

    def close(self):
        "Release resources."
        self.view.close()
        self.socket.close()