From 7ecaeb02145b1c9c514e65a81d3d1ae231dc681b Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Sat, 29 Oct 2016 11:50:10 +1300 Subject: [PATCH] addons.view.focus: next and prev methods --- mitmproxy/addons/view.py | 20 +++++++++++++++++--- test/mitmproxy/addons/test_view.py | 25 +++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/mitmproxy/addons/view.py b/mitmproxy/addons/view.py index e2224c589..af366d67e 100644 --- a/mitmproxy/addons/view.py +++ b/mitmproxy/addons/view.py @@ -181,8 +181,6 @@ class Focus: def __init__(self, v: View) -> None: self.view = v self._focusflow = None - - self.focusflow = None if len(self.view): self.focusflow = self.view[0] v.sig_add.connect(self._sig_add) @@ -204,8 +202,24 @@ class Focus: if self.focusflow: return self.view.index(self.focusflow) + def next(self): + """ + Sets the focus to the next flow. + """ + if self.focusflow: + idx = min(self.index + 1, len(self.view) - 1) + self.focusflow = self.view[idx] + + def prev(self): + """ + Sets the focus to the previous flow. + """ + if self.focusflow: + idx = max(self.index - 1, 0) + self.focusflow = self.view[idx] + def _nearest(self, f, v): - return min(v.bisect(f), len(v)-1) + return min(v.bisect(f), len(v) - 1) def _sig_remove(self, view, flow): if len(view) == 0: diff --git a/test/mitmproxy/addons/test_view.py b/test/mitmproxy/addons/test_view.py index 1404a78ad..e4083bac3 100644 --- a/test/mitmproxy/addons/test_view.py +++ b/test/mitmproxy/addons/test_view.py @@ -235,6 +235,31 @@ def test_focus(): assert f.index is None +def test_focus_nextprev(): + v = view.View() + # Nops on an empty view + v.focus.next() + v.focus.prev() + + # Nops on a single-flow view + v.add(tft(start=0)) + assert v.focus.focusflow == v[0] + v.focus.next() + assert v.focus.focusflow == v[0] + v.focus.prev() + assert v.focus.focusflow == v[0] + + v.add(tft(start=1)) + v.focus.next() + assert v.focus.focusflow == v[1] + v.focus.next() + assert v.focus.focusflow == v[1] + v.focus.prev() + assert v.focus.focusflow == v[0] + v.focus.prev() + assert v.focus.focusflow == v[0] + + def test_settings(): v = view.View() f = tft()