Commit 5f4302c6 authored by Lysander Trischler's avatar Lysander Trischler
Browse files

Implement crude search

parent 87209450
......@@ -18,9 +18,12 @@ manager = twtxtmanager.TwtxtManager()
manager.load_twts()
class TwtsListBox(widgets.VimListBox):
def __init__(self, manager):
def __init__(self, manager, cmdline):
self._manager = manager
self._cmdline = cmdline
self._twts = []
self._search = None
self._search_downwards = True
items = []
......@@ -155,10 +158,16 @@ class TwtsListBox(widgets.VimListBox):
self.list_urls()
return
if key == "n":
self.jump_to_unread_twt(downwards=True, ignore_missing=False)
if self._search:
self.jump_to_search_match(downwards=True)
else:
self.jump_to_unread_twt(downwards=True, ignore_missing=False)
return
if key == "N":
self.jump_to_unread_twt(downwards=False, ignore_missing=False)
if self._search:
self.jump_to_search_match(downwards=False)
else:
self.jump_to_unread_twt(downwards=False, ignore_missing=False)
return
if key == "J":
self.jump_to_unread_twt(downwards=True, ignore_missing=True)
......@@ -166,6 +175,15 @@ class TwtsListBox(widgets.VimListBox):
if key == "K":
self.jump_to_unread_twt(downwards=False, ignore_missing=True)
return
if key == "/":
self.open_search_line(downwards=True)
return
if key == "?":
self.open_search_line(downwards=False)
return
if key == "q" and self._search:
self.clear_search()
return
return super().keypress(size, key)
......@@ -186,18 +204,65 @@ class TwtsListBox(widgets.VimListBox):
frame.body.push_widget(URLListBox(self._manager, twt))
def jump_to_unread_twt(self, downwards=True, ignore_missing=False):
def _loop_twts(self, downwards=True):
pos = self.focus_position
if downwards:
indices = range(pos + 1, len(self._twts))
else:
indices = range(pos - 1, -1, -1)
for i in indices:
if not self._twts[i].read and (not ignore_missing or not hasattr(self._twts[i], "missing")):
yield i, self._twts[i]
def jump_to_unread_twt(self, downwards=True, ignore_missing=False):
for i, twt in self._loop_twts(downwards):
if not twt.read and (not ignore_missing or not hasattr(twt, "missing")):
self.focus_position = i
return
def jump_to_search_match(self, downwards=True):
if not self._search:
return
# invert given search direction if reverse search was initiated
if not self._search_downwards:
downwards = not downwards
for i, twt in self._loop_twts(downwards):
if self._search in twt.text:
self.focus_position = i
return
def open_search_line(self, downwards=True):
pile = frame.footer
if len(pile.contents) == 1:
pile.contents.append((self._cmdline, pile.options()))
frame.set_focus_path(["footer", 1])
def accept_search(search):
self._search = search
self._search_downwards = downwards
self.jump_to_search_match(downwards)
self._cmdline.open(leave_callback=self.close_search_line,
input_callback=accept_search,
prompt="/" if downwards else "?",
history=search_history)
def close_search_line(self):
pile = frame.footer
if len(pile.contents) == 2:
del pile.contents[1]
frame.set_focus_path(["body"])
def clear_search(self):
self._search = None
class URLListBox(widgets.VimListBox):
class Mention:
def __init__(self, nick, url, following):
......@@ -332,10 +397,12 @@ def generate_header_text():
len(manager.conversation_tree), manager.missing_conversation_root_count,
manager.twts_count, manager.read_twts_count, manager.unread_twts_count)
search_history = ["subject"]
cmdline = widgets.CommandLine()
frame = urwid.Frame(body=widgets.Stack([TwtsListBox(manager)]),
frame = urwid.Frame(body=widgets.Stack([TwtsListBox(manager, cmdline)]),
header=urwid.AttrMap(urwid.Text(generate_header_text()), "header"),
footer=urwid.AttrMap(urwid.Text("q: quit r: toggle read u: urls n: next unread N: prev unread J: next not missing unread K: prev not missing unread"), "footer"))
footer=urwid.Pile([urwid.AttrMap(urwid.Text("q: quit r: toggle read u: urls n: next unread N: prev unread J: next not missing unread K: prev not missing unread"), "footer")]))
def unhandled_input(key):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment