Pārlūkot izejas kodu

use curses for console IO

Martin 10 gadi atpakaļ
vecāks
revīzija
41104b2ed7

+ 2 - 1
voice_control/input_sources/console.py

@@ -1,4 +1,5 @@
 import base
+import util.curses_util
 
 class ConsoleInput(base.InputSource):
     def __init__(self, token_stream):
@@ -7,7 +8,7 @@ class ConsoleInput(base.InputSource):
     def run(self):
         while True:
             try:
-                cmd = raw_input('> ')
+                cmd = util.curses_util.read_str()
                 for token in cmd.split(' '):
                     token = token.strip()
                     if token:

+ 22 - 3
voice_control/main.py

@@ -3,6 +3,7 @@ import sys
 import signal
 import input_sources
 import services
+import util.curses_util
 
 _INTERRUPTED = False
 
@@ -16,10 +17,28 @@ def _interrupt_callback():
 
 def _init_logging():
     logging.basicConfig(stream=sys.stdout)
-    logging.getLogger().setLevel(logging.DEBUG)
+    logging.getLogger().setLevel(logging.INFO)
     signal.signal(signal.SIGINT, _signal_handler)
 
-if __name__ == "__main__":
+def cb():
+    print 'CB!'
+
+def test():
+    import snowboy.snowboydecoder
+    model = 'lib/snowboy/resources/alexa.umdl'
+    detector = snowboy.snowboydecoder.HotwordDetector(model, sensitivity=0.5)
+    print 'a'
+    detector.start(detected_callback=cb, interrupt_check=_interrupt_callback)
+    print 'b'
+    detector.terminate()
+    
+    print 'c'
+
+def _run():
     _init_logging()
     input_sources.launch_input_sources()
-    services.launch_services(_interrupt_callback)
+    services.launch_services(_interrupt_callback)
+
+if __name__ == "__main__":
+    util.curses_util.run(_run)
+    #test()

+ 8 - 7
voice_control/output/console.py

@@ -1,24 +1,25 @@
 import logging
 import base
+import util.curses_util
 
 class Console(base.Base):
     def failure(self, s=None):
         if s is None:
-            logging.error('Failed.')
+            util.curses_util.print_str('Failed.')
         else:
-            logging.error(s)
+            util.curses_util.print_str(s)
         
     def tts(self, s):
-        logging.info(s)
+        util.curses_util.print_str(s)
     
     def text(self, s):
-        logging.info(s)
+        util.curses_util.print_str(s)
     
     def debug(self, s):
-        logging.debug(s)
+        util.curses_util.print_str(s)
     
     def cancelled(self):
-        logging.info('cancelled.')
+        util.curses_util.print_str('Cancelled.')
     
     def success(self):
-        logging.info('OK.')
+        util.curses_util.print_str('OK.')

+ 1 - 1
voice_control/services/music.py

@@ -54,7 +54,7 @@ class Music(base.BaseService):
             f = self._search(tokens)
             if f:
                 output.debug('playing %s' % f)
-                self.current_process = subprocess.Popen(['cvlc', '-Z', f, ]) 
+                self.current_process = subprocess.Popen(['cvlc', '-Z', f, ], stdout=subprocess.PIPE, stderr=subprocess.PIPE) 
 
             else:
                 output.failure()

+ 68 - 0
voice_control/util/curses_util.py

@@ -0,0 +1,68 @@
+import curses
+import io
+
+_MAIN_WINDOW = None
+_INPUT_WINDOW = None
+_Y_POS = 0
+_HEIGHT = None
+_WIDTH = None
+
+class StreamWriter(io.TextIOBase):
+    def write(self, s):
+        print_str(s)
+        return len(s)
+
+def read_str(prompt='>', echo_to_main_window=True):
+    _INPUT_WINDOW.addstr(0, 1, prompt)
+    res = _INPUT_WINDOW.getstr(0, len(prompt) + 2)
+    _INPUT_WINDOW.clear()
+    _INPUT_WINDOW.refresh()
+
+    if echo_to_main_window:
+        print_str('%s %s' % (prompt, res))
+        
+    return res
+
+def print_str(s, *args):
+    global _Y_POS
+
+    while _Y_POS >= _HEIGHT:
+        _MAIN_WINDOW.scroll()
+        _Y_POS -= 1
+
+    _MAIN_WINDOW.addstr(_Y_POS, 0, s)
+    _MAIN_WINDOW.refresh()
+    _Y_POS += 1
+
+def _run(stdscr, main_method):
+    curses.echo()
+    curses.curs_set(0)
+    
+    height, width = stdscr.getmaxyx()
+    top_box = stdscr.subwin(height - 3, width, 0, 0)
+    top_win = top_box.subwin(height - 5, width - 2, 1, 1)
+    bottom_box = stdscr.subwin(height - 3, 0)
+    bottom_win = bottom_box.subwin(1, width - 2, height - 2, 1)
+    top_box.box()
+    bottom_box.box()
+
+    top_win.idlok(True)
+    top_win.scrollok(True)
+
+
+    stdscr.refresh()
+
+    global _MAIN_WINDOW
+    _MAIN_WINDOW = top_win
+
+    global _INPUT_WINDOW
+    _INPUT_WINDOW = bottom_win
+
+    global _HEIGHT
+    global _WIDTH
+    _HEIGHT, _WIDTH = _MAIN_WINDOW.getmaxyx()
+
+    main_method()
+
+def run(main_method):
+    curses.wrapper(_run, main_method=main_method)