Make hypertext searchable

Change-Id: Ifdfa8dac0f4505e82c346bcbc2f1e392b433a757
This commit is contained in:
James E. Blair 2017-02-23 21:58:46 -05:00
parent e948bfef94
commit 1da12089a5
2 changed files with 27 additions and 12 deletions

View File

@ -258,23 +258,33 @@ class YesNoDialog(ButtonDialog):
return r return r
class SearchableText(urwid.Text): class SearchableText(urwid.Text):
def __init__(self, *args, **kw):
self.__search = None
self.__attribute = None
super(SearchableText, self).__init__(*args, **kw)
def set_text(self, markup): def set_text(self, markup):
self._markup = markup self._searchable_markup = markup
super(SearchableText, self).set_text(markup) self._search()
def search(self, search, attribute): def search(self, search, attribute):
if not search: self.__search = search
self.set_text(self._markup) self.__attribute = attribute
self._search()
def _search(self):
if not self.__search:
super(SearchableText, self).set_text(self._searchable_markup)
return return
(text, attrs) = urwid.util.decompose_tagmarkup(self._markup) (text, attrs) = urwid.util.decompose_tagmarkup(self._searchable_markup)
last = 0 last = 0
found = False found = False
while True: while True:
start = text.find(search, last) start = text.find(self.__search, last)
if start < 0: if start < 0:
break break
found = True found = True
end = start + len(search) end = start + len(self.__search)
i = 0 i = 0
newattrs = [] newattrs = []
for attr, al in attrs: for attr, al in attrs:
@ -290,7 +300,7 @@ class SearchableText(urwid.Text):
after = max(i + al - end, 0) after = max(i + al - end, 0)
if before: if before:
newattrs.append((attr, before)) newattrs.append((attr, before))
newattrs.append((attribute, len(search))) newattrs.append((self.__attribute, len(self.__search)))
if after: if after:
newattrs.append((attr, after)) newattrs.append((attr, after))
i += al i += al
@ -298,7 +308,7 @@ class SearchableText(urwid.Text):
newattrs.append((None, start-i)) newattrs.append((None, start-i))
i += start-i i += start-i
if i < end: if i < end:
newattrs.append((attribute, len(search))) newattrs.append((self.__attribute, len(self.__search)))
last = start + 1 last = start + 1
attrs = newattrs attrs = newattrs
self._text = text self._text = text
@ -362,7 +372,7 @@ class Searchable(object):
if self.current_result >= len(self.results): if self.current_result >= len(self.results):
self.current_result = 0 self.current_result = 0
class HyperText(urwid.Text): class HyperText(SearchableText):
_selectable = True _selectable = True
def __init__(self, markup, align=urwid.LEFT, wrap=urwid.SPACE, layout=None): def __init__(self, markup, align=urwid.LEFT, wrap=urwid.SPACE, layout=None):
@ -409,7 +419,7 @@ class HyperText(urwid.Text):
def focusItem(self, item): def focusItem(self, item):
self.last_focused_index = self.focused_index self.last_focused_index = self.focused_index
self.focused_index = item self.focused_index = item
self.set_text(self._markup) self.set_text(self._hypertext_markup)
self._invalidate() self._invalidate()
def select(self): def select(self):
@ -497,7 +507,7 @@ class HyperText(urwid.Text):
return markup return markup
def set_text(self, markup): def set_text(self, markup):
self._markup = markup self._hypertext_markup = markup
self.selectable_items = [] self.selectable_items = []
super(HyperText, self).set_text(self.processLinks(markup)) super(HyperText, self).set_text(self.processLinks(markup))

View File

@ -436,6 +436,11 @@ class StoryEventBox(mywid.HyperText):
info = [] info = []
self.set_text(text+comment_text+info) self.set_text(text+comment_text+info)
def search(self, search, attribute):
if self.text.search(search, attribute):
return True
return False
class DescriptionBox(mywid.HyperText): class DescriptionBox(mywid.HyperText):
def __init__(self, app, description): def __init__(self, app, description):
self.app = app self.app = app