Improve Vogar defaults for ANSI and color output
System.console() should be null if stdin (or stdout) is
redirected. When there is no console, default to a non-ANSI terminal.
When the terminal kind is ANSI, check whether TERM is defined and
contains the string color. If not, default to a non-color terminal.
Test: art/test/run-libcore-tests.sh --mode=host | tee log.txt
Test: art/test/run-libcore-tests.sh --mode=host
Bug: 130709497
Change-Id: Ibb2562c9cb4803d3a559a5f85f9e491f6f5e2433
diff --git a/src/vogar/Vogar.java b/src/vogar/Vogar.java
index 6f41f42..04e3333 100644
--- a/src/vogar/Vogar.java
+++ b/src/vogar/Vogar.java
@@ -55,6 +55,22 @@
return new File(System.getProperty("user.home", "."), name);
}
+ private static boolean maybeAnsiTerminal() {
+ if (System.console() == null) {
+ return false;
+ }
+ String terminal = System.getenv("TERM");
+ return terminal == null || !terminal.contains("dumb");
+ }
+
+ private static boolean maybeColorTerminal() {
+ if (!maybeAnsiTerminal()) {
+ return false;
+ }
+ String terminal = System.getenv("TERM");
+ return terminal != null && terminal.contains("color");
+ }
+
@Option(names = { "--expectations" })
Set<File> expectationFiles = new LinkedHashSet<File>();
{
@@ -101,7 +117,7 @@
boolean stream = true;
@Option(names = { "--color" })
- private boolean color = true;
+ private boolean color = maybeColorTerminal();
@Option(names = { "--pass-color" })
private int passColor = 32; // green
@@ -116,7 +132,7 @@
private int warnColor = 35; // purple
@Option(names = { "--ansi" })
- private boolean ansi = !"dumb".equals(System.getenv("TERM"));
+ private boolean ansi = maybeAnsiTerminal();
@Option(names = { "--debug" })
Integer debugPort;