From 109cddae12387e87f8d118b363874585dcedfe4f Mon Sep 17 00:00:00 2001 From: Simon Forman Date: Mon, 17 Oct 2022 17:51:12 -0700 Subject: [PATCH] Readline support would be nice. --- implementations/Python/rdln.py | 35 ++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 implementations/Python/rdln.py diff --git a/implementations/Python/rdln.py b/implementations/Python/rdln.py new file mode 100644 index 0000000..08ca3a2 --- /dev/null +++ b/implementations/Python/rdln.py @@ -0,0 +1,35 @@ +from pathlib import Path +import atexit +import os +import readline + +NAMES = set(dir(__builtins__)) + +# Note that we WANT names here to be part of the closure of the +# function so it retains its value from call to call. +def completer(text, state, names=[]): + if 0 == state: + names[:] = filter(lambda name: name.startswith(text), NAMES) + try: + return names[state] + except IndexError: + return + + +readline.set_completer(completer) +readline.parse_and_bind('TAB: complete') + +hfname = str(Path.home() / '.joy_history') +try: + readline.read_history_file(hfname) +except FileNotFoundError: + pass +atexit.register(readline.write_history_file, hfname) + +while True: + try: + i = input('> ') + except (EOFError, KeyboardInterrupt): + i = '' + break + print(i)