Skip to content

Instantly share code, notes, and snippets.

@jpic
Created September 29, 2011 22:23
Show Gist options
  • Save jpic/1252105 to your computer and use it in GitHub Desktop.
Save jpic/1252105 to your computer and use it in GitHub Desktop.
Non blocking GNU readline usage example for python, proof of concept
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 = '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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment