From 3e500344282a364f1fbd7245c49d980fe0bfab11 Mon Sep 17 00:00:00 2001 From: Pedro Worcel Date: Sat, 22 Feb 2014 17:15:37 +1300 Subject: [PATCH] fix the wrapping on backward searches --- libmproxy/console/flowview.py | 55 +++++++++++++++++++++-------------- test/test_console_search.py | 20 ++++++++++++- 2 files changed, 52 insertions(+), 23 deletions(-) diff --git a/libmproxy/console/flowview.py b/libmproxy/console/flowview.py index 990dc967e..8f4cb1eb9 100644 --- a/libmproxy/console/flowview.py +++ b/libmproxy/console/flowview.py @@ -365,23 +365,7 @@ class FlowView(common.WWrap): return loop_range - def search_highlight_text(self, text_objects, search_string, looping = False, backwards = False): - start_line, start_index = self.search_get_start(search_string) - i = start_line - - found = False - text_objects = copy.deepcopy(text_objects) - 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 = 0 - - try: - text, style = text_object.get_text() - except AttributeError: - raise SearchError() - + def search_find(self, text, search_string, start_index, backwards): if backwards == False: find_index = text.find(search_string, start_index) else: @@ -392,27 +376,54 @@ class FlowView(common.WWrap): find_index = text.rfind(search_string, 0, start_index) + return find_index + + def search_highlight_text(self, text_objects, search_string, looping = False, backwards = False): + start_line, start_index = self.search_get_start(search_string) + i = start_line + + found = False + text_objects = copy.deepcopy(text_objects) + loop_range = self.search_get_range(len(text_objects), start_line, backwards) + for i in loop_range: + text_object = text_objects[i] + + try: + text, style = text_object.get_text() + except AttributeError: + raise SearchError() + + if i != start_line: + start_index = 0 + + find_index = self.search_find(text, search_string, start_index, backwards) + if find_index != -1: new_text = self.search_highlight_object(text, find_index, search_string) text_objects[i] = new_text + found = True 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 break + # handle search WRAP if found: focus_pos = i else : - if (start_line == 0 and start_index == 0) or looping: + if looping: focus_pos = None else: - self.state.add_flow_setting(self.flow, "last_search_index", 0) - self.state.add_flow_setting(self.flow, "last_find_line", 0) + if not backwards: + self.state.add_flow_setting(self.flow, "last_search_index", 0) + self.state.add_flow_setting(self.flow, "last_find_line", 0) + else: + self.state.add_flow_setting(self.flow, "last_search_index", None) + self.state.add_flow_setting(self.flow, "last_find_line", len(text_objects) - 1) + text_objects, focus_pos = self.search_highlight_text(text_objects, search_string, looping=True, backwards=backwards) diff --git a/test/test_console_search.py b/test/test_console_search.py index 60b998ccd..0e47ef795 100644 --- a/test/test_console_search.py +++ b/test/test_console_search.py @@ -153,6 +153,24 @@ def test_search_back_multi_multi_line(): # first line now f.search_again(backwards=True) text_object = tutils.get_body_line(f.last_displayed_body, 0) - 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)]) + +def test_search_backwards_wraps(): + """ + when searching past line 0, it should loop. + """ + f = tutils.tflowview(request_contents="this is string\nthis is string\nthis is string") + + # should be on second line + f.search("string") + f.search_again() + text_object = tutils.get_body_line(f.last_displayed_body, 1) + assert text_object.get_text() == ('this is string', [(None, 8), (f.highlight_color, 6)]) + + # should be on third now. + f.search_again(backwards=True) + message = f.search_again(backwards=True) + + text_object = tutils.get_body_line(f.last_displayed_body, 2) assert text_object.get_text() == ('this is string', [(None, 8), (f.highlight_color, 6)])