Skip to content

Commit 1f85123

Browse files
Orta TheroxDanielRosenwasser
Orta Therox
andauthored
Revisions for the new --help (#44800)
* WIP on refining the new --help * Fix types in the boolean trivial lint rule * Update baselines * More work * Updates the color logic * Simplifies the CLI color code * Use cyan instead of blue for win powershell/command prompt * Use bright white when blue is probably going to look off * Fix NO_COLOR * Adds a test to cover NO_COLOR * Update src/compiler/diagnosticMessages.json Co-authored-by: Daniel Rosenwasser <[email protected]> * Update compiler diagnostic text Co-authored-by: Daniel Rosenwasser <[email protected]>
1 parent 635f5bd commit 1f85123

File tree

22 files changed

+290
-110
lines changed

22 files changed

+290
-110
lines changed

Diff for: src/compiler/commandLineParser.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ namespace ts {
223223
type: "boolean",
224224
showInSimplifiedHelpView: true,
225225
category: Diagnostics.Output_Formatting,
226-
description: Diagnostics.Enable_color_and_formatting_in_output_to_make_compiler_errors_easier_to_read,
226+
description: Diagnostics.Enable_color_and_formatting_in_TypeScript_s_output_to_make_compiler_errors_easier_to_read,
227227
defaultValueDescription: "true"
228228
},
229229
{
@@ -553,7 +553,7 @@ namespace ts {
553553
type: "boolean",
554554
showInSimplifiedHelpView: true,
555555
category: Diagnostics.Emit,
556-
description: Diagnostics.Disable_emitting_file_from_a_compilation,
556+
description: Diagnostics.Disable_emitting_files_from_a_compilation,
557557
transpileOptionValue: undefined,
558558
defaultValueDescription: "false"
559559
},

Diff for: src/compiler/diagnosticMessages.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -5459,7 +5459,7 @@
54595459
"category": "Message",
54605460
"code": 6659
54615461
},
5462-
"Disable emitting file from a compilation.": {
5462+
"Disable emitting files from a compilation.": {
54635463
"category": "Message",
54645464
"code": 6660
54655465
},
@@ -5559,7 +5559,7 @@
55595559
"category": "Message",
55605560
"code": 6684
55615561
},
5562-
"Enable color and formatting in output to make compiler errors easier to read": {
5562+
"Enable color and formatting in TypeScript's output to make compiler errors easier to read": {
55635563
"category": "Message",
55645564
"code": 6685
55655565
},
@@ -5788,7 +5788,7 @@
57885788
"category": "Message",
57895789
"code": 6923
57905790
},
5791-
"Ignoring tsconfig.json, compiles the specified files with default compiler options": {
5791+
"Ignoring tsconfig.json, compiles the specified files with default compiler options.": {
57925792
"category": "Message",
57935793
"code": 6924
57945794
},
@@ -5800,15 +5800,15 @@
58005800
"category": "Message",
58015801
"code": 6926
58025802
},
5803-
"Compiles the TypeScript project located at the specified path": {
5803+
"Compiles the TypeScript project located at the specified path.": {
58045804
"category": "Message",
58055805
"code": 6927
58065806
},
58075807
"An expanded version of this information, showing all possible compiler options": {
58085808
"category": "Message",
58095809
"code": 6928
58105810
},
5811-
"Compiles the current project, with additional settings": {
5811+
"Compiles the current project, with additional settings.": {
58125812
"category": "Message",
58135813
"code": 6929
58145814
},

Diff for: src/executeCommandLine/executeCommandLine.ts

+31-10
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ namespace ts {
6868
}
6969

7070
function defaultIsPretty(sys: System) {
71-
return !!sys.writeOutputIsTTY && sys.writeOutputIsTTY();
71+
return !!sys.writeOutputIsTTY && sys.writeOutputIsTTY() && !sys.getEnvironmentVariable("NO_COLOR");
7272
}
7373

7474
function shouldBePretty(sys: System, options: CompilerOptions | BuildOptions) {
@@ -96,26 +96,47 @@ namespace ts {
9696
bold: (str: string) => str,
9797
blue: (str: string) => str,
9898
blueBackground: (str: string) => str,
99-
white: (str: string) => str
99+
brightWhite: (str: string) => str
100100
};
101101
}
102102

103103
function bold(str: string) {
104104
return `\x1b[1m${str}\x1b[22m`;
105105
}
106+
107+
const isWindows = sys.getEnvironmentVariable("OS") && stringContains(sys.getEnvironmentVariable("OS").toLowerCase(), "windows");
108+
const isWindowsTerminal = sys.getEnvironmentVariable("WT_SESSION");
109+
const isVSCode = sys.getEnvironmentVariable("TERM_PROGRAM") && sys.getEnvironmentVariable("TERM_PROGRAM") === "vscode";
110+
106111
function blue(str: string) {
107-
return `\x1b[34m${str}\x1b[39m`;
112+
// Effectively Powershell and Command prompt users use cyan instead
113+
// of blue because the default theme doesn't show blue with enough contrast.
114+
if (isWindows && !isWindowsTerminal && !isVSCode) {
115+
return brightWhite(str);
116+
}
117+
118+
return `\x1b[94m${str}\x1b[39m`;
108119
}
120+
121+
// There are ~3 types of terminal color support: 16 colors, 256 and 16m colors
122+
// If there is richer color support, e.g. 256+ we can use extended ANSI codes which are not just generic 'blue'
123+
// but a 'lighter blue' which is closer to the blue in the TS logo.
124+
const supportsRicherColors = sys.getEnvironmentVariable("COLORTERM") === "truecolor" || sys.getEnvironmentVariable("TERM") === "xterm-256color";
109125
function blueBackground(str: string) {
110-
return `\x1b[44m${str}\x1b[49m`;
126+
if (supportsRicherColors) {
127+
return `\x1B[48;5;68m${str}\x1B[39;49m`;
128+
}
129+
else {
130+
return `\x1b[44m${str}\x1B[39;49m`;
131+
}
111132
}
112-
function white(str: string) {
113-
return `\x1b[37m${str}\x1b[39m`;
133+
function brightWhite(str: string) {
134+
return `\x1b[97m${str}\x1b[39m`;
114135
}
115136
return {
116137
bold,
117138
blue,
118-
white,
139+
brightWhite,
119140
blueBackground
120141
};
121142
}
@@ -143,7 +164,7 @@ namespace ts {
143164
const terminalWidth = sys.getWidthOfTerminal?.() ?? 0;
144165

145166
// Note: child_process might return `terminalWidth` as undefined.
146-
if (terminalWidth >= 60) {
167+
if (terminalWidth >= 80) {
147168
let description = "";
148169
if (option.description) {
149170
description = getDiagnosticText(option.description);
@@ -340,7 +361,7 @@ namespace ts {
340361
example("tsc app.ts util.ts", Diagnostics.Ignoring_tsconfig_json_compiles_the_specified_files_with_default_compiler_options);
341362
example("tsc -b", Diagnostics.Build_a_composite_project_in_the_working_directory);
342363
example("tsc --init", Diagnostics.Creates_a_tsconfig_json_with_the_recommended_settings_in_the_working_directory);
343-
example("tsc -p .path/to/tsconfig.json", Diagnostics.Compiles_the_TypeScript_project_located_at_the_specified_path);
364+
example("tsc -p ./path/to/tsconfig.json", Diagnostics.Compiles_the_TypeScript_project_located_at_the_specified_path);
344365
example("tsc --help --all", Diagnostics.An_expanded_version_of_this_information_showing_all_possible_compiler_options);
345366
example(["tsc --noEmit", "tsc --target esnext"], Diagnostics.Compiles_the_current_project_with_additional_settings);
346367

@@ -392,7 +413,7 @@ namespace ts {
392413
const tsIconLength = 5;
393414

394415
const tsIconFirstLine = colors.blueBackground(padLeft("", tsIconLength));
395-
const tsIconSecondLine = colors.blueBackground(colors.white(padLeft("TS ", tsIconLength)));
416+
const tsIconSecondLine = colors.blueBackground(colors.brightWhite(padLeft("TS ", tsIconLength)));
396417
// If we have enough space, print TS icon.
397418
if (terminalWidth >= tscExplanation.length + tsIconLength) {
398419
// right align of the icon is 120 at most.

Diff for: src/testRunner/unittests/tsc/runWithoutArgs.ts

+9
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,14 @@ namespace ts {
1414
fs: () => loadProjectFromFiles({}),
1515
commandLineArgs: [],
1616
});
17+
18+
verifyTsc({
19+
scenario: "runWithoutArgs",
20+
subScenario: "does not add color when NO_COLOR is set",
21+
fs: () => loadProjectFromFiles({}),
22+
commandLineArgs: [],
23+
environmentVariables: { NO_COLOR: "true" }
24+
});
25+
1726
});
1827
}

Diff for: tests/baselines/reference/tsConfig/Default initialized TSConfig/tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If `declaration` is true, also designates a file that bundles all .d.ts output. */
5050
// "outDir": "./", /* Specify an output folder for all emitted files. */
5151
// "removeComments": true, /* Disable emitting comments. */
52-
// "noEmit": true, /* Disable emitting file from a compilation. */
52+
// "noEmit": true, /* Disable emitting files from a compilation. */
5353
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
5454
// "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types */
5555
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */

Diff for: tests/baselines/reference/tsConfig/Initialized TSConfig with advanced options/tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If `declaration` is true, also designates a file that bundles all .d.ts output. */
5050
// "outDir": "./", /* Specify an output folder for all emitted files. */
5151
// "removeComments": true, /* Disable emitting comments. */
52-
// "noEmit": true, /* Disable emitting file from a compilation. */
52+
// "noEmit": true, /* Disable emitting files from a compilation. */
5353
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
5454
// "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types */
5555
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */

Diff for: tests/baselines/reference/tsConfig/Initialized TSConfig with boolean value compiler options/tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If `declaration` is true, also designates a file that bundles all .d.ts output. */
5050
// "outDir": "./", /* Specify an output folder for all emitted files. */
5151
// "removeComments": true, /* Disable emitting comments. */
52-
// "noEmit": true, /* Disable emitting file from a compilation. */
52+
// "noEmit": true, /* Disable emitting files from a compilation. */
5353
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
5454
// "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types */
5555
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */

Diff for: tests/baselines/reference/tsConfig/Initialized TSConfig with enum value compiler options/tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If `declaration` is true, also designates a file that bundles all .d.ts output. */
5050
// "outDir": "./", /* Specify an output folder for all emitted files. */
5151
// "removeComments": true, /* Disable emitting comments. */
52-
// "noEmit": true, /* Disable emitting file from a compilation. */
52+
// "noEmit": true, /* Disable emitting files from a compilation. */
5353
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
5454
// "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types */
5555
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */

Diff for: tests/baselines/reference/tsConfig/Initialized TSConfig with files options/tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If `declaration` is true, also designates a file that bundles all .d.ts output. */
5050
// "outDir": "./", /* Specify an output folder for all emitted files. */
5151
// "removeComments": true, /* Disable emitting comments. */
52-
// "noEmit": true, /* Disable emitting file from a compilation. */
52+
// "noEmit": true, /* Disable emitting files from a compilation. */
5353
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
5454
// "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types */
5555
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */

Diff for: tests/baselines/reference/tsConfig/Initialized TSConfig with incorrect compiler option value/tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If `declaration` is true, also designates a file that bundles all .d.ts output. */
5050
// "outDir": "./", /* Specify an output folder for all emitted files. */
5151
// "removeComments": true, /* Disable emitting comments. */
52-
// "noEmit": true, /* Disable emitting file from a compilation. */
52+
// "noEmit": true, /* Disable emitting files from a compilation. */
5353
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
5454
// "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types */
5555
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */

Diff for: tests/baselines/reference/tsConfig/Initialized TSConfig with incorrect compiler option/tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If `declaration` is true, also designates a file that bundles all .d.ts output. */
5050
// "outDir": "./", /* Specify an output folder for all emitted files. */
5151
// "removeComments": true, /* Disable emitting comments. */
52-
// "noEmit": true, /* Disable emitting file from a compilation. */
52+
// "noEmit": true, /* Disable emitting files from a compilation. */
5353
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
5454
// "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types */
5555
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */

Diff for: tests/baselines/reference/tsConfig/Initialized TSConfig with list compiler options with enum value/tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If `declaration` is true, also designates a file that bundles all .d.ts output. */
5050
// "outDir": "./", /* Specify an output folder for all emitted files. */
5151
// "removeComments": true, /* Disable emitting comments. */
52-
// "noEmit": true, /* Disable emitting file from a compilation. */
52+
// "noEmit": true, /* Disable emitting files from a compilation. */
5353
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
5454
// "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types */
5555
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */

Diff for: tests/baselines/reference/tsConfig/Initialized TSConfig with list compiler options/tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If `declaration` is true, also designates a file that bundles all .d.ts output. */
5050
// "outDir": "./", /* Specify an output folder for all emitted files. */
5151
// "removeComments": true, /* Disable emitting comments. */
52-
// "noEmit": true, /* Disable emitting file from a compilation. */
52+
// "noEmit": true, /* Disable emitting files from a compilation. */
5353
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
5454
// "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types */
5555
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */

0 commit comments

Comments
 (0)