Skip to content

Commit 77cc952

Browse files
committed
WASM REPL: handle a few more special keys (tab, ctrl+c)
1 parent bd52abe commit 77cc952

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

Tools/wasm/emscripten/web_example/python.html

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,20 @@
235235
this.input + this.writeLine("\n"),
236236
);
237237
this.input = "";
238+
this.cursorPosition = 0;
238239
this.activeInput = false;
239240
break;
241+
case "\x03": // CTRL+C
242+
this.input = "";
243+
this.xterm.write("\n")
244+
// this is a real hack. the hard-coded 4... :\
245+
this.xterm.write('\x1b[' + (this.cursorPosition + 4) + 'D');
246+
this.cursorPosition = 0;
247+
this.resolveInput("" + '\n');
248+
break;
249+
case "\x09": // TAB
250+
this.handleTab();
251+
break;
240252
case "\x7F": // BACKSPACE
241253
case "\x08": // CTRL+H
242254
this.handleCursorErase(true);
@@ -245,6 +257,7 @@
245257
// Send empty input
246258
if (this.input === "") {
247259
this.resolveInput("");
260+
this.cursorPosition = 0;
248261
this.activeInput = false;
249262
}
250263
}
@@ -376,6 +389,26 @@
376389
}
377390
}
378391

392+
handleTab() {
393+
// handle tabs: from the current position, add spaces until
394+
// this.cursorPosition is a multiple of 4.
395+
const prefix = this.input.slice(0, this.cursorPosition);
396+
const suffix = this.input.slice(this.cursorPosition);
397+
const count = 4 - (this.cursorPosition % 4);
398+
const toAdd = " ".repeat(count);
399+
this.input = prefix + toAdd + suffix;
400+
if (this.cursorPosition > 0){
401+
this.xterm.write('\x1b[' + (this.cursorPosition) + 'D');
402+
}
403+
// clear the line
404+
this.xterm.write('\x1b[K')
405+
this.xterm.write(this.input);
406+
if (suffix){
407+
this.xterm.write('\x1b[' + suffix.length + 'D');
408+
}
409+
this.cursorPosition += count;
410+
}
411+
379412
prompt = async () => {
380413
this.activeInput = true;
381414
// Hack to allow stdout/stderr to finish before we figure out where input starts

0 commit comments

Comments
 (0)