From b3763f9e78db8d37f932333bb482aba8df2132d4 Mon Sep 17 00:00:00 2001 From: Brian Warner Date: Fri, 6 Jan 2017 12:05:34 -0500 Subject: [PATCH] show "you can use tabs" reminder if code was input without completion refs #15 --- src/wormhole/codes.py | 13 ++++++++++--- src/wormhole/wormhole.py | 16 +++++++++++----- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/wormhole/codes.py b/src/wormhole/codes.py index 340fa03..dfa4625 100644 --- a/src/wormhole/codes.py +++ b/src/wormhole/codes.py @@ -19,12 +19,14 @@ def extract_channel_id(code): return channel_id class CodeInputter: - def __init__(self, initial_channelids, get_channel_ids, code_length): + def __init__(self, initial_channelids, get_channel_ids, code_length, + used_completion_f): self._initial_channelids = initial_channelids self._get_channel_ids = get_channel_ids self.code_length = code_length self.last_text = None # memoize for a speedup self.last_matches = None + self._used_completion_f = used_completion_f def get_current_channel_ids(self): if self._initial_channelids is not None: @@ -43,6 +45,7 @@ class CodeInputter: raise e def completer(self, text, state): + self._used_completion_f() #if state == 0: # print("", file=sys.stderr) #print("completer: '%s' %d '%d'" % (text, state, @@ -84,9 +87,13 @@ class CodeInputter: def input_code_with_completion(prompt, initial_channelids, get_channel_ids, code_length): + used_completion = [] + def used_completion_f(): + used_completion.append(True) try: import readline - c = CodeInputter(initial_channelids, get_channel_ids, code_length) + c = CodeInputter(initial_channelids, get_channel_ids, code_length, + used_completion_f) if "libedit" in readline.__doc__: readline.parse_and_bind("bind ^I rl_complete") else: @@ -99,7 +106,7 @@ def input_code_with_completion(prompt, initial_channelids, get_channel_ids, # Code is str(bytes) on py2, and str(unicode) on py3. We want unicode. if isinstance(code, bytes): code = code.decode("utf-8") - return code + return (code, bool(used_completion)) if __name__ == "__main__": code = input_code_with_completion("Enter wormhole code: ", diff --git a/src/wormhole/wormhole.py b/src/wormhole/wormhole.py index 15d8eba..7c6de48 100644 --- a/src/wormhole/wormhole.py +++ b/src/wormhole/wormhole.py @@ -123,12 +123,15 @@ class _InputCode: with self._timing.add("input code", waiting="user"): t = self._reactor.addSystemEventTrigger("before", "shutdown", self._warn_readline) - code = yield deferToThread(codes.input_code_with_completion, - self._prompt, - initial_nameplate_ids, - self._list_blocking, - self._code_length) + res = yield deferToThread(codes.input_code_with_completion, + self._prompt, + initial_nameplate_ids, + self._list_blocking, + self._code_length) + (code, used_completion) = res self._reactor.removeSystemEventTrigger(t) + if not used_completion: + self._remind_about_tab() returnValue(code) def _response_handle_nameplates(self, msg): @@ -176,6 +179,9 @@ class _InputCode: # doesn't see the signal, and we must still wait for stdin to make # readline finish. + def _remind_about_tab(self): + print(" (note: you can use to complete words)", file=self._stderr) + class _WelcomeHandler: def __init__(self, url, current_version, signal_error): self._ws_url = url