diff --git a/docs/project/changelog.md b/docs/project/changelog.md index 6bc838ebd..8258c6c74 100644 --- a/docs/project/changelog.md +++ b/docs/project/changelog.md @@ -11,6 +11,15 @@ substitutions: # Change Log +## Unreleased + +### Console + +- {{Fix}} Ctrl+C handling in console now works correctly with multiline input. + New behavior more closely approximates the behavior of the native Python + console. + {pr}`1790` + ## Version 0.18.0 _August 3rd, 2021_ diff --git a/src/templates/console.html b/src/templates/console.html index c9164531e..ecbef5a44 100644 --- a/src/templates/console.html +++ b/src/templates/console.html @@ -12,6 +12,7 @@ @@ -30,20 +31,23 @@ pyodide.runPython( ` import sys - import js from pyodide import to_js from pyodide.console import PyodideConsole, repr_shorten, BANNER import __main__ BANNER = "Welcome to the Pyodide terminal emulator 🐍\\n" + BANNER - js.pyconsole = PyodideConsole(__main__.__dict__) + pyconsole = PyodideConsole(__main__.__dict__) async def await_fut(fut): return to_js([await fut], depth=1) + def clear_console(): + pyconsole.buffer = [] `, namespace ); let repr_shorten = namespace.get("repr_shorten"); let banner = namespace.get("BANNER"); let await_fut = namespace.get("await_fut"); + let pyconsole = namespace.get("pyconsole"); + let clear_console = namespace.get("clear_console"); namespace.destroy(); let ps1 = ">>> ", @@ -116,6 +120,15 @@ completion: function (command, callback) { callback(pyconsole.complete(command).toJs()[0]); }, + keymap: { + "CTRL+C": async function (event, original) { + clear_console(); + term.echo_command(); + term.echo("KeyboardInterrupt"); + term.set_command(""); + term.set_prompt(ps1); + }, + }, }); window.term = term; pyconsole.stdout_callback = (s) => term.echo(s, { newline: false });