[gimp] pygimp: pyconsole: replace raw_input builtin with modal raw input



commit d9bb8d18f98a32eacc78a0b2e2d7045aa0ecf711
Author: Kristian Rietveld <kris loopnest org>
Date:   Thu Apr 28 22:17:02 2016 +0200

    pygimp: pyconsole: replace raw_input builtin with modal raw input
    
    We need to run raw_input in modal mode, such that we can return the
    text that has been entered as the return value. This makes the use
    of "raw_input()" within the pyconsole work as well as makes "help()"
    not get stuck (it was blocking on input on stdin).

 plug-ins/pygimp/plug-ins/pyconsole.py |   34 +++++++++++++++++++++++++++++++-
 1 files changed, 32 insertions(+), 2 deletions(-)
---
diff --git a/plug-ins/pygimp/plug-ins/pyconsole.py b/plug-ins/pygimp/plug-ins/pyconsole.py
index d791da9..2f01c4a 100644
--- a/plug-ins/pygimp/plug-ins/pyconsole.py
+++ b/plug-ins/pygimp/plug-ins/pyconsole.py
@@ -135,6 +135,7 @@ class _ReadLine(object):
 
         self.ps = ''
         self.in_raw_input = False
+        self.in_modal_raw_input = False
         self.run_on_raw_input = None
         self.tab_pressed = 0
         self.history = _ReadLine.History()
@@ -174,6 +175,23 @@ class _ReadLine(object):
             self.run_on_raw_input = None
             self.buffer.insert_at_cursor(run_now + '\n')
 
+    def modal_raw_input(self, text):
+        '''Starts raw input in modal mode. The event loop is spinned until
+        the input is committed. Returns the text entered after the prompt.'''
+        orig_ps = self.ps
+
+        self.raw_input(text)
+        self.in_modal_raw_input = True
+
+        while self.in_modal_raw_input:
+            gtk.main_iteration()
+
+        self.ps = orig_ps
+        self.in_modal_raw_input = False
+        self.in_raw_input = False
+
+        return self.modal_raw_input_result
+
     # Each time the insert mark is modified, move the cursor to it.
     def on_buf_mark_set(self, buffer, iter, mark):
         if mark is not buffer.get_insert():
@@ -470,9 +488,15 @@ class _ReadLine(object):
         self.__move_cursor_to(end)
         self.freeze_undo()
         self.__insert(end, "\n")
-        self.in_raw_input = False
+
         self.history.commit(text)
-        self.do_raw_input(text)
+        if self.in_modal_raw_input:
+            self.in_modal_raw_input = False
+            self.modal_raw_input_result = text
+        else:
+            self.in_raw_input = False
+            self.do_raw_input(text)
+
         self.thaw_undo()
 
     def do_raw_input(self, text):
@@ -488,6 +512,12 @@ class _Console(_ReadLine, code.InteractiveInterpreter):
         code.InteractiveInterpreter.__init__(self, locals)
         self.locals["__console__"] = self
 
+        # The builtin raw_input function reads from stdin, we don't want
+        # this. Therefore, replace this function with our own modal raw
+        # input function.
+        exec "import __builtin__" in self.locals
+        self.locals['__builtin__'].__dict__['raw_input'] = lambda text: self.modal_raw_input(text)
+
         self.start_script = start_script
         self.completer = completer
         self.banner = banner


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]