Skip to content

Commit 29ec84e

Browse files
committed
Change main.js to print to the console
1 parent 8407f74 commit 29ec84e

1 file changed

Lines changed: 37 additions & 36 deletions

File tree

wasm/demo/src/main.js

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function runCodeFromTextarea() {
3636
const code = editor.getValue();
3737
try {
3838
const result = rp.pyEval(code, {
39-
stdout: '#console'
39+
stdout: consoleElement
4040
});
4141
if (result !== null) {
4242
consoleElement.value += `\n${result}\n`;
@@ -74,57 +74,58 @@ snippets.addEventListener('change', updateSnippet);
7474
// option selected for the `select`, but the textarea won't be updated)
7575
updateSnippet();
7676

77-
const prompt = ">>>>> ";
77+
const prompt = '>>>>> ';
7878

7979
const term = new Terminal();
8080
term.open(document.getElementById('terminal'));
8181
term.write(prompt);
8282

8383
function removeNonAscii(str) {
84-
if ((str===null) || (str===''))
85-
return false;
86-
else
87-
str = str.toString();
84+
if (str === null || str === '') return false;
85+
else str = str.toString();
8886

8987
return str.replace(/[^\x20-\x7E]/g, '');
9088
}
9189

9290
function printToConsole(data) {
93-
term.write(removeNonAscii(data) + "\r\n");
91+
term.write(removeNonAscii(data) + '\r\n');
9492
}
9593

96-
const terminalVM = rp.vmStore.init("term_vm");
94+
const terminalVM = rp.vmStore.init('term_vm');
9795
terminalVM.setStdout(printToConsole);
9896

99-
var input = "";
100-
term.on("data", (data) => {
101-
const code = data.charCodeAt(0);
102-
if (code == 13) { // CR
103-
if (input[input.length - 1] == ':') {
104-
input += data
105-
term.write("\r\n.....");
106-
} else {
107-
term.write("\r\n");
108-
try {
109-
terminalVM.exec(input);
110-
} catch (err) {
111-
if (err instanceof WebAssembly.RuntimeError) {
112-
err = window.__RUSTPYTHON_ERROR || err;
97+
var input = '';
98+
term.on('data', data => {
99+
const code = data.charCodeAt(0);
100+
if (code == 13) {
101+
// CR
102+
if (input[input.length - 1] == ':') {
103+
input += data;
104+
term.write('\r\n.....');
105+
} else {
106+
term.write('\r\n');
107+
try {
108+
terminalVM.exec(input);
109+
} catch (err) {
110+
if (err instanceof WebAssembly.RuntimeError) {
111+
err = window.__RUSTPYTHON_ERROR || err;
112+
}
113+
printToConsole(err);
113114
}
114-
printToConsole(err);
115+
term.write(prompt);
116+
input = '';
115117
}
116-
term.write(prompt);
117-
input = "";
118-
}
119-
} else if (code == 127) {
120-
if (input.length > 0) {
121-
term.write("\b \b");
122-
input = input.slice(0, -1);
118+
} else if (code == 127) {
119+
if (input.length > 0) {
120+
term.write('\b \b');
121+
input = input.slice(0, -1);
122+
}
123+
} else if (code < 32 || code == 127) {
124+
// Control
125+
return;
126+
} else {
127+
// Visible
128+
term.write(data);
129+
input += data;
123130
}
124-
} else if (code < 32 || code == 127) { // Control
125-
return;
126-
} else { // Visible
127-
term.write(data);
128-
input += data;
129-
}
130131
});

0 commit comments

Comments
 (0)