/
console.py
180 lines (146 loc) · 5.86 KB
/
console.py
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
from __future__ import unicode_literals
import signal
from six.moves import input as six_input
from contextlib import contextmanager
from radian.settings import radian_settings as settings
from rchitect import console
TERMINAL_CURSOR_AT_BEGINNING = [True]
SUPPRESS_STDOUT = False
SUPPRESS_STDERR = False
@contextmanager
def suppress_stderr(suppress=True):
"""
It is used in completion to avoid running prompt-toolkit nestedly
"""
global SUPPRESS_STDERR
OLD_SUPPRESS_STDERR = SUPPRESS_STDERR
SUPPRESS_STDERR = suppress
try:
yield
finally:
console.flush()
SUPPRESS_STDERR = OLD_SUPPRESS_STDERR
def sigint_handler(signum, frame):
raise KeyboardInterrupt()
def ask_input(s):
orig_handler = signal.getsignal(signal.SIGINT)
# allow Ctrl+C to throw KeyboardInterrupt in callback
signal.signal(signal.SIGINT, sigint_handler)
try:
return six_input(s)
finally:
signal.signal(signal.SIGINT, orig_handler)
def create_read_console(session):
interrupted = [False]
def read_console(message, add_history=1):
app = session.app
if app.is_running:
# fallback to `input` if `read_console` is called nestedly
# c.f. run_coroutine_in_terminal of prompt_toolkit
with suppress_stderr(False):
console.flush()
app.output.flush()
app._running_in_terminal = True
try:
with app.input.detach():
with app.input.cooked_mode():
return ask_input(message)
except KeyboardInterrupt:
app.output.write_raw("\n")
raise
finally:
app._running_in_terminal = False
app.renderer.reset()
app._request_absolute_cursor_position()
app._redraw()
session.prompt_text = message
activated = False
for name in reversed(session.modes):
mode = session.modes[name]
if mode.activator and mode.activator(session):
session.activate_mode(name)
activated = True
break
if not activated:
session.activate_mode("unknown")
current_mode = session.current_mode
if interrupted[0]:
interrupted[0] = False
elif not TERMINAL_CURSOR_AT_BEGINNING[0] or \
(settings.insert_new_line and current_mode.insert_new_line):
app.output.write_raw("\n")
text = None
while text is None:
try:
text = session.prompt(add_history=add_history)
except KeyboardInterrupt:
interrupted[0] = True
raise
except Exception as e:
if isinstance(e, EOFError):
# todo: confirmation in "r" mode
return None
else:
print("unexpected error was caught.")
print("please report to https://github.com/randy3k/radian for such error.")
print(e)
import traceback
traceback.print_exc()
import os
os._exit(1)
current_mode = session.current_mode
if current_mode.on_post_accept:
result = current_mode.on_post_accept(session)
if result is not None:
return result
if settings.insert_new_line and current_mode.insert_new_line:
app.output.write_raw("\n")
text = None
return text
return read_console
def create_write_console_ex(session, stderr_format):
app = session.app
output = app.output
from prompt_toolkit.utils import is_windows
write_console_ex = None
if is_windows():
from prompt_toolkit.output.win32 import Win32Output
if isinstance(output, Win32Output):
# we use print_formatted_text to support ANSI sequences in older Windows
from prompt_toolkit.formatted_text import ANSI
from prompt_toolkit.shortcuts import print_formatted_text
def write_console_ex(buf, otype):
if otype == 0:
if not SUPPRESS_STDOUT:
buf = buf.replace("\r\n", "\n")
sbuf = buf.split("\r")
for i, b in enumerate(sbuf):
print_formatted_text(ANSI(b), end="", output=output)
if i < len(sbuf) - 1:
output.write("\r")
output.flush()
TERMINAL_CURSOR_AT_BEGINNING[0] = buf.endswith("\n")
else:
if not SUPPRESS_STDERR:
buf = buf.replace("\r\n", "\n")
sbuf = buf.split("\r")
for i, b in enumerate(sbuf):
print_formatted_text(
ANSI(stderr_format.format(b)), end="", output=output)
if i < len(sbuf) - 1:
output.write("\r")
output.flush()
TERMINAL_CURSOR_AT_BEGINNING[0] = buf.endswith("\n")
if not write_console_ex:
def write_console_ex(buf, otype):
if otype == 0:
if not SUPPRESS_STDOUT:
output.write_raw(buf)
output.flush()
TERMINAL_CURSOR_AT_BEGINNING[0] = buf.endswith("\n")
else:
if not SUPPRESS_STDERR:
output.write_raw(stderr_format.format(buf))
output.flush()
TERMINAL_CURSOR_AT_BEGINNING[0] = buf.endswith("\n")
return write_console_ex