@@ -106,10 +106,10 @@ async def task(g=None, prompt="--> "):
106106 hist_n = 0 # Number of history entries.
107107 c = 0 # ord of most recent character.
108108 t = 0 # timestamp of most recent character.
109- sw = sys .stdout .write
109+ stdout_write = sys .stdout .write
110110 while True :
111111 hist_b = 0 # How far back in the history are we currently.
112- sw (prompt )
112+ stdout_write (prompt )
113113 cmd : str = ""
114114 paste = False
115115 curs = 0 # cursor offset from end of cmd buffer
@@ -126,7 +126,7 @@ async def task(g=None, prompt="--> "):
126126 # LF or CR (handle both for raw terminal mode compatibility)
127127 if paste :
128128 # In paste mode, preserve the actual character
129- sw (b )
129+ stdout_write (b )
130130 cmd += b
131131 continue
132132 # Handle various newline sequences to avoid double-execution:
@@ -141,9 +141,9 @@ async def task(g=None, prompt="--> "):
141141 continue
142142 if curs :
143143 # move cursor to end of the line
144- sw ("\x1b [{}C" .format (curs ))
144+ stdout_write ("\x1b [{}C" .format (curs ))
145145 curs = 0
146- sw ("\n " )
146+ stdout_write ("\n " )
147147 if cmd :
148148 # Push current command.
149149 hist [hist_i ] = cmd
@@ -153,20 +153,22 @@ async def task(g=None, prompt="--> "):
153153
154154 result = await execute (cmd , g , s )
155155 if result is not None :
156- sw (repr (result ))
157- sw ("\n " )
156+ stdout_write (repr (result ))
157+ stdout_write ("\n " )
158158 break
159159 elif c == 0x08 or c == 0x7F :
160160 # Backspace.
161161 if cmd :
162162 if curs :
163163 cmd = "" .join ((cmd [: - curs - 1 ], cmd [- curs :]))
164- sw ("\x08 \x1b [K" ) # move cursor back, erase to end of line
165- sw (cmd [- curs :]) # redraw line
166- sw ("\x1b [{}D" .format (curs )) # reset cursor location
164+ stdout_write (
165+ "\x08 \x1b [K"
166+ ) # move cursor back, erase to end of line
167+ stdout_write (cmd [- curs :]) # redraw line
168+ stdout_write ("\x1b [{}D" .format (curs )) # reset cursor location
167169 else :
168170 cmd = cmd [:- 1 ]
169- sw ("\x08 \x08 " )
171+ stdout_write ("\x08 \x08 " )
170172 elif c == CHAR_CTRL_A :
171173 raw_repl (sys .stdin , g )
172174 break
@@ -175,22 +177,22 @@ async def task(g=None, prompt="--> "):
175177 elif c == CHAR_CTRL_C :
176178 if paste :
177179 break
178- sw ("\n " )
180+ stdout_write ("\n " )
179181 break
180182 elif c == CHAR_CTRL_D :
181183 if paste :
182184 result = await execute (cmd , g , s )
183185 if result is not None :
184- sw (repr (result ))
185- sw ("\n " )
186+ stdout_write (repr (result ))
187+ stdout_write ("\n " )
186188 break
187189
188- sw ("\n " )
190+ stdout_write ("\n " )
189191 # Shutdown asyncio.
190192 asyncio .new_event_loop ()
191193 return
192194 elif c == CHAR_CTRL_E :
193- sw ("paste mode; Ctrl-C to cancel, Ctrl-D to finish\n ===\n " )
195+ stdout_write ("paste mode; Ctrl-C to cancel, Ctrl-D to finish\n ===\n " )
194196 paste = True
195197 elif c == 0x1B :
196198 # Start of escape sequence.
@@ -200,35 +202,35 @@ async def task(g=None, prompt="--> "):
200202 hist [(hist_i - hist_b ) % _HISTORY_LIMIT ] = cmd
201203 # Clear current command.
202204 b = "\x08 " * len (cmd )
203- sw (b )
204- sw (" " * len (cmd ))
205- sw (b )
205+ stdout_write (b )
206+ stdout_write (" " * len (cmd ))
207+ stdout_write (b )
206208 # Go backwards or forwards in the history.
207209 if key == "[A" :
208210 hist_b = min (hist_n , hist_b + 1 )
209211 else :
210212 hist_b = max (0 , hist_b - 1 )
211213 # Update current command.
212214 cmd = hist [(hist_i - hist_b ) % _HISTORY_LIMIT ]
213- sw (cmd )
215+ stdout_write (cmd )
214216 elif key == "[D" : # left
215217 if curs < len (cmd ) - 1 :
216218 curs += 1
217- sw ("\x1b " )
218- sw (key )
219+ stdout_write ("\x1b " )
220+ stdout_write (key )
219221 elif key == "[C" : # right
220222 if curs :
221223 curs -= 1
222- sw ("\x1b " )
223- sw (key )
224+ stdout_write ("\x1b " )
225+ stdout_write (key )
224226 elif key == "[H" : # home
225227 pcurs = curs
226228 curs = len (cmd )
227- sw ("\x1b [{}D" .format (curs - pcurs )) # move cursor left
229+ stdout_write ("\x1b [{}D" .format (curs - pcurs )) # move cursor left
228230 elif key == "[F" : # end
229231 pcurs = curs
230232 curs = 0
231- sw ("\x1b [{}C" .format (pcurs )) # move cursor right
233+ stdout_write ("\x1b [{}C" .format (pcurs )) # move cursor right
232234 else :
233235 # sw("\\x")
234236 # sw(hex(c))
@@ -237,19 +239,19 @@ async def task(g=None, prompt="--> "):
237239 if curs :
238240 # inserting into middle of line
239241 cmd = "" .join ((cmd [:- curs ], b , cmd [- curs :]))
240- sw (cmd [- curs - 1 :]) # redraw line to end
241- sw ("\x1b [{}D" .format (curs )) # reset cursor location
242+ stdout_write (cmd [- curs - 1 :]) # redraw line to end
243+ stdout_write ("\x1b [{}D" .format (curs )) # reset cursor location
242244 else :
243- sw (b )
245+ stdout_write (b )
244246 cmd += b
245247 finally :
246248 micropython .kbd_intr (3 )
247249
248250
249251def raw_paste (s , window = 512 ):
250- sw = sys .stdout .write
251- sw ("R\x01 " ) # supported
252- sw (bytearray ([window & 0xFF , window >> 8 , 0x01 ]).decode ())
252+ stdout_write = sys .stdout .write
253+ stdout_write ("R\x01 " ) # supported
254+ stdout_write (bytearray ([window & 0xFF , window >> 8 , 0x01 ]).decode ())
253255 eof = False
254256 idx = 0
255257 buff = bytearray (window )
@@ -260,7 +262,7 @@ def raw_paste(s, window=512):
260262 c = ord (b )
261263 if c == CHAR_CTRL_C or c == CHAR_CTRL_D :
262264 # end of file
263- sw (chr (CHAR_CTRL_D ))
265+ stdout_write (chr (CHAR_CTRL_D ))
264266 if c == CHAR_CTRL_C :
265267 raise KeyboardInterrupt
266268 file += buff [:idx ]
@@ -270,7 +272,7 @@ def raw_paste(s, window=512):
270272
271273 if not eof :
272274 file += buff
273- sw ("\x01 " ) # indicate window available to host
275+ stdout_write ("\x01 " ) # indicate window available to host
274276
275277 return file
276278
@@ -283,12 +285,12 @@ def raw_repl(s, g: dict):
283285 """
284286 heading = "raw REPL; CTRL-B to exit\n "
285287 line = ""
286- sw = sys .stdout .write
287- sw (heading )
288+ stdout_write = sys .stdout .write
289+ stdout_write (heading )
288290
289291 while True :
290292 line = ""
291- sw (">" )
293+ stdout_write (">" )
292294 while True :
293295 b = s .read (1 )
294296 c = ord (b )
@@ -302,20 +304,20 @@ def raw_repl(s, g: dict):
302304 break
303305 else :
304306 # reset raw REPL
305- sw (heading )
306- sw (">" )
307+ stdout_write (heading )
308+ stdout_write (">" )
307309 continue
308310 elif c == CHAR_CTRL_B :
309311 # exit raw REPL
310- sw ("\n " )
312+ stdout_write ("\n " )
311313 return 0
312314 elif c == CHAR_CTRL_C :
313315 # clear line
314316 line = ""
315317 elif c == CHAR_CTRL_D :
316318 # entry finished
317319 # indicate reception of command
318- sw ("OK" )
320+ stdout_write ("OK" )
319321 break
320322 else :
321323 # let through any other raw 8-bit value
@@ -324,16 +326,16 @@ def raw_repl(s, g: dict):
324326 if len (line ) == 0 :
325327 # Normally used to trigger soft-reset but stay in raw mode.
326328 # Fake it for aiorepl / mpremote.
327- sw ("Ignored: soft reboot\n " )
328- sw (heading )
329+ stdout_write ("Ignored: soft reboot\n " )
330+ stdout_write (heading )
329331
330332 try :
331333 result = exec (line , g )
332334 if result is not None :
333- sw (repr (result ))
334- sw (chr (CHAR_CTRL_D ))
335+ stdout_write (repr (result ))
336+ stdout_write (chr (CHAR_CTRL_D ))
335337 except Exception as ex :
336338 print (line )
337- sw (chr (CHAR_CTRL_D ))
339+ stdout_write (chr (CHAR_CTRL_D ))
338340 sys .print_exception (ex , sys .stdout )
339- sw (chr (CHAR_CTRL_D ))
341+ stdout_write (chr (CHAR_CTRL_D ))
0 commit comments