move to promp_toolkit

This commit is contained in:
Fabian Dill
2020-03-13 03:53:20 +01:00
parent 2c8d4e550c
commit 2a05b9cd72
4 changed files with 90 additions and 74 deletions

View File

@@ -19,12 +19,8 @@ ModuleUpdate.update()
import colorama
import websockets
import aioconsole
try:
import tqdm
except:
tqdm = None
import prompt_toolkit
from prompt_toolkit.patch_stdout import patch_stdout
import Items
import Regions
@@ -439,12 +435,13 @@ async def snes_connect(ctx : Context, address):
logging.error(f"Error connecting to snes, attempt again in {RECONNECT_DELAY}s")
asyncio.create_task(snes_autoreconnect(ctx))
async def snes_autoreconnect(ctx: Context):
if tqdm:
for _ in tqdm.trange(100, unit='%', leave=False):
await asyncio.sleep(RECONNECT_DELAY/100)
else:
await asyncio.sleep(RECONNECT_DELAY)
# unfortunately currently broken. See: https://github.com/prompt-toolkit/python-prompt-toolkit/issues/1033
# with prompt_toolkit.shortcuts.ProgressBar() as pb:
# for _ in pb(range(100)):
# await asyncio.sleep(RECONNECT_DELAY/100)
await asyncio.sleep(RECONNECT_DELAY)
if ctx.snes_reconnect_address and ctx.snes_socket is None:
await snes_connect(ctx, ctx.snes_reconnect_address)
@@ -629,12 +626,13 @@ async def server_loop(ctx : Context, address = None):
logging.info(f"... reconnecting in {RECONNECT_DELAY}s")
asyncio.create_task(server_autoreconnect(ctx))
async def server_autoreconnect(ctx: Context):
if tqdm:
for _ in tqdm.trange(100, unit='%', leave=False):
await asyncio.sleep(RECONNECT_DELAY/100)
else:
await asyncio.sleep(RECONNECT_DELAY)
# unfortunately currently broken. See: https://github.com/prompt-toolkit/python-prompt-toolkit/issues/1033
# with prompt_toolkit.shortcuts.ProgressBar() as pb:
# for _ in pb(range(100)):
# await asyncio.sleep(RECONNECT_DELAY/100)
await asyncio.sleep(RECONNECT_DELAY)
if ctx.server_address and ctx.server_task is None:
ctx.server_task = asyncio.create_task(server_loop(ctx))
@@ -764,63 +762,68 @@ async def connect(ctx: Context, address=None):
await disconnect(ctx)
ctx.server_task = asyncio.create_task(server_loop(ctx, address))
async def console_loop(ctx : Context):
session = prompt_toolkit.PromptSession()
while not ctx.exit_event.is_set():
input = await aioconsole.ainput()
try:
with patch_stdout():
input_text = await session.prompt_async()
if ctx.input_requests > 0:
ctx.input_requests -= 1
ctx.input_queue.put_nowait(input)
continue
if ctx.input_requests > 0:
ctx.input_requests -= 1
ctx.input_queue.put_nowait(input_text)
continue
command = input.split()
if not command:
continue
command = input_text.split()
if not command:
continue
if command[0][:1] != '/':
asyncio.create_task(send_msgs(ctx.socket, [['Say', input]]))
continue
if command[0][:1] != '/':
asyncio.create_task(send_msgs(ctx.socket, [['Say', input_text]]))
continue
precommand = command[0][1:]
precommand = command[0][1:]
if precommand == 'exit':
ctx.exit_event.set()
if precommand == 'exit':
ctx.exit_event.set()
elif precommand == 'snes':
ctx.snes_reconnect_address = None
asyncio.create_task(snes_connect(ctx, command[1] if len(command) > 1 else ctx.snes_address))
elif precommand == 'snes':
ctx.snes_reconnect_address = None
asyncio.create_task(snes_connect(ctx, command[1] if len(command) > 1 else ctx.snes_address))
elif precommand in {'snes_close', 'snes_quit'}:
ctx.snes_reconnect_address = None
if ctx.snes_socket is not None and not ctx.snes_socket.closed:
await ctx.snes_socket.close()
elif precommand in {'snes_close', 'snes_quit'}:
ctx.snes_reconnect_address = None
if ctx.snes_socket is not None and not ctx.snes_socket.closed:
await ctx.snes_socket.close()
elif precommand in {'connect', 'reconnect'}:
ctx.server_address = None
asyncio.create_task(connect(ctx, command[1] if len(command) > 1 else None))
elif precommand in {'connect', 'reconnect'}:
ctx.server_address = None
asyncio.create_task(connect(ctx, command[1] if len(command) > 1 else None))
elif precommand == 'disconnect':
ctx.server_address = None
asyncio.create_task(disconnect(ctx))
elif precommand == 'disconnect':
ctx.server_address = None
asyncio.create_task(disconnect(ctx))
elif precommand == 'received':
logging.info('Received items:')
for index, item in enumerate(ctx.items_received, 1):
logging.info('%s from %s (%s) (%d/%d in list)' % (
color(get_item_name_from_id(item.item), 'red', 'bold'),
color(ctx.player_names[item.player], 'yellow'),
get_location_name_from_address(item.location), index, len(ctx.items_received)))
elif precommand == 'received':
logging.info('Received items:')
for index, item in enumerate(ctx.items_received, 1):
logging.info('%s from %s (%s) (%d/%d in list)' % (
color(get_item_name_from_id(item.item), 'red', 'bold'),
color(ctx.player_names[item.player], 'yellow'),
get_location_name_from_address(item.location), index, len(ctx.items_received)))
elif precommand == 'missing':
for location in [k for k, v in Regions.location_table.items() if type(v[0]) is int]:
if location not in ctx.locations_checked:
logging.info('Missing: ' + location)
elif precommand == "license":
with open("LICENSE") as f:
logging.info(f.read())
elif precommand == 'missing':
for location in [k for k, v in Regions.location_table.items() if type(v[0]) is int]:
if location not in ctx.locations_checked:
logging.info('Missing: ' + location)
elif precommand == "license":
with open("LICENSE") as f:
logging.info(f.read())
except Exception as e:
logging.exception(e)
await snes_flush_writes(ctx)
def get_item_name_from_id(code):