Skip to content

Commit

Permalink
Support multi-character inputs (escape sequences) whilst command running
Browse files Browse the repository at this point in the history
  • Loading branch information
ianthomas23 committed Dec 3, 2024
1 parent 67f0307 commit d4ee105
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/commands/wasm_command_runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,19 @@ export abstract class WasmCommandRunner implements ICommandRunner {
const start = Date.now();
const wasmModule = this.wasmLoader.getModule(this.moduleName());

let _getCharBuffer: number[] = [];

// Functions for monkey-patching.
function getChar(tty: any) {
if (_getCharBuffer.length > 0) {
return _getCharBuffer.shift()!;
}

const utf16codes = stdin.readChar();
const utf16 = utf16codes[0];
if (utf16codes.length > 1) {
_getCharBuffer = utf16codes.slice(1);
}

if (stdin.isTerminal()) {
if (utf16 === 10) {
Expand Down
14 changes: 14 additions & 0 deletions test/tests/shell.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,20 @@ test.describe('Shell', () => {
expect(output).toMatch(/^wc\r\na b\r\nc {6}1 {7}3 {7}5\r\n/);
});

test('should support terminal stdin of an ansi escape sequence', async ({ page }) => {
const output = await page.evaluate(async () => {
const { shell, output } = await globalThis.cockle.shellSetupEmpty();
const EOT = String.fromCharCode(4);
const downArrow = '\x1B[B';
await Promise.all([
shell.inputLine('wc'),
globalThis.cockle.terminalInput(shell, ['a', downArrow, 'b', EOT])
]);
return output.text;
});
expect(output).toMatch(/^wc\r\nab {6}0 {7}1 {7}5\r\n/);
});

test('should support terminal stdin more than once', async ({ page }) => {
const output = await page.evaluate(async () => {
const { shell, output } = await globalThis.cockle.shellSetupEmpty();
Expand Down

0 comments on commit d4ee105

Please sign in to comment.