Skip to content

Commit 4d56823

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]>
1 parent 5b496e9 commit 4d56823

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

micropython/aiorepl/aiorepl.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,16 +119,20 @@ 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 ((c == 0x0A and pc == 0x0D) or # LF after CR (CRLF)
134+
(c == 0x0A and pc == 0x0A) or # LF after LF (double LF)
135+
(c == 0x0D and pc == 0x0D)) and time.ticks_diff(t, pt) < 20: # CR after CR
132136
continue
133137
if curs:
134138
# move cursor to end of the line

0 commit comments

Comments
 (0)