From d35918a3278f7cf1c9c25babd1407681de2d1da2 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Wed, 17 Apr 2024 16:48:21 -0700 Subject: [PATCH] Use readline for interactive input (#235) * Use readline and input() for interactive loop * More idiomatic check for quit or exit * Satisfy pyright * Remove unused import sys --- python/src/typechat/_internal/interactive.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/python/src/typechat/_internal/interactive.py b/python/src/typechat/_internal/interactive.py index cc8c9349..8ce7ca6d 100644 --- a/python/src/typechat/_internal/interactive.py +++ b/python/src/typechat/_internal/interactive.py @@ -1,4 +1,3 @@ -import sys from typing import Callable, Awaitable async def process_requests(interactive_prompt: str, input_file_name: str | None, process_request: Callable[[str], Awaitable[None]]): @@ -21,13 +20,15 @@ async def process_requests(interactive_prompt: str, input_file_name: str | None, print(interactive_prompt + line) await process_request(line) else: - print(interactive_prompt, end="", flush=True) - for line in sys.stdin: - lower_line = line.lower().strip() - if lower_line == "quit" or lower_line == "exit": + # Use readline to enable input editing and history + import readline # type: ignore + while True: + try: + line = input(interactive_prompt) + except EOFError: + print("\n") + break + if line.lower().strip() in ("quit", "exit"): break else: await process_request(line) - print(interactive_prompt, end="", flush=True) - - \ No newline at end of file