diff --git a/bbot/cli.py b/bbot/cli.py index 9a8ab51dd..c64d242ff 100755 --- a/bbot/cli.py +++ b/bbot/cli.py @@ -6,9 +6,9 @@ import asyncio import logging import traceback -from aioconsole import ainput from omegaconf import OmegaConf from contextlib import suppress +from aioconsole.stream import NonFileStreamReader # fix tee buffering sys.stdout.reconfigure(line_buffering=True) @@ -20,6 +20,7 @@ from bbot import __version__ from bbot.modules import module_loader from bbot.core.configurator.args import parser +from bbot.core.helpers.misc import smart_decode from bbot.core.helpers.logger import log_to_stderr from bbot.core.configurator import ensure_config_files, check_cli_args, environ @@ -321,21 +322,28 @@ def handle_keyboard_input(keyboard_input): toggle_log_level(logger=log) scanner.manager.modules_status(_log=True) + reader = NonFileStreamReader(sys.stdin) + async def akeyboard_listen(): - allowed_errors = 10 - while 1: - keyboard_input = "a" - try: - keyboard_input = await ainput() - except Exception: - allowed_errors -= 1 - handle_keyboard_input(keyboard_input) - if allowed_errors <= 0: - break + try: + allowed_errors = 10 + while 1: + keyboard_input = None + try: + keyboard_input = smart_decode((await reader.readline()).strip()) + allowed_errors = 0 + except Exception: + allowed_errors -= 1 + if keyboard_input is not None: + handle_keyboard_input(keyboard_input) + if allowed_errors <= 0: + break + except Exception as e: + log_to_stderr(f"Error in keyboard listen task: {e}", level="ERROR") + log_to_stderr(traceback.format_exc(), level="TRACE") try: keyboard_listen_task = asyncio.create_task(akeyboard_listen()) - await scanner.async_start_without_generator() finally: keyboard_listen_task.cancel()