Fix terminal Ctrl+C handling (#1790)

This commit is contained in:
Hood Chatham 2021-08-13 20:13:47 +02:00 committed by GitHub
parent bb2f594372
commit cacfdde929
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 2 deletions

View File

@ -11,6 +11,15 @@ substitutions:
# Change Log # 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 ## Version 0.18.0
_August 3rd, 2021_ _August 3rd, 2021_

View File

@ -12,6 +12,7 @@
<style> <style>
.terminal { .terminal {
--size: 1.5; --size: 1.5;
--color: rgba(255, 255, 255, 0.8);
} }
</style> </style>
</head> </head>
@ -30,20 +31,23 @@
pyodide.runPython( pyodide.runPython(
` `
import sys import sys
import js
from pyodide import to_js from pyodide import to_js
from pyodide.console import PyodideConsole, repr_shorten, BANNER from pyodide.console import PyodideConsole, repr_shorten, BANNER
import __main__ import __main__
BANNER = "Welcome to the Pyodide terminal emulator 🐍\\n" + BANNER BANNER = "Welcome to the Pyodide terminal emulator 🐍\\n" + BANNER
js.pyconsole = PyodideConsole(__main__.__dict__) pyconsole = PyodideConsole(__main__.__dict__)
async def await_fut(fut): async def await_fut(fut):
return to_js([await fut], depth=1) return to_js([await fut], depth=1)
def clear_console():
pyconsole.buffer = []
`, `,
namespace namespace
); );
let repr_shorten = namespace.get("repr_shorten"); let repr_shorten = namespace.get("repr_shorten");
let banner = namespace.get("BANNER"); let banner = namespace.get("BANNER");
let await_fut = namespace.get("await_fut"); let await_fut = namespace.get("await_fut");
let pyconsole = namespace.get("pyconsole");
let clear_console = namespace.get("clear_console");
namespace.destroy(); namespace.destroy();
let ps1 = ">>> ", let ps1 = ">>> ",
@ -116,6 +120,15 @@
completion: function (command, callback) { completion: function (command, callback) {
callback(pyconsole.complete(command).toJs()[0]); 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; window.term = term;
pyconsole.stdout_callback = (s) => term.echo(s, { newline: false }); pyconsole.stdout_callback = (s) => term.echo(s, { newline: false });