diff options
author | pazz <patricktotzke@gmail.com> | 2011-05-23 21:47:44 +0100 |
---|---|---|
committer | pazz <patricktotzke@gmail.com> | 2011-05-23 21:47:44 +0100 |
commit | 30788c15e76e2d58840c0e4b89116572854586da (patch) | |
tree | b8c486b22f89e9f15e6dc3c7652f39428a7c70ba /alot/walker.py | |
parent | 25a42f2cf157632740e6940b544fca7d5bc98c5f (diff) |
rename to "alot"
read and laugh, alot: http://hyperboleandahalf.blogspot.com/2010/04/alot-is-better-than-you-at-everything.html
Diffstat (limited to 'alot/walker.py')
-rw-r--r-- | alot/walker.py | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/alot/walker.py b/alot/walker.py new file mode 100644 index 00000000..417a9e21 --- /dev/null +++ b/alot/walker.py @@ -0,0 +1,66 @@ +import urwid +import logging + +from notmuch import NotmuchError + + +class IteratorWalker(urwid.ListWalker): + def __init__(self, it, containerclass): + self.it = it + self.containerclass = containerclass + self.lines = [] + self.focus = 0 + self.empty = False + + def get_focus(self): + return self._get_at_pos(self.focus) + + def set_focus(self, focus): + self.focus = focus + self._modified() + + def get_next(self, start_from): + return self._get_at_pos(start_from + 1) + + def get_prev(self, start_from): + return self._get_at_pos(start_from - 1) + + def _get_at_pos(self, pos): + if pos < 0: # pos too low + return (None, None) + elif pos > len(self.lines): # pos too high + return (None, None) + elif len(self.lines) > pos: # pos already cached + return (self.lines[pos], pos) + else: # pos not cached yet, look at next item from iterator + if self.empty: # iterator is empty + return (None, None) + else: + widget = self._get_next_item() + if widget: + return (widget, pos) + else: + return (None, None) + + def _get_next_item(self): + try: + next_obj = self.it.next() + next_widget = self.containerclass(next_obj) + self.lines.append(next_widget) + except StopIteration: + next_widget = None + self.empty = True + return next_widget + + +class NotmuchIteratorWalker(IteratorWalker): + def _get_next_item(self): + logging.error("it still there") + try: + next_obj = self.it.next() + logging.error("next obj: %s" % next_obj) + next_widget = self.containerclass(next_obj) + self.lines.append(next_widget) + except NotmuchError: + next_widget = None + return next_widget |