From 8a0404ddf81a61642e516dd32025ba54d7d3676f Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Sun, 29 Mar 2015 14:32:36 +1300 Subject: Beginning of a simpler and more flexible search implementation --- libmproxy/console/searchable.py | 83 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 libmproxy/console/searchable.py (limited to 'libmproxy/console/searchable.py') diff --git a/libmproxy/console/searchable.py b/libmproxy/console/searchable.py new file mode 100644 index 00000000..dc8b0bab --- /dev/null +++ b/libmproxy/console/searchable.py @@ -0,0 +1,83 @@ +import urwid + +from . import signals + + +class Highlight(urwid.AttrMap): + def __init__(self, t): + urwid.AttrMap.__init__( + self, + urwid.Text(t.text), + "focusfield", + ) + self.backup = t + + +class Searchable(urwid.ListBox): + def __init__(self, contents): + urwid.ListBox.__init__( + self, + urwid.SimpleFocusListWalker(contents) + ) + + self.search_offset = 0 + self.current_highlight = None + self.search_term = None + + def keypress(self, size, key): + if key == "/": + signals.status_prompt.send( + prompt = "Search for", + text = self.search_term or "", + callback = self.set_search + ) + if key == "n": + self.find_next(False) + if key == "N": + self.find_next(True) + else: + return super(self.__class__, self).keypress(size, key) + + def set_search(self, text): + self.search_term = text or None + self.find_next(False) + + def set_highlight(self, offset): + if self.current_highlight is not None: + old = self.body[self.current_highlight] + self.body[self.current_highlight] = old.backup + if offset is None: + self.current_highlight = None + else: + self.body[offset] = Highlight(self.body[offset]) + self.current_highlight = offset + + def get_text(self, w): + if isinstance(w, urwid.Text): + return w.text + elif isinstance(w, Highlight): + return w.backup.text + else: + return None + + def find_next(self, backwards): + if not self.search_term: + self.set_highlight(None) + return + # Start search at focus + 1 + if backwards: + rng = xrange(len(self.body)-1, -1, -1) + else: + rng = xrange(1, len(self.body)) + for i in rng: + off = (self.focus_position + i)%len(self.body) + w = self.body[off] + txt = self.get_text(w) + if txt and self.search_term in txt: + self.set_highlight(off) + self.set_focus(off, coming_from="above") + self.body._modified() + return + else: + self.set_highlight(None) + signals.status_message.send(message="Search not found.", expire=1) -- cgit v1.2.3 From e4738bdd39c50f682f50eb16d996e8c35ebb053f Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Sun, 29 Mar 2015 14:39:47 +1300 Subject: Fix search wrap-around offsets. --- libmproxy/console/searchable.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'libmproxy/console/searchable.py') diff --git a/libmproxy/console/searchable.py b/libmproxy/console/searchable.py index dc8b0bab..5539ecb2 100644 --- a/libmproxy/console/searchable.py +++ b/libmproxy/console/searchable.py @@ -19,7 +19,6 @@ class Searchable(urwid.ListBox): self, urwid.SimpleFocusListWalker(contents) ) - self.search_offset = 0 self.current_highlight = None self.search_term = None @@ -68,7 +67,7 @@ class Searchable(urwid.ListBox): if backwards: rng = xrange(len(self.body)-1, -1, -1) else: - rng = xrange(1, len(self.body)) + rng = xrange(1, len(self.body) + 1) for i in rng: off = (self.focus_position + i)%len(self.body) w = self.body[off] -- cgit v1.2.3 From 80c4de5ca462a211fca33d45fe52441b246d4d03 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Sun, 29 Mar 2015 15:14:56 +1300 Subject: Keep record of last search term --- libmproxy/console/searchable.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'libmproxy/console/searchable.py') diff --git a/libmproxy/console/searchable.py b/libmproxy/console/searchable.py index 5539ecb2..bd1288d5 100644 --- a/libmproxy/console/searchable.py +++ b/libmproxy/console/searchable.py @@ -14,11 +14,12 @@ class Highlight(urwid.AttrMap): class Searchable(urwid.ListBox): - def __init__(self, contents): + def __init__(self, state, contents): urwid.ListBox.__init__( self, urwid.SimpleFocusListWalker(contents) ) + self.state = state self.search_offset = 0 self.current_highlight = None self.search_term = None @@ -38,6 +39,7 @@ class Searchable(urwid.ListBox): return super(self.__class__, self).keypress(size, key) def set_search(self, text): + self.state.last_search = text self.search_term = text or None self.find_next(False) @@ -61,8 +63,11 @@ class Searchable(urwid.ListBox): def find_next(self, backwards): if not self.search_term: - self.set_highlight(None) - return + if self.state.last_search: + self.search_term = self.state.last_search + else: + self.set_highlight(None) + return # Start search at focus + 1 if backwards: rng = xrange(len(self.body)-1, -1, -1) -- cgit v1.2.3 From bdc2fda7ef9d6085f14f52abf2447773a63d9712 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Sun, 29 Mar 2015 15:16:20 +1300 Subject: Seems more natural to re-prompt for search every time --- libmproxy/console/searchable.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'libmproxy/console/searchable.py') diff --git a/libmproxy/console/searchable.py b/libmproxy/console/searchable.py index bd1288d5..8f63c3f5 100644 --- a/libmproxy/console/searchable.py +++ b/libmproxy/console/searchable.py @@ -28,7 +28,7 @@ class Searchable(urwid.ListBox): if key == "/": signals.status_prompt.send( prompt = "Search for", - text = self.search_term or "", + text = "", callback = self.set_search ) if key == "n": -- cgit v1.2.3 From 8f5cf833d08aba685263554d0bd89f922cd6afae Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Sun, 29 Mar 2015 19:21:54 +1300 Subject: Add flow detail view as a tab in the flow view --- libmproxy/console/searchable.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'libmproxy/console/searchable.py') diff --git a/libmproxy/console/searchable.py b/libmproxy/console/searchable.py index 8f63c3f5..9d66c718 100644 --- a/libmproxy/console/searchable.py +++ b/libmproxy/console/searchable.py @@ -36,7 +36,7 @@ class Searchable(urwid.ListBox): if key == "N": self.find_next(True) else: - return super(self.__class__, self).keypress(size, key) + return key def set_search(self, text): self.state.last_search = text -- cgit v1.2.3 From 44fb42185f0e177b775c5e4f11249a55390048f6 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Tue, 31 Mar 2015 15:59:54 +1300 Subject: console: fix body scrolling --- libmproxy/console/searchable.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'libmproxy/console/searchable.py') diff --git a/libmproxy/console/searchable.py b/libmproxy/console/searchable.py index 9d66c718..a723dca8 100644 --- a/libmproxy/console/searchable.py +++ b/libmproxy/console/searchable.py @@ -36,7 +36,7 @@ class Searchable(urwid.ListBox): if key == "N": self.find_next(True) else: - return key + super(self.__class__, self).keypress(size, key) def set_search(self, text): self.state.last_search = text -- cgit v1.2.3 From 32ba6021b3c07efaa45a9223479151cd7e74ccbd Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Wed, 1 Apr 2015 09:25:50 +1300 Subject: console: improve handling of help contexts, fix key bindings in flow views --- libmproxy/console/searchable.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'libmproxy/console/searchable.py') diff --git a/libmproxy/console/searchable.py b/libmproxy/console/searchable.py index a723dca8..8f63c3f5 100644 --- a/libmproxy/console/searchable.py +++ b/libmproxy/console/searchable.py @@ -36,7 +36,7 @@ class Searchable(urwid.ListBox): if key == "N": self.find_next(True) else: - super(self.__class__, self).keypress(size, key) + return super(self.__class__, self).keypress(size, key) def set_search(self, text): self.state.last_search = text -- cgit v1.2.3 From 8e2e83a3c6d47e52a5398f851b2900dd042f3d6a Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Tue, 7 Apr 2015 16:13:42 +1200 Subject: console: add g/G shortcuts throughout g: go to end G: go to beginning --- libmproxy/console/searchable.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'libmproxy/console/searchable.py') diff --git a/libmproxy/console/searchable.py b/libmproxy/console/searchable.py index 8f63c3f5..a9572ae3 100644 --- a/libmproxy/console/searchable.py +++ b/libmproxy/console/searchable.py @@ -15,10 +15,8 @@ class Highlight(urwid.AttrMap): class Searchable(urwid.ListBox): def __init__(self, state, contents): - urwid.ListBox.__init__( - self, - urwid.SimpleFocusListWalker(contents) - ) + self.walker = urwid.SimpleFocusListWalker(contents) + urwid.ListBox.__init__(self, self.walker) self.state = state self.search_offset = 0 self.current_highlight = None @@ -31,10 +29,16 @@ class Searchable(urwid.ListBox): text = "", callback = self.set_search ) - if key == "n": + elif key == "n": self.find_next(False) - if key == "N": + elif key == "N": self.find_next(True) + elif key == "G": + self.set_focus(0) + self.walker._modified() + elif key == "g": + self.set_focus(len(self.walker)-1) + self.walker._modified() else: return super(self.__class__, self).keypress(size, key) -- cgit v1.2.3 From a05a70d8168a07c92b2a3ecbbb1958d85532efe3 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Sat, 30 May 2015 12:03:28 +1200 Subject: Add coding style check, reformat. --- libmproxy/console/searchable.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'libmproxy/console/searchable.py') diff --git a/libmproxy/console/searchable.py b/libmproxy/console/searchable.py index a9572ae3..627d595d 100644 --- a/libmproxy/console/searchable.py +++ b/libmproxy/console/searchable.py @@ -37,7 +37,7 @@ class Searchable(urwid.ListBox): self.set_focus(0) self.walker._modified() elif key == "g": - self.set_focus(len(self.walker)-1) + self.set_focus(len(self.walker) - 1) self.walker._modified() else: return super(self.__class__, self).keypress(size, key) @@ -74,11 +74,11 @@ class Searchable(urwid.ListBox): return # Start search at focus + 1 if backwards: - rng = xrange(len(self.body)-1, -1, -1) + rng = xrange(len(self.body) - 1, -1, -1) else: rng = xrange(1, len(self.body) + 1) for i in rng: - off = (self.focus_position + i)%len(self.body) + off = (self.focus_position + i) % len(self.body) w = self.body[off] txt = self.get_text(w) if txt and self.search_term in txt: -- cgit v1.2.3