Skip to content

Commit 84c2445

Browse files
committed
aiorepl: Switch terminal mode around code execution.
Use micropython.stdio_mode_raw() when available to set the terminal to raw mode for character-at-a-time input and restore cooked mode during code execution. This matches how the native REPL toggles terminal mode and fixes aiorepl on the unix port where the terminal defaults to cooked mode after the native REPL yields control. On embedded ports where stdio_mode_raw is not available, the getattr fallback to a no-op lambda keeps the existing behaviour unchanged. Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
1 parent a84abd2 commit 84c2445

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

micropython/aiorepl/aiorepl.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ async def task(g=None, prompt="--> "):
9797
print("Starting asyncio REPL...")
9898
if g is None:
9999
g = __import__("__main__").__dict__
100+
_stdio_raw = getattr(micropython, "stdio_mode_raw", lambda _: None)
100101
try:
101102
micropython.kbd_intr(-1)
102103
s = asyncio.StreamReader(sys.stdin)
@@ -107,6 +108,7 @@ async def task(g=None, prompt="--> "):
107108
c = 0 # ord of most recent character.
108109
t = 0 # timestamp of most recent character.
109110
_autocomplete = getattr(micropython, "repl_autocomplete", None)
111+
_stdio_raw(True)
110112
while True:
111113
hist_b = 0 # How far back in the history are we currently.
112114
sys.stdout.write(prompt)
@@ -145,10 +147,12 @@ async def task(g=None, prompt="--> "):
145147
hist_n = min(_HISTORY_LIMIT - 1, hist_n + 1)
146148
hist_i = (hist_i + 1) % _HISTORY_LIMIT
147149

150+
_stdio_raw(False)
148151
result = await execute(cmd, g, s)
149152
if result is not None:
150153
sys.stdout.write(repr(result))
151154
sys.stdout.write("\n")
155+
_stdio_raw(True)
152156
break
153157
elif c == 0x08 or c == 0x7F:
154158
# Backspace.
@@ -164,7 +168,9 @@ async def task(g=None, prompt="--> "):
164168
cmd = cmd[:-1]
165169
sys.stdout.write("\x08 \x08")
166170
elif c == CHAR_CTRL_A:
171+
_stdio_raw(False)
167172
raw_repl(sys.stdin, g)
173+
_stdio_raw(True)
168174
break
169175
elif c == CHAR_CTRL_B:
170176
continue
@@ -175,10 +181,12 @@ async def task(g=None, prompt="--> "):
175181
break
176182
elif c == CHAR_CTRL_D:
177183
if paste:
184+
_stdio_raw(False)
178185
result = await execute(cmd, g, s)
179186
if result is not None:
180187
sys.stdout.write(repr(result))
181188
sys.stdout.write("\n")
189+
_stdio_raw(True)
182190
break
183191

184192
sys.stdout.write("\n")
@@ -264,6 +272,7 @@ async def task(g=None, prompt="--> "):
264272
sys.stdout.write(b)
265273
cmd += b
266274
finally:
275+
_stdio_raw(False)
267276
micropython.kbd_intr(3)
268277

269278

0 commit comments

Comments
 (0)