Skip to content

Commit

Permalink
Use readline for interactive input (#235)
Browse files Browse the repository at this point in the history
* Use readline and input() for interactive loop

* More idiomatic check for quit or exit

* Satisfy pyright

* Remove unused import sys
  • Loading branch information
gvanrossum authored Apr 17, 2024
1 parent a1930e4 commit d35918a
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions python/src/typechat/_internal/interactive.py
Original file line number Diff line number Diff line change
@@ -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]]):
Expand All @@ -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)


0 comments on commit d35918a

Please sign in to comment.