Skip to content

Commit d41851c

Browse files
pi-anldpgeorge
authored andcommitted
aiorepl: Add support for paste mode (ctrl-e).
Signed-off-by: Andrew Leech <[email protected]>
1 parent e051a12 commit d41851c

File tree

1 file changed

+30
-9
lines changed

1 file changed

+30
-9
lines changed

micropython/aiorepl/aiorepl.py

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@
1919
_HISTORY_LIMIT = const(5 + 1)
2020

2121

22+
CHAR_CTRL_A = const(1)
23+
CHAR_CTRL_B = const(2)
24+
CHAR_CTRL_C = const(3)
25+
CHAR_CTRL_D = const(4)
26+
CHAR_CTRL_E = const(5)
27+
28+
2229
async def execute(code, g, s):
2330
if not code.strip():
2431
return
@@ -43,7 +50,7 @@ async def __code():
4350

4451
async def kbd_intr_task(exec_task, s):
4552
while True:
46-
if ord(await s.read(1)) == 0x03:
53+
if ord(await s.read(1)) == CHAR_CTRL_C:
4754
exec_task.cancel()
4855
return
4956

@@ -102,7 +109,8 @@ async def task(g=None, prompt="--> "):
102109
while True:
103110
hist_b = 0 # How far back in the history are we currently.
104111
sys.stdout.write(prompt)
105-
cmd = ""
112+
cmd: str = ""
113+
paste = False
106114
while True:
107115
b = await s.read(1)
108116
pc = c # save previous character
@@ -112,6 +120,10 @@ async def task(g=None, prompt="--> "):
112120
if c < 0x20 or c > 0x7E:
113121
if c == 0x0A:
114122
# LF
123+
if paste:
124+
sys.stdout.write(b)
125+
cmd += b
126+
continue
115127
# If the previous character was also LF, and was less
116128
# than 20 ms ago, this was likely due to CRLF->LFLF
117129
# conversion, so ignore this linefeed.
@@ -135,25 +147,34 @@ async def task(g=None, prompt="--> "):
135147
if cmd:
136148
cmd = cmd[:-1]
137149
sys.stdout.write("\x08 \x08")
138-
elif c == 0x02:
139-
# Ctrl-B
150+
elif c == CHAR_CTRL_B:
140151
continue
141-
elif c == 0x03:
142-
# Ctrl-C
143-
if pc == 0x03 and time.ticks_diff(t, pt) < 20:
152+
elif c == CHAR_CTRL_C:
153+
if paste:
154+
break
155+
if pc == CHAR_CTRL_C and time.ticks_diff(t, pt) < 20:
144156
# Two very quick Ctrl-C (faster than a human
145157
# typing) likely means mpremote trying to
146158
# escape.
147159
asyncio.new_event_loop()
148160
return
149161
sys.stdout.write("\n")
150162
break
151-
elif c == 0x04:
152-
# Ctrl-D
163+
elif c == CHAR_CTRL_D:
164+
if paste:
165+
result = await execute(cmd, g, s)
166+
if result is not None:
167+
sys.stdout.write(repr(result))
168+
sys.stdout.write("\n")
169+
break
170+
153171
sys.stdout.write("\n")
154172
# Shutdown asyncio.
155173
asyncio.new_event_loop()
156174
return
175+
elif c == CHAR_CTRL_E:
176+
sys.stdout.write("paste mode; Ctrl-C to cancel, Ctrl-D to finish\n===\n")
177+
paste = True
157178
elif c == 0x1B:
158179
# Start of escape sequence.
159180
key = await s.read(2)

0 commit comments

Comments
 (0)