Skip to content

Add line continuation for the WASM demo terminal #1022

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 9, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 60 additions & 24 deletions wasm/demo/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,6 @@ snippets.addEventListener('change', updateSnippet);
// option selected for the `select`, but the textarea won't be updated)
updateSnippet();

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();
Expand All @@ -99,38 +93,80 @@ function printToConsole(data) {
term.write(removeNonAscii(data) + '\r\n');
}

const term = new Terminal();
term.open(document.getElementById('terminal'));

const terminalVM = rp.vmStore.init('term_vm');
terminalVM.setStdout(printToConsole);

var input = '';
function getPrompt(name = 'ps1') {
terminalVM.exec(`
try:
import sys as __sys
__prompt = __sys.${name}
except:
__prompt = ''
finally:
del __sys
`);
return String(terminalVM.eval('__prompt'));
}

term.write(getPrompt());

function resetInput() {
continuedInput = [];
input = '';
continuing = false;
}

let continuedInput, input, continuing;
resetInput();

let ps2;

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.execSingle(input);
} catch (err) {
if (err instanceof WebAssembly.RuntimeError) {
err = window.__RUSTPYTHON_ERROR || err;
}
printToConsole(err);
term.write('\r\n');
continuedInput.push(input);
if (continuing) {
if (input === '') {
continuing = false;
} else {
input = '';
term.write(ps2);
return;
}
}
try {
terminalVM.execSingle(continuedInput.join('\n'));
} catch (err) {
if (err instanceof SyntaxError && err.message.includes('EOF')) {
ps2 = getPrompt('ps2');
term.write(ps2);
continuing = true;
input = '';
return;
} else if (err instanceof WebAssembly.RuntimeError) {
err = window.__RUSTPYTHON_ERROR || err;
}
term.write(prompt);
input = '';
printToConsole(err);
}
} else if (code == 127) {
resetInput();
term.write(getPrompt());
} else if (code == 127 || code == 8) {
// Backspace
if (input.length > 0) {
term.write('\b \b');
input = input.slice(0, -1);
}
} else if (code < 32 || code == 127) {
} else if (code < 32) {
// Control
return;
term.write('\r\n' + getPrompt());
input = '';
continuedInput = [];
} else {
// Visible
term.write(data);
Expand Down