Skip to content

Commit eb5d8d0

Browse files
author
Josh Goldberg
committed
Prettified timestamps and error reports in --pretty
Timestamps look like Gulp's, with grey times inside white brackets. Files have cyan filenames, yellow line and column numbers, and grey TS{####} errors. I wonder if those are actually useful for folks using the --pretty CLI: are they used for anything outside Visual Studio... Can we just get rid of them? Re-uses compiler/program's color logic in compiler/watch. The relevant variables are now exported and marked `@internal`. Is there a preferred way of re-using this code in both those files?
1 parent 49a48ff commit eb5d8d0

2 files changed

Lines changed: 35 additions & 16 deletions

File tree

src/compiler/program.ts

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -241,22 +241,27 @@ namespace ts {
241241
return errorMessage;
242242
}
243243

244-
const redForegroundEscapeSequence = "\u001b[91m";
245-
const yellowForegroundEscapeSequence = "\u001b[93m";
246-
const blueForegroundEscapeSequence = "\u001b[93m";
244+
/** @internal */
245+
export const foregroundColorEscapeSequences = {
246+
grey: "\u001b[90m",
247+
red: "\u001b[91m",
248+
yellow: "\u001b[93m",
249+
cyan: "\u001b[96m"
250+
};
247251
const gutterStyleSequence = "\u001b[30;47m";
248252
const gutterSeparator = " ";
249253
const resetEscapeSequence = "\u001b[0m";
250254
const ellipsis = "...";
251255
function getCategoryFormat(category: DiagnosticCategory): string {
252256
switch (category) {
253-
case DiagnosticCategory.Warning: return yellowForegroundEscapeSequence;
254-
case DiagnosticCategory.Error: return redForegroundEscapeSequence;
255-
case DiagnosticCategory.Message: return blueForegroundEscapeSequence;
257+
case DiagnosticCategory.Warning: return foregroundColorEscapeSequences.yellow;
258+
case DiagnosticCategory.Error: return foregroundColorEscapeSequences.red;
259+
case DiagnosticCategory.Message: return foregroundColorEscapeSequences.yellow;
256260
}
257261
}
258262

259-
function formatAndReset(text: string, formatStyle: string) {
263+
/** @internal */
264+
export function formatColorAndReset(text: string, formatStyle: string) {
260265
return formatStyle + text + resetEscapeSequence;
261266
}
262267

@@ -289,7 +294,7 @@ namespace ts {
289294
// If the error spans over 5 lines, we'll only show the first 2 and last 2 lines,
290295
// so we'll skip ahead to the second-to-last line.
291296
if (hasMoreThanFiveLines && firstLine + 1 < i && i < lastLine - 1) {
292-
context += formatAndReset(padLeft(ellipsis, gutterWidth), gutterStyleSequence) + gutterSeparator + host.getNewLine();
297+
context += formatColorAndReset(padLeft(ellipsis, gutterWidth), gutterStyleSequence) + gutterSeparator + host.getNewLine();
293298
i = lastLine - 1;
294299
}
295300

@@ -300,12 +305,12 @@ namespace ts {
300305
lineContent = lineContent.replace("\t", " "); // convert tabs to single spaces
301306

302307
// Output the gutter and the actual contents of the line.
303-
context += formatAndReset(padLeft(i + 1 + "", gutterWidth), gutterStyleSequence) + gutterSeparator;
308+
context += formatColorAndReset(padLeft(i + 1 + "", gutterWidth), gutterStyleSequence) + gutterSeparator;
304309
context += lineContent + host.getNewLine();
305310

306311
// Output the gutter and the error span for the line using tildes.
307-
context += formatAndReset(padLeft("", gutterWidth), gutterStyleSequence) + gutterSeparator;
308-
context += redForegroundEscapeSequence;
312+
context += formatColorAndReset(padLeft("", gutterWidth), gutterStyleSequence) + gutterSeparator;
313+
context += foregroundColorEscapeSequences.red;
309314
if (i === firstLine) {
310315
// If we're on the last line, then limit it to the last character of the last line.
311316
// Otherwise, we'll just squiggle the rest of the line, giving 'slice' no end position.
@@ -324,13 +329,19 @@ namespace ts {
324329
context += resetEscapeSequence;
325330
}
326331

327-
output += host.getNewLine();
328-
output += `${ relativeFileName }(${ firstLine + 1 },${ firstLineChar + 1 }): `;
332+
output += formatColorAndReset(relativeFileName, foregroundColorEscapeSequences.cyan);
333+
output += "(";
334+
output += formatColorAndReset(`${ firstLine + 1 }`, foregroundColorEscapeSequences.yellow);
335+
output += ",";
336+
output += formatColorAndReset(`${ firstLineChar + 1 }`, foregroundColorEscapeSequences.yellow);
337+
output += "): ";
329338
}
330339

331340
const categoryColor = getCategoryFormat(diagnostic.category);
332341
const category = DiagnosticCategory[diagnostic.category].toLowerCase();
333-
output += `${ formatAndReset(category, categoryColor) } TS${ diagnostic.code }: ${ flattenDiagnosticMessageText(diagnostic.messageText, host.getNewLine()) }`;
342+
output += formatColorAndReset(category, categoryColor);
343+
output += formatColorAndReset(` TS${ diagnostic.code }: `, foregroundColorEscapeSequences.grey);
344+
output += flattenDiagnosticMessageText(diagnostic.messageText, host.getNewLine());
334345

335346
if (diagnostic.file) {
336347
output += host.getNewLine();
@@ -339,7 +350,7 @@ namespace ts {
339350

340351
output += host.getNewLine();
341352
}
342-
return output;
353+
return output + host.getNewLine();
343354
}
344355

345356
export function flattenDiagnosticMessageText(messageText: string | DiagnosticMessageChain, newLine: string): string {

src/compiler/watch.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ namespace ts {
4848
};
4949
}
5050

51+
export function createWatchDiagnosticReporterWithColor(system = sys): DiagnosticReporter {
52+
return diagnostic => {
53+
let output = `[${ formatColorAndReset(new Date().toLocaleTimeString(), foregroundColorEscapeSequences.grey) }] `;
54+
output += `${flattenDiagnosticMessageText(diagnostic.messageText, system.newLine)}${system.newLine + system.newLine + system.newLine}`;
55+
system.write(output);
56+
};
57+
}
58+
5159
export function reportDiagnostics(diagnostics: Diagnostic[], reportDiagnostic: DiagnosticReporter): void {
5260
for (const diagnostic of diagnostics) {
5361
reportDiagnostic(diagnostic);
@@ -131,7 +139,7 @@ namespace ts {
131139
reportWatchDiagnostic?: DiagnosticReporter
132140
): WatchingSystemHost {
133141
reportDiagnostic = reportDiagnostic || createDiagnosticReporter(system, pretty ? reportDiagnosticWithColorAndContext : reportDiagnosticSimply);
134-
reportWatchDiagnostic = reportWatchDiagnostic || createWatchDiagnosticReporter(system);
142+
reportWatchDiagnostic = reportWatchDiagnostic || pretty ? createWatchDiagnosticReporterWithColor(system) : createWatchDiagnosticReporter(system);
135143
parseConfigFile = parseConfigFile || ts.parseConfigFile;
136144
return {
137145
system,

0 commit comments

Comments
 (0)