-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathinput_handler.py
More file actions
65 lines (51 loc) · 1.86 KB
/
input_handler.py
File metadata and controls
65 lines (51 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""User input handling for tn-cli."""
from __future__ import print_function
import sys
import tn_globals
from tn_globals import printerr
# Prints prompt and reads lines from stdin.
def readLinesFromStdin():
if tn_globals.IsInteractive:
while True:
try:
line = tn_globals.Prompt.prompt()
yield line
except EOFError as e:
# Ctrl+D.
break
else:
# iter(...) is a workaround for a python2 bug https://bugs.python.org/issue3907
for cmd in iter(sys.stdin.readline, ''):
yield cmd
# Stdin reads a possibly multiline input from stdin and queues it for asynchronous processing.
def stdin(InputQueue):
partial_input = ""
try:
for cmd in readLinesFromStdin():
cmd = cmd.strip()
# Check for continuation symbol \ in the end of the line.
if len(cmd) > 0 and cmd[-1] == "\\":
cmd = cmd[:-1].rstrip()
if cmd:
if partial_input:
partial_input += " " + cmd
else:
partial_input = cmd
if tn_globals.IsInteractive:
sys.stdout.write("... ")
sys.stdout.flush()
continue
# Check if we have cached input from a previous multiline command.
if partial_input:
if cmd:
partial_input += " " + cmd
InputQueue.append(partial_input)
partial_input = ""
continue
InputQueue.append(cmd)
# Stop processing input
if cmd == 'exit' or cmd == 'quit' or cmd == '.exit' or cmd == '.quit':
return
except Exception as ex:
printerr("Exception in stdin", ex)
InputQueue.append('exit')