Skip to content

Commit

Permalink
Prevent use of color in command output to file (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
ianthomas23 authored Sep 23, 2024
1 parent f7d7fbf commit d1feb64
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/commands/wasm_command_runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export abstract class WasmCommandRunner implements ICommandRunner {

if (Object.prototype.hasOwnProperty.call(module, 'ENV')) {
// Copy environment variables into command.
context.environment.copyIntoCommand(module.ENV);
context.environment.copyIntoCommand(module.ENV, stdout.supportsAnsiEscapes());
}

if (Object.prototype.hasOwnProperty.call(module, 'TTY')) {
Expand Down
6 changes: 4 additions & 2 deletions src/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ export class Environment extends Map<string, string> {
/**
* Copy environment variables into a command before it is run.
*/
copyIntoCommand(target: { [key: string]: string }) {
copyIntoCommand(target: { [key: string]: string }, supportsAnsiEscapes: boolean) {
for (const [key, value] of this.entries()) {
target[key] = value;
if (supportsAnsiEscapes || key !== 'TERM') {
target[key] = value;
}
}
}

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 @@ -40,6 +40,20 @@ test.describe('Shell', () => {
expect(output[5]).toMatch('\r\n 2 2 14 out\r\n');
});

test('should output redirect to file without ansi escapes', async ({ page }) => {
// grep to terminal is colored
const output_direct = await shellLineSimple(page, 'grep of file1', { color: true });
const start = '\x1B[01;31m\x1B[K';
const end = '\x1B[m\x1B[K';
expect(output_direct).toMatch(`\r\nContents ${start}of${end} the file\r\n`);

// grep to file is not colored
const output_file = await shellLineSimpleN(page, ['grep of file1 > output', 'cat output'], {
color: false
});
expect(output_file[1]).toMatch(/^cat output\r\nContents of the file\r\n/);
});

test('should input redirect from file', async ({ page }) => {
expect(await shellLineSimple(page, 'wc < file2')).toMatch(' 1 5 27\r\n');
});
Expand Down

0 comments on commit d1feb64

Please sign in to comment.