@@ -111,6 +111,7 @@ async def task(g=None, prompt="--> "):
111
111
sys .stdout .write (prompt )
112
112
cmd : str = ""
113
113
paste = False
114
+ curs = 0 # cursor offset from end of cmd buffer
114
115
while True :
115
116
b = await s .read (1 )
116
117
pc = c # save previous character
@@ -129,6 +130,10 @@ async def task(g=None, prompt="--> "):
129
130
# conversion, so ignore this linefeed.
130
131
if pc == 0x0A and time .ticks_diff (t , pt ) < 20 :
131
132
continue
133
+ if curs :
134
+ # move cursor to end of the line
135
+ sys .stdout .write ("\x1B [{}C" .format (curs ))
136
+ curs = 0
132
137
sys .stdout .write ("\n " )
133
138
if cmd :
134
139
# Push current command.
@@ -145,8 +150,16 @@ async def task(g=None, prompt="--> "):
145
150
elif c == 0x08 or c == 0x7F :
146
151
# Backspace.
147
152
if cmd :
148
- cmd = cmd [:- 1 ]
149
- sys .stdout .write ("\x08 \x08 " )
153
+ if curs :
154
+ cmd = "" .join ((cmd [: - curs - 1 ], cmd [- curs :]))
155
+ sys .stdout .write (
156
+ "\x08 \x1B [K"
157
+ ) # move cursor back, erase to end of line
158
+ sys .stdout .write (cmd [- curs :]) # redraw line
159
+ sys .stdout .write ("\x1B [{}D" .format (curs )) # reset cursor location
160
+ else :
161
+ cmd = cmd [:- 1 ]
162
+ sys .stdout .write ("\x08 \x08 " )
150
163
elif c == CHAR_CTRL_B :
151
164
continue
152
165
elif c == CHAR_CTRL_C :
@@ -178,7 +191,7 @@ async def task(g=None, prompt="--> "):
178
191
elif c == 0x1B :
179
192
# Start of escape sequence.
180
193
key = await s .read (2 )
181
- if key in ("[A" , "[B" ):
194
+ if key in ("[A" , "[B" ): # up, down
182
195
# Stash the current command.
183
196
hist [(hist_i - hist_b ) % _HISTORY_LIMIT ] = cmd
184
197
# Clear current command.
@@ -194,12 +207,36 @@ async def task(g=None, prompt="--> "):
194
207
# Update current command.
195
208
cmd = hist [(hist_i - hist_b ) % _HISTORY_LIMIT ]
196
209
sys .stdout .write (cmd )
210
+ elif key == "[D" : # left
211
+ if curs < len (cmd ) - 1 :
212
+ curs += 1
213
+ sys .stdout .write ("\x1B " )
214
+ sys .stdout .write (key )
215
+ elif key == "[C" : # right
216
+ if curs :
217
+ curs -= 1
218
+ sys .stdout .write ("\x1B " )
219
+ sys .stdout .write (key )
220
+ elif key == "[H" : # home
221
+ pcurs = curs
222
+ curs = len (cmd )
223
+ sys .stdout .write ("\x1B [{}D" .format (curs - pcurs )) # move cursor left
224
+ elif key == "[F" : # end
225
+ pcurs = curs
226
+ curs = 0
227
+ sys .stdout .write ("\x1B [{}C" .format (pcurs )) # move cursor right
197
228
else :
198
229
# sys.stdout.write("\\x")
199
230
# sys.stdout.write(hex(c))
200
231
pass
201
232
else :
202
- sys .stdout .write (b )
203
- cmd += b
233
+ if curs :
234
+ # inserting into middle of line
235
+ cmd = "" .join ((cmd [:- curs ], b , cmd [- curs :]))
236
+ sys .stdout .write (cmd [- curs - 1 :]) # redraw line to end
237
+ sys .stdout .write ("\x1B [{}D" .format (curs )) # reset cursor location
238
+ else :
239
+ sys .stdout .write (b )
240
+ cmd += b
204
241
finally :
205
242
micropython .kbd_intr (3 )
0 commit comments