from collections import namedtuple
import queue
import shlex
import threading

from prompt_toolkit import prompt
from prompt_toolkit.history import InMemoryHistory
from prompt_toolkit.styles import style_from_dict
from prompt_toolkit.token import Token

Display = namedtuple('Display', 'type')
Exit = namedtuple('Exit', '')
Skip = namedtuple('Skip', '')


class Prompt:
    def __init__(self):
        self.history = InMemoryHistory()
        self.q = queue.Queue()
        self.shutdown = threading.Event()
        self.thread = threading.Thread(target=self.run)
        self.run_name = ''

    def start(self):
        self.thread.start()

    def stop(self):
        self.shutdown.set()

    def __del__(self):
        self.stop()

    def set_run_name(self, run):
        self.run_name = run

    def get_bottom_toolbar_tokens(self, cli):
        return [(Token.Toolbar, 'Run '),
                (Token.Name, self.run_name),
                (Token.Toolbar, ' in progress.')]

    def run(self):
        style = style_from_dict({
            Token.Toolbar: '#ccc bg:#333',
            Token.Name: '#fff bold bg:#333',
        })

        while not self.shutdown.is_set():
            try:
                text = prompt('> ', history=self.history, patch_stdout=True, style=style,
                              get_bottom_toolbar_tokens=self.get_bottom_toolbar_tokens)
                cmd = shlex.split(text)
                if not cmd:
                    continue
                elif cmd[0] in ('exit', 'quit'):
                    self.q.put(Exit())
                    return
                elif cmd[0] == 'help':
                    print('Help text forthcoming.')
                elif cmd[0] == 'skip':
                    self.q.put(Skip())
                else:
                    print('Unknown command. Try \'help\'.')
            except KeyboardInterrupt:
                continue
            except EOFError:
                self.q.put(Exit())
                return


class PromptResponder:
    def __init__(self, q):
        self.q = q

    def __call__(self):
        try:
            while True:
                event = self.q.get(block=False)
                if isinstance(event, Exit):
                    raise KeyboardInterrupt()
                elif isinstance(event, Skip):
                    return event
        except queue.Empty:
            pass

@@ -99,22 +99,36 @@
  import 
-prompt
+CommandLineInterface
 %0Afrom pr
@@ -221,16 +221,97 @@
 om_dict%0A
+from prompt_toolkit.shortcuts import create_eventloop, create_prompt_application%0A
 from pro
@@ -486,49 +486,8 @@
 f):%0A
-        self.history = InMemoryHistory()%0A
     
@@ -1192,41 +1192,35 @@
     
-w
 hi
-le not self.shutdown.is_set
+story = InMemoryHistory
 ()
-:
 %0A   
@@ -1228,46 +1228,78 @@
     
-    try:%0A                text = prompt
+eventloop = create_eventloop()%0A        app = create_prompt_application
 ('%3E 
@@ -1313,21 +1313,16 @@
 ory=
-self.
 history,
  pat
@@ -1321,27 +1321,8 @@
 ory,
- patch_stdout=True,
  sty
@@ -1331,16 +1331,26 @@
 =style,%0A
+          
         
@@ -1429,16 +1429,218 @@
 tokens)%0A
+        cli = CommandLineInterface(app, eventloop)%0A%0A        with cli.patch_stdout_context(raw=True):%0A            while not self.shutdown.is_set():%0A                try:%0A                    cli.run()%0A    
         
@@ -1669,14 +1669,99 @@
 lit(
+cli.return_value().
 text)%0A
+                    app.buffer.reset(append_to_history=True)%0A%0A    
     
@@ -1796,32 +1796,36 @@
                 
+    
 continue%0A       
@@ -1809,32 +1809,36 @@
        continue%0A
+    
                 
@@ -1882,32 +1882,36 @@
                 
+    
 self.q.put(Exit(
@@ -1933,23 +1933,31 @@
         
+   
+ 
 return%0A
+    
         
@@ -2003,24 +2003,28 @@
             
+   
+ 
 print('Help 
@@ -2051,32 +2051,36 @@
                 
+    
 elif cmd%5B0%5D == '
@@ -2098,32 +2098,36 @@
                 
+  
+  
 self.q.put(Skip(
@@ -2145,16 +2145,20 @@
         
+    
 else:%0A  
@@ -2171,24 +2171,28 @@
             
+  
+  
 print('Unkno
@@ -2227,24 +2227,28 @@
             
+    
 except Keybo
@@ -2269,32 +2269,36 @@
                 
+ 
+   
 continue%0A       
@@ -2282,32 +2282,36 @@
        continue%0A
+    
             exce
@@ -2331,32 +2331,36 @@
                 
+    
 self.q.put(Exit(
@@ -2354,32 +2354,36 @@
 f.q.put(Exit())%0A
+    
                 

Allow ANSI color to work with --prompt