From 3e500344282a364f1fbd7245c49d980fe0bfab11 Mon Sep 17 00:00:00 2001 From: Pedro Worcel Date: Sat, 22 Feb 2014 17:15:37 +1300 Subject: fix the wrapping on backward searches --- libmproxy/console/flowview.py | 41 ++++++++++++++++++++++++++--------------- test/test_console_search.py | 20 +++++++++++++++++++- 2 files changed, 45 insertions(+), 16 deletions(-) diff --git a/libmproxy/console/flowview.py b/libmproxy/console/flowview.py index 990dc967..8f4cb1eb 100644 --- a/libmproxy/console/flowview.py +++ b/libmproxy/console/flowview.py @@ -365,6 +365,19 @@ class FlowView(common.WWrap): return loop_range + def search_find(self, text, search_string, start_index, backwards): + if backwards == False: + find_index = text.find(search_string, start_index) + else: + if start_index != 0: + start_index -= len(search_string) + else: + start_index = None + + 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 @@ -374,45 +387,43 @@ class FlowView(common.WWrap): 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() - if backwards == False: - find_index = text.find(search_string, start_index) - else: - if start_index != 0: - start_index -= len(search_string) - else: - start_index = None + if i != start_line: + start_index = 0 - find_index = text.rfind(search_string, 0, start_index) + 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 60b998cc..0e47ef79 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)]) -- cgit v1.2.3