Skip to content
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

Support multi-character inputs (escape sequences) whilst command running #80

Merged
merged 1 commit into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
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
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
Loading