From cacfdde929e72038e9214013e56d44967be6fcc2 Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Fri, 13 Aug 2021 20:13:47 +0200 Subject: [PATCH] Fix terminal Ctrl+C handling (#1790) --- docs/project/changelog.md | 9 +++++++++ src/templates/console.html | 17 +++++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) 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 });