mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-29 03:04:38 +08:00
Change main.js to print to the console
This commit is contained in:
1 file changed
+37
-36
+37
-36
@@ -36,7 +36,7 @@ function runCodeFromTextarea() {
|
||||
const code = editor.getValue();
|
||||
try {
|
||||
const result = rp.pyEval(code, {
|
||||
stdout: '#console'
|
||||
stdout: consoleElement
|
||||
});
|
||||
if (result !== null) {
|
||||
consoleElement.value += `\n${result}\n`;
|
||||
@@ -74,57 +74,58 @@ snippets.addEventListener('change', updateSnippet);
|
||||
// option selected for the `select`, but the textarea won't be updated)
|
||||
updateSnippet();
|
||||
|
||||
const prompt = ">>>>> ";
|
||||
const prompt = '>>>>> ';
|
||||
|
||||
const term = new Terminal();
|
||||
term.open(document.getElementById('terminal'));
|
||||
term.write(prompt);
|
||||
|
||||
function removeNonAscii(str) {
|
||||
if ((str===null) || (str===''))
|
||||
return false;
|
||||
else
|
||||
str = str.toString();
|
||||
if (str === null || str === '') return false;
|
||||
else str = str.toString();
|
||||
|
||||
return str.replace(/[^\x20-\x7E]/g, '');
|
||||
}
|
||||
|
||||
function printToConsole(data) {
|
||||
term.write(removeNonAscii(data) + "\r\n");
|
||||
term.write(removeNonAscii(data) + '\r\n');
|
||||
}
|
||||
|
||||
const terminalVM = rp.vmStore.init("term_vm");
|
||||
const terminalVM = rp.vmStore.init('term_vm');
|
||||
terminalVM.setStdout(printToConsole);
|
||||
|
||||
var input = "";
|
||||
term.on("data", (data) => {
|
||||
const code = data.charCodeAt(0);
|
||||
if (code == 13) { // CR
|
||||
if (input[input.length - 1] == ':') {
|
||||
input += data
|
||||
term.write("\r\n.....");
|
||||
} else {
|
||||
term.write("\r\n");
|
||||
try {
|
||||
terminalVM.exec(input);
|
||||
} catch (err) {
|
||||
if (err instanceof WebAssembly.RuntimeError) {
|
||||
err = window.__RUSTPYTHON_ERROR || err;
|
||||
var input = '';
|
||||
term.on('data', data => {
|
||||
const code = data.charCodeAt(0);
|
||||
if (code == 13) {
|
||||
// CR
|
||||
if (input[input.length - 1] == ':') {
|
||||
input += data;
|
||||
term.write('\r\n.....');
|
||||
} else {
|
||||
term.write('\r\n');
|
||||
try {
|
||||
terminalVM.exec(input);
|
||||
} catch (err) {
|
||||
if (err instanceof WebAssembly.RuntimeError) {
|
||||
err = window.__RUSTPYTHON_ERROR || err;
|
||||
}
|
||||
printToConsole(err);
|
||||
}
|
||||
printToConsole(err);
|
||||
term.write(prompt);
|
||||
input = '';
|
||||
}
|
||||
term.write(prompt);
|
||||
input = "";
|
||||
} else if (code == 127) {
|
||||
if (input.length > 0) {
|
||||
term.write('\b \b');
|
||||
input = input.slice(0, -1);
|
||||
}
|
||||
} else if (code < 32 || code == 127) {
|
||||
// Control
|
||||
return;
|
||||
} else {
|
||||
// Visible
|
||||
term.write(data);
|
||||
input += data;
|
||||
}
|
||||
} else if (code == 127) {
|
||||
if (input.length > 0) {
|
||||
term.write("\b \b");
|
||||
input = input.slice(0, -1);
|
||||
}
|
||||
} else if (code < 32 || code == 127) { // Control
|
||||
return;
|
||||
} else { // Visible
|
||||
term.write(data);
|
||||
input += data;
|
||||
}
|
||||
});
|
||||
Reference in new issue
Block a user