From bb9506c55eb11de70032e7b777c36c5419b475c0 Mon Sep 17 00:00:00 2001 From: Andrew Leech Date: Mon, 2 Jun 2025 13:19:35 +1000 Subject: [PATCH] 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 Signed-off-by: Andrew Leech --- micropython/aiorepl/aiorepl.py | 18 ++++++++++++------ micropython/aiorepl/manifest.py | 2 +- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/micropython/aiorepl/aiorepl.py b/micropython/aiorepl/aiorepl.py index 15026e435..8d5d41342 100644 --- a/micropython/aiorepl/aiorepl.py +++ b/micropython/aiorepl/aiorepl.py @@ -121,16 +121,22 @@ async def task(g=None, prompt="--> "): pt = t # save previous time t = time.ticks_ms() if c < 0x20 or c > 0x7E: - if c == 0x0A: - # LF + if c == 0x0A or c == 0x0D: + # LF or CR (handle both for raw terminal mode compatibility) if paste: + # In paste mode, preserve the actual character sys.stdout.write(b) cmd += b continue - # If the previous character was also LF, and was less - # than 20 ms ago, this was likely due to CRLF->LFLF - # conversion, so ignore this linefeed. - if pc == 0x0A and time.ticks_diff(t, pt) < 20: + # Handle various newline sequences to avoid double-execution: + # - CR+LF (Windows style): ignore LF if it follows CR quickly + # - LF+LF (PTY double-newline): ignore second LF if it follows quickly + # - CR+CR (potential double-CR): ignore second CR if it follows quickly + if ( + (c == 0x0A and pc == 0x0D) # LF after CR (CRLF) + or (c == 0x0A and pc == 0x0A) # LF after LF (double LF) + or (c == 0x0D and pc == 0x0D) + ) and time.ticks_diff(t, pt) < 20: # CR after CR continue if curs: # move cursor to end of the line diff --git a/micropython/aiorepl/manifest.py b/micropython/aiorepl/manifest.py index 83802e1c0..32a20dbed 100644 --- a/micropython/aiorepl/manifest.py +++ b/micropython/aiorepl/manifest.py @@ -1,5 +1,5 @@ metadata( - version="0.2.2", + version="0.2.3", description="Provides an asynchronous REPL that can run concurrently with an asyncio, also allowing await expressions.", )