Skip to content

Commit 5ab36ae

Browse files
pi-anlclaude
andcommitted
aiorepl: Fix Enter key handling in raw terminal mode.
Handle both CR (0x0D) and LF (0x0A) for command execution to ensure compatibility with raw terminal mode where Enter sends CR instead of LF. This fixes the issue where aiorepl required Ctrl+Enter instead of just Enter to execute commands when used with MicroPython ports that put stdin in raw mode (such as the updated unix port using pyexec). Also improves handling of various newline sequences (CRLF, double-LF, double-CR) to prevent double-execution of commands. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]> Signed-off-by: Andrew Leech <[email protected]>
1 parent 5b496e9 commit 5ab36ae

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

micropython/aiorepl/aiorepl.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -119,20 +119,26 @@ async def task(g=None, prompt="--> "):
119119
pt = t # save previous time
120120
t = time.ticks_ms()
121121
if c < 0x20 or c > 0x7E:
122-
if c == 0x0A:
123-
# LF
122+
if c == 0x0A or c == 0x0D:
123+
# LF or CR (handle both for raw terminal mode compatibility)
124124
if paste:
125+
# In paste mode, preserve the actual character
125126
sys.stdout.write(b)
126127
cmd += b
127128
continue
128-
# If the previous character was also LF, and was less
129-
# than 20 ms ago, this was likely due to CRLF->LFLF
130-
# conversion, so ignore this linefeed.
131-
if pc == 0x0A and time.ticks_diff(t, pt) < 20:
129+
# Handle various newline sequences to avoid double-execution:
130+
# - CR+LF (Windows style): ignore LF if it follows CR quickly
131+
# - LF+LF (PTY double-newline): ignore second LF if it follows quickly
132+
# - CR+CR (potential double-CR): ignore second CR if it follows quickly
133+
if (
134+
(c == 0x0A and pc == 0x0D) # LF after CR (CRLF)
135+
or (c == 0x0A and pc == 0x0A) # LF after LF (double LF)
136+
or (c == 0x0D and pc == 0x0D)
137+
) and time.ticks_diff(t, pt) < 20: # CR after CR
132138
continue
133139
if curs:
134140
# move cursor to end of the line
135-
sys.stdout.write("\x1B[{}C".format(curs))
141+
sys.stdout.write("\x1b[{}C".format(curs))
136142
curs = 0
137143
sys.stdout.write("\n")
138144
if cmd:
@@ -153,10 +159,10 @@ async def task(g=None, prompt="--> "):
153159
if curs:
154160
cmd = "".join((cmd[: -curs - 1], cmd[-curs:]))
155161
sys.stdout.write(
156-
"\x08\x1B[K"
162+
"\x08\x1b[K"
157163
) # move cursor back, erase to end of line
158164
sys.stdout.write(cmd[-curs:]) # redraw line
159-
sys.stdout.write("\x1B[{}D".format(curs)) # reset cursor location
165+
sys.stdout.write("\x1b[{}D".format(curs)) # reset cursor location
160166
else:
161167
cmd = cmd[:-1]
162168
sys.stdout.write("\x08 \x08")
@@ -207,21 +213,21 @@ async def task(g=None, prompt="--> "):
207213
elif key == "[D": # left
208214
if curs < len(cmd) - 1:
209215
curs += 1
210-
sys.stdout.write("\x1B")
216+
sys.stdout.write("\x1b")
211217
sys.stdout.write(key)
212218
elif key == "[C": # right
213219
if curs:
214220
curs -= 1
215-
sys.stdout.write("\x1B")
221+
sys.stdout.write("\x1b")
216222
sys.stdout.write(key)
217223
elif key == "[H": # home
218224
pcurs = curs
219225
curs = len(cmd)
220-
sys.stdout.write("\x1B[{}D".format(curs - pcurs)) # move cursor left
226+
sys.stdout.write("\x1b[{}D".format(curs - pcurs)) # move cursor left
221227
elif key == "[F": # end
222228
pcurs = curs
223229
curs = 0
224-
sys.stdout.write("\x1B[{}C".format(pcurs)) # move cursor right
230+
sys.stdout.write("\x1b[{}C".format(pcurs)) # move cursor right
225231
else:
226232
# sys.stdout.write("\\x")
227233
# sys.stdout.write(hex(c))
@@ -231,7 +237,7 @@ async def task(g=None, prompt="--> "):
231237
# inserting into middle of line
232238
cmd = "".join((cmd[:-curs], b, cmd[-curs:]))
233239
sys.stdout.write(cmd[-curs - 1 :]) # redraw line to end
234-
sys.stdout.write("\x1B[{}D".format(curs)) # reset cursor location
240+
sys.stdout.write("\x1b[{}D".format(curs)) # reset cursor location
235241
else:
236242
sys.stdout.write(b)
237243
cmd += b

0 commit comments

Comments
 (0)