diff options
author | Pedro Worcel <pedro@worcel.com> | 2014-02-22 16:32:22 +1300 |
---|---|---|
committer | Pedro Worcel <pedro@worcel.com> | 2014-02-22 16:32:22 +1300 |
commit | 4284fd3614561fe5e2c53154defb3774f14c589c (patch) | |
tree | d26bbe6d369a00cdf96778c10fa53be2a73ff9cc | |
parent | 3c02865e8b5839d536bc9982e4c0e6e699fd1943 (diff) | |
download | mitmproxy-4284fd3614561fe5e2c53154defb3774f14c589c.tar.gz mitmproxy-4284fd3614561fe5e2c53154defb3774f14c589c.tar.bz2 mitmproxy-4284fd3614561fe5e2c53154defb3774f14c589c.zip |
add multi-line support to backwards search
-rw-r--r-- | libmproxy/console/flowview.py | 18 | ||||
-rw-r--r-- | test/test_console_search.py | 38 |
2 files changed, 41 insertions, 15 deletions
diff --git a/libmproxy/console/flowview.py b/libmproxy/console/flowview.py index 9cdd2923..624cf95d 100644 --- a/libmproxy/console/flowview.py +++ b/libmproxy/console/flowview.py @@ -330,7 +330,7 @@ class FlowView(common.WWrap): self.last_displayed_body = list_box - if self.search_wrapped_around(last_find_line, last_search_index): + if not backwards and self.search_wrapped_around(last_find_line, last_search_index): return "search hit BOTTOM, continuing at TOP" def search_get_start(self, search_string): @@ -358,9 +358,9 @@ class FlowView(common.WWrap): def search_get_range(self, len_text_objects, start_line, backwards): if not backwards: - loop_range = range(start_line, len_text_objects) + loop_range = xrange(start_line, len_text_objects) else: - loop_range = range(start_line, 0, -1) + loop_range = xrange(start_line, -1, -1) return loop_range @@ -370,10 +370,11 @@ class FlowView(common.WWrap): found = False text_objects = copy.deepcopy(text_objects) - for i in self.search_get_range(len(text_objects), start_line, backwards): + loop_range = self.search_get_range(len(text_objects), start_line, backwards) + for i in loop_range: text_object = text_objects[i] if i != start_line: - start_index = None + start_index = 0 try: text, style = text_object.get_text() @@ -385,16 +386,18 @@ class FlowView(common.WWrap): else: if start_index != 0: start_index -= len(search_string) + else: + start_index = None find_index = text.rfind(search_string, 0, start_index) - # Found text in line, do the highlight highlight. if find_index != -1: new_text = self.search_highlight_object(text, find_index, search_string) text_objects[i] = new_text self.state.add_flow_setting(self.flow, "last_search_index", find_index) + self.state.add_flow_setting(self.flow, "last_find_line", i) found = True @@ -409,7 +412,8 @@ class FlowView(common.WWrap): else: self.state.add_flow_setting(self.flow, "last_search_index", 0) self.state.add_flow_setting(self.flow, "last_find_line", 0) - text_objects, focus_pos = self.search_highlight_text(text_objects, search_string, True) + text_objects, focus_pos = self.search_highlight_text(text_objects, + search_string, looping=True, backwards=backwards) return text_objects, focus_pos diff --git a/test/test_console_search.py b/test/test_console_search.py index 69c033b9..60b998cc 100644 --- a/test/test_console_search.py +++ b/test/test_console_search.py @@ -38,8 +38,8 @@ def test_search_highlights_clears_prev(): text_object = tutils.get_body_line(f.last_displayed_body, 0) assert text_object.get_text() != ('this is string', [(None, 8), (f.highlight_color, 6)]) -def test_search_highlights_multi_line(): - f = tutils.tflowview(request_contents="this is string\nstring is string") +def test_search_highlights_multi_line(flow=None): + f = flow if flow else tutils.tflowview(request_contents="this is string\nstring is string") # should highlight the first line. f.search("string") @@ -54,7 +54,6 @@ def test_search_highlights_multi_line(): # should highlight third line, second appearance of string. f.search("string") text_object = tutils.get_body_line(f.last_displayed_body, 1) - print(text_object.get_text(), ('string is string', [(None, 10), (f.highlight_color, 6)])) assert text_object.get_text() == ('string is string', [(None, 10), (f.highlight_color, 6)]) def test_search_loops(): @@ -122,15 +121,38 @@ def test_search_backwards(): def test_search_back_multiline(): f = tutils.tflowview(request_contents="this is string\nstring is string") - f.search("string") + # shared assertions. highlight and pointers should now be on the third + # 'string' appearance + test_search_highlights_multi_line(f) + + # should highlight second line, first appearance of string. + f.search_again(backwards=True) + text_object = tutils.get_body_line(f.last_displayed_body, 1) + assert text_object.get_text() == ('string is string', [(None, 0), (f.highlight_color, 6)]) + + # should highlight the first line again. + f.search_again(backwards=True) text_object = tutils.get_body_line(f.last_displayed_body, 0) - first_match = ('this is string', [(None, 8), (f.highlight_color, 6)]) - assert text_object.get_text() == first_match + assert text_object.get_text() == ('this is string', [(None, 8), (f.highlight_color, 6)]) +def test_search_back_multi_multi_line(): + """ + same as above for some bugs the above won't catch. + """ + f = tutils.tflowview(request_contents="this is string\nthis is string\nthis is string") + + f.search("string") f.search_again() + f.search_again() + + # should be on second line + f.search_again(backwards=True) text_object = tutils.get_body_line(f.last_displayed_body, 1) - assert text_object.get_text() == ('string is string', [(None, 0), (f.highlight_color, 6)]) + assert text_object.get_text() == ('this is string', [(None, 8), (f.highlight_color, 6)]) + # first line now f.search_again(backwards=True) text_object = tutils.get_body_line(f.last_displayed_body, 0) - assert text_object.get_text() == first_match + print(text_object.get_text(), ('this is string', [(None, 8), (f.highlight_color, 6)])) + assert text_object.get_text() == ('this is string', [(None, 8), (f.highlight_color, 6)]) + |