I am trying to write a syntax highlighted version of an interactive interpreter.
Here is what I still have that works quite well (uses blessings and pygments ) ...
import code
import readline
import threading
import blessings
from pygments import highlight
from pygments.formatters import TerminalFormatter
from pygments.lexers import PythonLexer
def check_line():
global current_line
try:
while True:
line = readline.get_line_buffer()
if line != current_line:
current_line = line
do_colour()
except:
pass
def do_colour():
global terminal
raw_line = readline.get_line_buffer()
line = highlight(raw_line, lexer, formatter)[:-1]
with terminal.location(x = 4):
print line,
readline.redisplay()
current_line = ''
terminal = blessings.Terminal()
lexer = PythonLexer()
formatter = TerminalFormatter()
console = code.InteractiveConsole()
colour_thread = threading.Thread(target=check_line)
colour_thread.setDaemon(True)
colour_thread.start()
console.interact('')
It colors the lines as they are entered ...

Problems:
- In this case, a separate thread is used to check the employment for changes in the line (I think it could be using the whole kernel).
- If you move the cursor backward along the line and then move it to the right, the terminal redraws the character that was simply not selected and does it in white
, , / - ? stdin , - raw_input, ?
P.S ("""like this""") , .
Edit:
, , ...
import code, ctypes, readline, blessings, signal, threading
from pygments import highlight
from pygments.formatters import TerminalFormatter
from pygments.lexers import PythonLexer
def slight_delay():
threading.Timer(0.001, draw).start()
return 0
def draw():
raw_line = readline.get_line_buffer()
line = highlight(raw_line, lexer, formatter)[:-1]
with lock:
with terminal.location(x = 4):
print line,
readline.redisplay()
def keyboard_interrupt(c, frame):
pass
callback = ctypes.PYFUNCTYPE(ctypes.c_int)(slight_delay)
hook_ptr = ctypes.c_void_p.in_dll(ctypes.pythonapi,"PyOS_InputHook")
hook_ptr.value = ctypes.cast(callback, ctypes.c_void_p).value
signal.signal(signal.SIGINT, keyboard_interrupt)
terminal = blessings.Terminal()
lexer = PythonLexer()
formatter = TerminalFormatter()
console = code.InteractiveConsole()
lock = threading.Lock()
console.interact('')
, , PyOS_InputHook,
, , () .
, ; , stdin ( ), . , 0,1 .
, 0.001 .