19
19
_HISTORY_LIMIT = const (5 + 1 )
20
20
21
21
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
+
22
29
async def execute (code , g , s ):
23
30
if not code .strip ():
24
31
return
@@ -43,7 +50,7 @@ async def __code():
43
50
44
51
async def kbd_intr_task (exec_task , s ):
45
52
while True :
46
- if ord (await s .read (1 )) == 0x03 :
53
+ if ord (await s .read (1 )) == CHAR_CTRL_C :
47
54
exec_task .cancel ()
48
55
return
49
56
@@ -102,7 +109,8 @@ async def task(g=None, prompt="--> "):
102
109
while True :
103
110
hist_b = 0 # How far back in the history are we currently.
104
111
sys .stdout .write (prompt )
105
- cmd = ""
112
+ cmd : str = ""
113
+ paste = False
106
114
while True :
107
115
b = await s .read (1 )
108
116
pc = c # save previous character
@@ -112,6 +120,10 @@ async def task(g=None, prompt="--> "):
112
120
if c < 0x20 or c > 0x7E :
113
121
if c == 0x0A :
114
122
# LF
123
+ if paste :
124
+ sys .stdout .write (b )
125
+ cmd += b
126
+ continue
115
127
# If the previous character was also LF, and was less
116
128
# than 20 ms ago, this was likely due to CRLF->LFLF
117
129
# conversion, so ignore this linefeed.
@@ -135,25 +147,34 @@ async def task(g=None, prompt="--> "):
135
147
if cmd :
136
148
cmd = cmd [:- 1 ]
137
149
sys .stdout .write ("\x08 \x08 " )
138
- elif c == 0x02 :
139
- # Ctrl-B
150
+ elif c == CHAR_CTRL_B :
140
151
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 :
144
156
# Two very quick Ctrl-C (faster than a human
145
157
# typing) likely means mpremote trying to
146
158
# escape.
147
159
asyncio .new_event_loop ()
148
160
return
149
161
sys .stdout .write ("\n " )
150
162
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
+
153
171
sys .stdout .write ("\n " )
154
172
# Shutdown asyncio.
155
173
asyncio .new_event_loop ()
156
174
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
157
178
elif c == 0x1B :
158
179
# Start of escape sequence.
159
180
key = await s .read (2 )
0 commit comments