#!/usr/bin/python # Don't recall where I cribbed this from, because I didn't make a # note of it. Stackoverflow maybe. import sys from select import select import socket import readline # HACK - python doesn't include a binding to rl_callback_read_char import readline import ctypes rl_lib = ctypes.cdll.LoadLibrary("libreadline.so") readline.callback_handler_remove = rl_lib.rl_callback_handler_remove readline.callback_read_char = rl_lib.rl_callback_read_char # the callback needs special treatment: rlcallbackfunctype = ctypes.CFUNCTYPE(ctypes.c_void_p, ctypes.c_char_p) def setcallbackfunc(prompt, thefunc): rl_lib.rl_callback_handler_install(prompt, rlcallbackfunctype(thefunc)) readline.callback_handler_install = setcallbackfunc # ENDHACK PROMPT = b'fics> ' def erase_prompt(): print(chr(8)*len(PROMPT)) def print_prompt(): readline.callback_handler_remove() setcallbackfunc(PROMPT, process_input) def process_input(input): input = input.strip() if not len(input): return erase_prompt() print ('ok, you said: ', input) print_prompt() print_prompt() while True: r, w, x = select([sys.stdin], [], []) if sys.stdin in r: readline.callback_read_char()