Skip to content

Commit 128ac43

Browse files
authored
Merge pull request #27 from badsyntax/terminal-colours
Use colours in custom terminal
2 parents df9dded + a90c83c commit 128ac43

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

src/cli/CLI.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ import type { ChildProcessWithoutNullStreams } from 'child_process';
22
import { spawn } from 'child_process';
33

44
import { getEnvConfig } from '../config/config';
5+
import { TerminalColors } from '../terminal/TerminalColors';
56
import type { Logger } from '../util/Logger';
67

78
const NEWLINE_SEPARATOR = /\r\n|\r|\n/;
8-
const STDOUT_PREFIX = /^[a-z]+: /;
9+
const OUTPUT_PREFIX = /^([a-z]+: )/;
910

1011
export class CLI {
1112
constructor(private readonly logger: Logger) {}
@@ -44,10 +45,25 @@ export class CLI {
4445
.join('\n');
4546
}
4647

48+
public static colorizeOutput(output: string): string {
49+
return output
50+
.split(NEWLINE_SEPARATOR)
51+
.map(line => {
52+
if (line.startsWith('warn:')) {
53+
return (
54+
line.replace(OUTPUT_PREFIX, `$1${TerminalColors.yellow}`) +
55+
TerminalColors.reset
56+
);
57+
}
58+
return line;
59+
})
60+
.join('\n');
61+
}
62+
4763
public static removePrefixFromStdOut(output: string): string {
4864
return output
4965
.split(NEWLINE_SEPARATOR)
50-
.map(line => line.replace(STDOUT_PREFIX, ''))
66+
.map(line => line.replace(OUTPUT_PREFIX, ''))
5167
.join('\n');
5268
}
5369

src/terminal/Terminal.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,12 @@ import { type ChildProcessWithoutNullStreams } from 'child_process';
33

44
import { EventWaiter } from '../util/EventWaiter';
55
import { CLI } from '../cli/CLI';
6+
import { TerminalColors } from './TerminalColors';
67

78
const NL = '\n';
89
const CR = '\r';
910
const nlRegExp = new RegExp(`${NL}([^${CR}]|$)`, 'g');
1011

11-
class TerminalColors {
12-
public static blue = '\u001b[34m';
13-
public static reset = `\u001b[0m`;
14-
}
15-
1612
export class Terminal implements vscode.Pseudoterminal {
1713
private cmd: ChildProcessWithoutNullStreams | undefined;
1814

@@ -43,10 +39,10 @@ export class Terminal implements vscode.Pseudoterminal {
4339

4440
const { cmd, output } = this.cli.exec(cmdArgs, cwd, {
4541
onStdOut: (buffer: string) => {
46-
this.write(CLI.removePrefixFromStdOut(buffer));
42+
this.write(CLI.removePrefixFromStdOut(CLI.colorizeOutput(buffer)));
4743
},
4844
onStdErr: (buffer: string) => {
49-
this.write(CLI.removePrefixFromStdOut(buffer));
45+
this.write(CLI.removePrefixFromStdOut(CLI.colorizeOutput(buffer)));
5046
},
5147
});
5248
this.cmd = cmd;

src/terminal/TerminalColors.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export class TerminalColors {
2+
public static blue = '\u001b[34m';
3+
public static yellow = '\u001b[33m';
4+
public static reset = `\u001b[0m`;
5+
}

0 commit comments

Comments
 (0)