#!/usr/bin/env python

'''
'''

__docformat__ = 'restructuredtext'
__version__ = '$Id$'

from code import InteractiveInterpreter
import sys

from pyglet.GL.VERSION_1_1 import *
from pyglet.layout import *
from pyglet.window import *
from pyglet.window.event import *
from pyglet.window.key import *
from pyglet.clock import *

from pyglet.text import *
from pyglet.layout import *

data = '''<?xml version="1.0"?>
<html>
  <head>
    <style>
      #interpreter-block {
          white-space: pre;
          font-family: monospace;
          border-left: 5px solid silver;
          border-right: 5px solid silver;
      }
      #interpreter-block #cursor {
          white-space: pre;
      }
      #interpreter {
          white-space: pre;
      }
    </style>
  </head>
  <body>
    <h1>Interpreter example</h1>
    <div id="interpreter-block"><span
id="interpreter" /><span id="cursor"> </span></div>
  </body>
</html>
'''

class DOMInterpreter(InteractiveInterpreter):
    class Stream(object):
        def __init__(self, interpreter):
            self.interpreter = interpreter
        
        def write(self, data):
            self.interpreter.write(data)

    def __init__(self, element):
        InteractiveInterpreter.__init__(self)
        self.element = element
        self.buffer = ''
        self.stream = DOMInterpreter.Stream(self)

        self.write('pyglet interpreter\n')
        self.write('>>> ')

    def write(self, data):
        self.element.add_text(data)

    def input(self, input):
        print 'BEFORE', (self.element.text, )
        _stdout = sys.stdout
        sys.stdout = self.stream

        self.write(input)
        self.buffer += input
        if '\n' in self.buffer:
            source, self.buffer = self.buffer.rsplit('\n', 1)
            print >> _stdout, ('run', source, self.buffer)
            prompt = self.runsource(source)
            print >> _stdout, ('...', prompt)

            if prompt:
                self.write('... ')
            else:
                self.write('>>> ')

        sys.stdout = _stdout
        print 'AFTER', (self.element.text, )

    def backspace(self):
        self.buffer = self.buffer[:-1]
        # There is no easy way to change element text yet...
        self.element.text = self.element.text[:-1]
        self.element.document.element_modified(self.element)

window = Window(visible=False, vsync=False)

layout = Layout()
layout.set_xhtml(data)
window.push_handlers(layout)

interp = DOMInterpreter(layout.document.get_element('interpreter'))

def on_text(text):
    interp.input(text.replace('\r', '\n'))
window.push_handlers(on_text)

def on_key_press(symbol, modifiers):
    if symbol == K_BACKSPACE:
        interp.backspace()
    else:
        return True
window.push_handlers(on_key_press)

def blink_cursor(dt):
    cursor = layout.document.get_element('cursor')
    if cursor.style['background-color']:
        del cursor.style['background-color']
    else:
        cursor.style['background-color'] = 'black'
clock = Clock()
clock.schedule_interval(blink_cursor, 0.5)

glClearColor(1, 1, 1, 1)
window.set_visible()

while not window.has_exit:
    window.dispatch_events()
    clock.tick()
    glClear(GL_COLOR_BUFFER_BIT)
    layout.draw()

    window.flip()

@@ -1343,16 +1343,41 @@
 am(self)
+%0A        self.source = %5B%5D
 %0A%0A      
@@ -1537,54 +1537,8 @@
 t):%0A
-        print 'BEFORE', (self.element.text, )%0A
     
@@ -1761,143 +1761,93 @@
     
-print %3E%3E _stdout, ('run', source, self.buffer)%0A            prompt = self.runsource(source)%0A            print %3E%3E _stdout, ('...', prompt
+self.source.append(source)%0A            prompt = self.runsource('%5Cn'.join(self.source)
 )%0A%0A 
@@ -1913,32 +1913,65 @@
           else:%0A
+                self.source = %5B%5D%0A
                 
@@ -1986,24 +1986,24 @@
 te('%3E%3E%3E ')%0A%0A
+
         sys.
@@ -2022,53 +2022,8 @@
 dout
-%0A        print 'AFTER', (self.element.text, )
 %0A%0A  

Fix contiuation in interpreter.