Skip to content

Commit c3e00a2

Browse files
Only use colors if we are certain we are using a pseudoterminal.
1 parent 2b4febe commit c3e00a2

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

src/compiler/sys.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ namespace ts {
272272
args: process.argv.slice(2),
273273
newLine: _os.EOL,
274274
useCaseSensitiveFileNames: useCaseSensitiveFileNames,
275-
write(s: string): void {
275+
write(s: string): void {
276276
process.stdout.write(s);
277277
},
278278
writesToTty: () => _tty.isatty(1),

src/compiler/tsc.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,11 @@ namespace ts {
108108
sys.write(output);
109109
}
110110

111-
const redForegroundEscapeSequence = "\u001b[91m";
112-
const gutterStyleSequence = "\u001b[100;30m";
113-
const resetEscapeSequence = "\u001b[0m";
111+
const shouldUseColors = sys.writesToTty && sys.writesToTty();
112+
const redForegroundEscapeSequence = shouldUseColors ? "\u001b[91m" : "";
113+
const gutterStyleSequence = shouldUseColors ? "\u001b[100;30m" : "";
114+
const gutterSeparator = shouldUseColors ? " " : " | "
115+
const resetEscapeSequence = shouldUseColors ? "\u001b[0m" : "";
114116

115117
function reportDiagnosticWithColorAndContext(diagnostic: Diagnostic): void {
116118
let output = "";
@@ -132,7 +134,7 @@ namespace ts {
132134
// If the error spans over 5 lines, we'll only show the first 2 and last 2 lines,
133135
// so we'll skip ahead to the second-to-last line.
134136
if (hasMoreThanFiveLines && firstLine + 1 < i && i < lastLine - 1) {
135-
output += gutterStyleSequence + padLeft("...", gutterWidth) + resetEscapeSequence + " " + sys.newLine;
137+
output += gutterStyleSequence + padLeft("...", gutterWidth) + resetEscapeSequence + gutterSeparator + sys.newLine;
136138
i = lastLine - 1;
137139
}
138140

@@ -142,11 +144,12 @@ namespace ts {
142144
lineContent = lineContent.replace(/\s+$/g, ""); // trim from end
143145
lineContent = lineContent.replace("\t", " "); // convert tabs to single spaces
144146

145-
// Output the actual contents of the line.
146-
output += gutterStyleSequence + padLeft(i + 1 + "", gutterWidth) + resetEscapeSequence + " " + lineContent + sys.newLine;
147+
// Output the gutter and the actual contents of the line.
148+
output += gutterStyleSequence + padLeft(i + 1 + "", gutterWidth) + resetEscapeSequence + gutterSeparator;
149+
output += lineContent + sys.newLine;
147150

148-
// Output the error span for the line using tildes.
149-
output += gutterStyleSequence + padLeft("", gutterWidth) + resetEscapeSequence + " ";
151+
// Output the gutter and the error span for the line using tildes.
152+
output += gutterStyleSequence + padLeft("", gutterWidth) + resetEscapeSequence + gutterSeparator;
150153
output += redForegroundEscapeSequence;
151154
if (i === firstLine) {
152155
// If we're on the last line, then limit it to the last character of the last line.

0 commit comments

Comments
 (0)