From 7806a3485893e5b54d701fd38e3cb62d044d4091 Mon Sep 17 00:00:00 2001 From: Vane11ope Date: Thu, 2 Jul 2020 09:46:24 +0900 Subject: [PATCH 1/4] List was not cycled right for tab autocompletion --- mitmproxy/tools/console/commander/commander.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/mitmproxy/tools/console/commander/commander.py b/mitmproxy/tools/console/commander/commander.py index 0feae28ea..9d2ccb64a 100644 --- a/mitmproxy/tools/console/commander/commander.py +++ b/mitmproxy/tools/console/commander/commander.py @@ -28,15 +28,17 @@ class ListCompleter(Completer): if o.startswith(start): self.options.append(o) self.options.sort() - self.offset = 0 + self.pos = None def cycle(self, forward: bool = True) -> str: if not self.options: return self.start - ret = self.options[self.offset] - delta = 1 if forward else -1 - self.offset = (self.offset + delta) % len(self.options) - return ret + if self.pos is None: + self.pos = 0 if forward else len(self.options) - 1 + else: + delta = 1 if forward else -1 + self.pos = (self.pos + delta) % len(self.options) + return self.options[self.pos] class CompletionState(typing.NamedTuple): From e43c4e3cb1227963a401170b0b147854705c60fb Mon Sep 17 00:00:00 2001 From: Vane11ope Date: Thu, 2 Jul 2020 11:31:04 +0900 Subject: [PATCH 2/4] Fix a type error --- mitmproxy/tools/console/commander/commander.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/mitmproxy/tools/console/commander/commander.py b/mitmproxy/tools/console/commander/commander.py index 9d2ccb64a..85e488273 100644 --- a/mitmproxy/tools/console/commander/commander.py +++ b/mitmproxy/tools/console/commander/commander.py @@ -1,3 +1,4 @@ +import sys import abc import typing @@ -28,12 +29,12 @@ class ListCompleter(Completer): if o.startswith(start): self.options.append(o) self.options.sort() - self.pos = None + self.pos = sys.maxsize def cycle(self, forward: bool = True) -> str: if not self.options: return self.start - if self.pos is None: + if self.pos == sys.maxsize: self.pos = 0 if forward else len(self.options) - 1 else: delta = 1 if forward else -1 From 2e0c078e7cc2b8cd0722f27a7612a016838aa829 Mon Sep 17 00:00:00 2001 From: Vane11ope Date: Sun, 5 Jul 2020 04:55:10 +0900 Subject: [PATCH 3/4] Use -1 as an initial value for the pos --- mitmproxy/tools/console/commander/commander.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/mitmproxy/tools/console/commander/commander.py b/mitmproxy/tools/console/commander/commander.py index 85e488273..47cf1962a 100644 --- a/mitmproxy/tools/console/commander/commander.py +++ b/mitmproxy/tools/console/commander/commander.py @@ -1,4 +1,3 @@ -import sys import abc import typing @@ -29,12 +28,12 @@ class ListCompleter(Completer): if o.startswith(start): self.options.append(o) self.options.sort() - self.pos = sys.maxsize + self.pos = -1 def cycle(self, forward: bool = True) -> str: if not self.options: return self.start - if self.pos == sys.maxsize: + if self.pos == -1: self.pos = 0 if forward else len(self.options) - 1 else: delta = 1 if forward else -1 From ecf076cb2413bd71083841fee1b522131a9dea40 Mon Sep 17 00:00:00 2001 From: Vane11ope Date: Sun, 5 Jul 2020 10:40:48 +0900 Subject: [PATCH 4/4] Add tests for list completer --- test/mitmproxy/tools/console/test_commander.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/test/mitmproxy/tools/console/test_commander.py b/test/mitmproxy/tools/console/test_commander.py index 839f81e52..c096325be 100644 --- a/test/mitmproxy/tools/console/test_commander.py +++ b/test/mitmproxy/tools/console/test_commander.py @@ -31,23 +31,35 @@ class TestListCompleter: [ "", ["a", "b", "c"], - ["a", "b", "c", "a"] + ["a", "b", "c", "a"], + ["c", "b", "a", "c"], + ["a", "c", "a", "c"] ], [ "xxx", ["a", "b", "c"], + ["xxx", "xxx", "xxx"], + ["xxx", "xxx", "xxx"], ["xxx", "xxx", "xxx"] ], [ "b", ["a", "b", "ba", "bb", "c"], - ["b", "ba", "bb", "b"] + ["b", "ba", "bb", "b"], + ["bb", "ba", "b", "bb"], + ["b", "bb", "b", "bb"] ], ] - for start, opts, cycle in tests: + for start, opts, cycle, cycle_reverse, cycle_mix in tests: c = commander.ListCompleter(start, opts) for expected in cycle: assert c.cycle() == expected + for expected in cycle_reverse: + assert c.cycle(False) == expected + forward = True + for expected in cycle_mix: + assert c.cycle(forward) == expected + forward = not forward class TestCommandEdit: