Skip to content

Commit d35918a

Browse files
authored
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
1 parent a1930e4 commit d35918a

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

python/src/typechat/_internal/interactive.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import sys
21
from typing import Callable, Awaitable
32

43
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,
2120
print(interactive_prompt + line)
2221
await process_request(line)
2322
else:
24-
print(interactive_prompt, end="", flush=True)
25-
for line in sys.stdin:
26-
lower_line = line.lower().strip()
27-
if lower_line == "quit" or lower_line == "exit":
23+
# Use readline to enable input editing and history
24+
import readline # type: ignore
25+
while True:
26+
try:
27+
line = input(interactive_prompt)
28+
except EOFError:
29+
print("\n")
30+
break
31+
if line.lower().strip() in ("quit", "exit"):
2832
break
2933
else:
3034
await process_request(line)
31-
print(interactive_prompt, end="", flush=True)
32-
33-

0 commit comments

Comments
 (0)