()
| 375 | if __name__ == "__main__": |
| 376 | |
| 377 | async def interactive_shell(): |
| 378 | shell_cmd, prompt_hint = ("powershell.exe", ">") if _IS_WIN else ("/bin/bash", "$") |
| 379 | |
| 380 | # echo=False → suppress the shell’s own echo of commands |
| 381 | term = TTYSession(shell_cmd) |
| 382 | await term.start() |
| 383 | |
| 384 | timeout = 1.0 |
| 385 | |
| 386 | print(f"Connected to {shell_cmd}.") |
| 387 | print("Type commands for the shell.") |
| 388 | print("• /t=<seconds> → change idle timeout") |
| 389 | print("• /exit → quit helper\n") |
| 390 | |
| 391 | await term.sendline(" ") |
| 392 | print(await term.read_full_until_idle(timeout, timeout), end="", flush=True) |
| 393 | |
| 394 | while True: |
| 395 | try: |
| 396 | user = input(f"(timeout={timeout}) {prompt_hint} ") |
| 397 | except (EOFError, KeyboardInterrupt): |
| 398 | print("\nLeaving…") |
| 399 | break |
| 400 | |
| 401 | if user.lower() == "/exit": |
| 402 | break |
| 403 | if user.startswith("/t="): |
| 404 | try: |
| 405 | timeout = float(user.split("=", 1)[1]) |
| 406 | print(f"[helper] idle timeout set to {timeout}s") |
| 407 | except ValueError: |
| 408 | print("[helper] invalid number") |
| 409 | continue |
| 410 | |
| 411 | idle_timeout = timeout |
| 412 | total_timeout = 10 * idle_timeout |
| 413 | if user == "": |
| 414 | # Just read output, do not send empty line |
| 415 | async for chunk in term.read_chunks_until_idle( |
| 416 | idle_timeout, total_timeout |
| 417 | ): |
| 418 | print(chunk, end="", flush=True) |
| 419 | else: |
| 420 | await term.sendline(user) |
| 421 | async for chunk in term.read_chunks_until_idle( |
| 422 | idle_timeout, total_timeout |
| 423 | ): |
| 424 | print(chunk, end="", flush=True) |
| 425 | |
| 426 | await term.sendline("exit") |
| 427 | await term.wait() |
| 428 | |
| 429 | asyncio.run(interactive_shell()) |
no test coverage detected