Ejemplo n.º 1
0
class DetailWidget(BasicWidget):
    """ Display content for message

    Attributes:
        message: message generate by DataStore
        displayer_context: DisplayerContext instance
        index: view's index
    """
    def __init__(self, message, displayer_context, index=0, **kwargs):
        super(DetailWidget, self).__init__(**kwargs)
        self.index = index
        self.message = message
        self.displayer_context = displayer_context

        self.views = self.displayer_context.displayer.get_views()
        self.name, view_callable = self.views[index]
        self.view = view_callable.__call__(self.message)

        self.content_widget = self.view.widget(
            self.message, **kwargs)

        _verify_keys(self.view.actions)
        self.search_widget = SearchWidget(self._search, self._clear_search)
        self.help_widget = HelpWidget(
            HelpContent(
                [HelpCategory("Basic", self.context.config.keys),
                 HelpCategory("Advanced", _ADVANCED_KEYS),
                 make_category_with_actions("Custom", self.view.actions)]),
            **kwargs
        )

        if len(self.views) > 1:
            header = Tabs([k for k, _ in self.views], self.index)
        else:
            header = None

        widget = urwid.Frame(self.content_widget, header=header)
        self.display(widget)

    def _open(self, index):
        self._clear_search()
        try:
            self.controller.open_view(DetailWidget(
                self.message, self.displayer_context, index=index,
                controller=self.controller, context=self.context),
                push_prev=False)
        except:
            self.controller.open_error()

    def _next_view(self):
        if len(self.views) == 1:
            return

        if len(self.views) > self.index + 1:
            next_index = self.index + 1
        else:
            next_index = 0
        self._open(next_index)

    def _prev_view(self):
        if len(self.views) == 1:
            return

        next_index = len(self.views) - 1 if self.index == 0 else self.index - 1
        self._open(next_index)

    def _open_search(self):
        self.search_widget.clear()
        self.content_widget.clear_prev_search()
        self.controller.open_edit(self.search_widget)

    def _search(self, keyword):
        self.controller._focus_body()
        self.content_widget.search_next(keyword)
        self.controller.close_edit()

    def _clear_search(self):
        self.controller.close_edit()

    def _export(self):  # pragma: no cover
        file_name = "export-%13d" % (time.time() * 1000)
        with open(file_name, "w") as f:
            f.write(str(self.view))
        self.controller.notify("Export to file {0}".format(file_name))

    def update_info(self):
        return self.controller._update_info(self, self.name)

    def keypress(self, size, key):
        if key == "q":
            self._clear_search()
            self.controller.back()
            return None
        if key == "tab":
            self._next_view()
            return None
        if key == "shift tab":
            self._prev_view()
            return None
        if key == "/":
            self._open_search()
            return None
        if key == "n":
            if self.search_widget.get_keyword():
                self.content_widget.search_next(
                    self.search_widget.get_keyword()
                )
            return None
        if key == "N":
            if self.search_widget.get_keyword():
                self.content_widget.search_prev(
                    self.search_widget.get_keyword()
                )
            return None
        if key == "e":  # pragma: no cover
            self._export()
            return None
        if key == "?":  # pragma: no cover
            self.controller.open_view(self.help_widget)
            return None

        return super(DetailWidget, self).keypress(size, key)  # pragma: no cover
Ejemplo n.º 2
0
class SummaryListWidget(BasicWidget):
    """ ListBox widget to contains the content of SummaryItemWidget

    Attributes:
        displayer_context: DisplayerContext
    """

    def __init__(self, displayer_context, **kwargs):
        super(SummaryListWidget, self).__init__(**kwargs)
        _verify_keys(displayer_context.actions)

        self.name = displayer_context.displayer.get_name()
        self.base_walker = SummaryListWalker(displayer_context=displayer_context, on_receive=self._on_receive, **kwargs)
        self.current_walker = self.base_walker

        self.search_widget = SearchWidget(self._filter, self._clear_search)
        self.help_widget = HelpWidget(
            HelpContent(
                [
                    HelpCategory("Basic", self.context.config.keys),
                    HelpCategory("Advanced", _ADVANCED_KEYS),
                    make_category_with_actions("Custom", displayer_context.actions),
                ]
            ),
            **kwargs
        )

        self.display(urwid.ListBox(self.base_walker))

    def _filter(self, keyword):
        if keyword:
            new_walker = FilterSummaryListWalker(self.base_walker, keyword)
        else:
            new_walker = self.base_walker
        if new_walker is not self.current_walker:
            self._update_content(new_walker)
        self.controller._focus_body()

    def _update_content(self, walker):
        if self.current_walker is not self.base_walker:
            self.current_walker.close()

        self.current_walker = walker
        self.display(urwid.ListBox(walker))
        self.update_info()

    def _open_search(self):
        self.search_widget.clear()
        self.controller.open_edit(self.search_widget)

    def _clear_search(self):
        self._filter(None)
        self.controller.close_edit()

    def update_info(self):
        if len(self.current_walker):
            curr_index = self._w.focus_position + 1
            total_index = len(self.current_walker)
            self.controller._update_info(self, "{0}[{1}/{2}]".format(self.name, curr_index, total_index))
        else:
            self.controller._update_info(self, "{0}[0/0]".format(self.name))

    def _on_receive(self):
        if self.context.config.auto_scroll:
            curr_index = self._w.focus_position + 1
            total_index = len(self.current_walker)
            if curr_index == total_index - 1:
                self._w.set_focus(curr_index)
        self.update_info()

    def keypress(self, size, key):
        if key == "/":
            self._open_search()
            return None
        if key == "q" and isinstance(self.current_walker, FilterSummaryListWalker):
            self._clear_search()
            return None
        if key == "q":
            self.controller.back()
            return None
        if key == "g":
            self._w.set_focus(0)
            self.update_info()
            return super(SummaryListWidget, self).keypress(size, key)
        if key == "G":
            self._w.set_focus(len(self.current_walker) - 1)
            self.update_info()
            return super(SummaryListWidget, self).keypress(size, key)
        if key == "x":
            del self.current_walker[self._w.focus_position]
            self.update_info()
            return super(SummaryListWidget, self).keypress(size, key)
        if key == "X":
            del self.current_walker[:]
            self.update_info()
            return super(SummaryListWidget, self).keypress(size, key)
        if key == "?":
            self.controller.open_view(self.help_widget)
            return None

        keypress_result = super(SummaryListWidget, self).keypress(size, key)  # pragma: no cover
        self.update_info()
        return keypress_result