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

Prevent use of color in command output to file #59

Merged
merged 1 commit into from
Sep 23, 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
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
Loading