add tab-completion to twisted-style input_code()

This commit is contained in:
Brian Warner 2016-04-15 17:27:32 -07:00
parent c856081a58
commit 94a3be91b2
2 changed files with 30 additions and 1 deletions

View File

@ -60,7 +60,7 @@ class TwistedReceiver(BlockingReceiver):
@inlineCallbacks
def _go(self, w, tor_manager):
self.handle_code(w)
yield self.handle_code(w)
verifier = yield w.get_verifier()
self.show_verifier(verifier)
them_d = yield self.get_data(w)
@ -90,6 +90,17 @@ class TwistedReceiver(BlockingReceiver):
returnValue(1)
returnValue(0)
@inlineCallbacks
def handle_code(self, w):
code = self.args.code
if self.args.zeromode:
assert not code
code = u"0-"
if not code:
code = yield w.input_code("Enter receive wormhole code: ",
self.args.code_length)
yield w.set_code(code)
@inlineCallbacks
def get_data(self, w):
try:

View File

@ -4,6 +4,7 @@ from six.moves.urllib_parse import urlencode
from binascii import hexlify, unhexlify
from zope.interface import implementer
from twisted.internet import reactor, defer
from twisted.internet.threads import deferToThread, blockingCallFromThread
from twisted.internet.defer import inlineCallbacks, returnValue
from twisted.web import client as web_client
from twisted.web import error as web_error
@ -319,6 +320,23 @@ class Wormhole:
self._start()
returnValue(code)
@inlineCallbacks
def input_code(self, prompt="Enter wormhole code: ", code_length=2):
def _lister():
return blockingCallFromThread(reactor,
self._channel_manager.list_channels)
# fetch the list of channels ahead of time, to give us a chance to
# discover the welcome message (and warn the user about an obsolete
# client)
initial_channelids = yield self._channel_manager.list_channels()
_start = self._timing.add_event("input code", waiting="user")
code = yield deferToThread(codes.input_code_with_completion,
prompt,
initial_channelids, _lister,
code_length)
self._timing.finish_event(_start)
returnValue(code)
def set_code(self, code):
if not isinstance(code, type(u"")): raise TypeError(type(code))
if self.code is not None: raise UsageError