Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(48)

Unified Diff: pkg/front_end/lib/src/fasta/colors.dart

Issue 2765223002: Change code to determine ANSI support on Windows (Closed)
Patch Set: Address comments Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/front_end/lib/src/fasta/colors.dart
diff --git a/pkg/front_end/lib/src/fasta/colors.dart b/pkg/front_end/lib/src/fasta/colors.dart
index 649229f65bce29cf7fd660b50e2bf2e537c01f6e..c993caabcec82d70461e6dff45dd92c3a40a8b2c 100644
--- a/pkg/front_end/lib/src/fasta/colors.dart
+++ b/pkg/front_end/lib/src/fasta/colors.dart
@@ -8,8 +8,7 @@ library colors;
import 'dart:convert' show JSON;
-import 'dart:io'
- show Platform, Process, ProcessResult, StdioType, stderr, stdioType, stdout;
+import 'dart:io' show Platform, Process, ProcessResult, stderr, stdout;
import 'compiler_context.dart' show CompilerContext;
@@ -90,45 +89,56 @@ String magenta(String string) => wrap(string, MAGENTA_COLOR);
String cyan(String string) => wrap(string, CYAN_COLOR);
String white(String string) => wrap(string, WHITE_COLOR);
-/// True if we should enable colors in output. We enable colors when:
-/// 1. stdout and stderr are terminals,
-/// 2. the terminal supports more than 8 colors, and
-/// 3. the terminal's ANSI color codes matches what we expect.
-///
-/// Note: do not call this method directly, as it is expensive to
-/// compute. Instead, use [CompilerContext.enableColors].
-bool computeEnableColors(CompilerContext context) {
- bool ansiSupported;
+/// Returns whether [sink] supports ANSI escapes or `null` if it could not be
+/// determined.
+bool _supportsAnsiEscapes(sink) {
try {
// ignore: undefined_getter
- ansiSupported = Platform.ansiSupported;
+ return sink.supportsAnsiEscapes;
} on NoSuchMethodError {
// Ignored: We're running on an older version of the Dart VM which doesn't
- // implement `ansiSupported`.
+ // implement `supportsAnsiEscapes`.
+ return null;
}
+}
- if (ansiSupported == false) {
- if (context.options.verbose) {
- print("Not enabling colors, 'Platform.ansiSupported' is false.");
- }
- return false;
- }
+/// True if we should enable colors in output.
+///
+/// We enable colors when both `stdout` and `stderr` support ANSI escapes.
+///
+/// On non-Windows platforms, this functions checks the terminal capabilities,
+/// on Windows we only enable colors if the VM getters are present and returned
+/// `true`.
+///
+/// Note: do not call this method directly, as it is expensive to
+/// compute. Instead, use [CompilerContext.enableColors].
+bool computeEnableColors(CompilerContext context) {
+ bool stderrSupportsColors = _supportsAnsiEscapes(stdout);
+ bool stdoutSupportsColors = _supportsAnsiEscapes(stderr);
- if (stdioType(stderr) != StdioType.TERMINAL) {
+ if (stdoutSupportsColors == false) {
if (context.options.verbose) {
- print("Not enabling colors, stderr isn't a terminal.");
+ print("Not enabling colors, stdout does not support ANSI colors.");
}
return false;
}
-
- if (stdioType(stdout) != StdioType.TERMINAL) {
+ if (stderrSupportsColors == false) {
if (context.options.verbose) {
- print("Not enabling colors, stdout isn't a terminal.");
+ print("Not enabling colors, stderr does not support ANSI colors.");
}
return false;
}
- if (ansiSupported == true && Platform.isWindows) {
+ if (Platform.isWindows) {
+ if (stderrSupportsColors != true || stdoutSupportsColors != true) {
+ // In this case, either [stdout] or [stderr] did not support the
+ // property `supportsAnsiEscapes`. Since we do not have another way
+ // to determine support for colors, we disable them.
+ if (context.options.verbose) {
+ print("Not enabling colors as ANSI is not supported.");
+ }
+ return false;
+ }
if (context.options.verbose) {
print("Enabling colors as OS is Windows.");
}
@@ -136,7 +146,8 @@ bool computeEnableColors(CompilerContext context) {
}
// We have to check if the terminal actually supports colors. Currently,
- // `Platform.ansiSupported` is hard-coded to true on non-Windows platforms.
+ // to avoid linking the Dart VM with ncurses, ANSI escape support is reduced
+ // to `Platform.environment['TERM'].contains("xterm")`.
// The `-S` option of `tput` allows us to query multiple capabilities at
// once.
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698