Skip to content

Commit

Permalink
CLI improvements, as per issue #493
Browse files Browse the repository at this point in the history
* "Input:" doesn't get intermingled with the output (usually -- long pauses can still cause it to happen)
* "Output:" is now displayed
* Ctrl+C is handled gracefully
  • Loading branch information
penrods authored and aatchison committed Feb 3, 2017
1 parent fc71c9f commit c3070be
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions mycroft/client/text/main.py
Expand Up @@ -17,6 +17,7 @@


import sys
import time
from threading import Thread, Lock

from mycroft.messagebus.client.ws import WebsocketClient
Expand All @@ -35,13 +36,21 @@ def handle_speak(event):
ws.emit(Message("recognizer_loop:audio_output_start"))
try:
utterance = event.data.get('utterance')
logger.info("Speak: " + utterance)
print(">> " + utterance)
tts.execute(utterance)
finally:
mutex.release()
ws.emit(Message("recognizer_loop:audio_output_end"))


def handle_quiet(event):
try:
utterance = event.data.get('utterance')
print(">> " + utterance)
finally:
pass


def connect():
ws.run_forever()

Expand All @@ -50,18 +59,27 @@ def main():
global ws
ws = WebsocketClient()
tts.init(ws)
if '--quiet' not in sys.argv:
if '--quiet' in sys.argv:
ws.on('speak', handle_quiet)
else:
ws.on('speak', handle_speak)
event_thread = Thread(target=connect)
event_thread.setDaemon(True)
event_thread.start()
try:
while True:
print("Input:")
# TODO: Change this mechanism
# Sleep for a while so all the output that results
# from the previous command finishes before we print.
time.sleep(1.5)
print("Input (Ctrl+C to quit):")
line = sys.stdin.readline()
ws.emit(
Message("recognizer_loop:utterance",
{'utterances': [line.strip()]}))
except KeyboardInterrupt, e:
# User hit Ctrl+C to quit
print("")
except KeyboardInterrupt, e:
logger.exception(e)
event_thread.exit()
Expand All @@ -70,3 +88,5 @@ def main():

if __name__ == "__main__":
main()


0 comments on commit c3070be

Please sign in to comment.