aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Ulf Adams <ulfjack@google.com>2017-06-19 17:03:19 +0200
committerGravatar Philipp Wollermann <philwo@google.com>2017-06-19 18:25:33 +0200
commitbaf7d4bce8bb14d785760d10694122e8ead2a177 (patch)
tree2dfaa124b1544a97bf5331ac11e0cd81d7377242
parent734b9e984f4d954dcb5dba3e2bbbd7819b889b5d (diff)
Extract the ansi color escape sequences to an enum
I want to make the color scheme configurable, which requires an abstraction to represent color, so it can be looked up / stored in a map / etc. Closes #2487. Change-Id: I2f8bd0dd19ecd6a243ac9b7acc7be52e59c90021 PiperOrigin-RevId: 159426774
-rw-r--r--src/main/java/com/google/devtools/build/lib/runtime/ExperimentalEventHandler.java9
-rw-r--r--src/main/java/com/google/devtools/build/lib/util/io/AnsiTerminal.java67
2 files changed, 47 insertions, 29 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/runtime/ExperimentalEventHandler.java b/src/main/java/com/google/devtools/build/lib/runtime/ExperimentalEventHandler.java
index 91e67e4454..0515276aca 100644
--- a/src/main/java/com/google/devtools/build/lib/runtime/ExperimentalEventHandler.java
+++ b/src/main/java/com/google/devtools/build/lib/runtime/ExperimentalEventHandler.java
@@ -37,6 +37,7 @@ import com.google.devtools.build.lib.pkgcache.LoadingPhaseCompleteEvent;
import com.google.devtools.build.lib.skyframe.LoadingPhaseStartedEvent;
import com.google.devtools.build.lib.util.Clock;
import com.google.devtools.build.lib.util.io.AnsiTerminal;
+import com.google.devtools.build.lib.util.io.AnsiTerminal.Color;
import com.google.devtools.build.lib.util.io.AnsiTerminalWriter;
import com.google.devtools.build.lib.util.io.LineCountingAnsiTerminalWriter;
import com.google.devtools.build.lib.util.io.LineWrappingAnsiTerminalWriter;
@@ -386,17 +387,17 @@ public class ExperimentalEventHandler implements EventHandler {
switch (kind) {
case ERROR:
case FAIL:
- terminal.textRed();
+ terminal.setTextColor(Color.RED);
terminal.textBold();
break;
case WARNING:
- terminal.textMagenta();
+ terminal.setTextColor(Color.MAGENTA);
break;
case INFO:
- terminal.textGreen();
+ terminal.setTextColor(Color.GREEN);
break;
case SUBCOMMAND:
- terminal.textBlue();
+ terminal.setTextColor(Color.BLUE);
break;
default:
terminal.resetTerminal();
diff --git a/src/main/java/com/google/devtools/build/lib/util/io/AnsiTerminal.java b/src/main/java/com/google/devtools/build/lib/util/io/AnsiTerminal.java
index 38c7fdce56..a28b8f7e57 100644
--- a/src/main/java/com/google/devtools/build/lib/util/io/AnsiTerminal.java
+++ b/src/main/java/com/google/devtools/build/lib/util/io/AnsiTerminal.java
@@ -15,12 +15,38 @@ package com.google.devtools.build.lib.util.io;
import java.io.IOException;
import java.io.OutputStream;
+import java.nio.charset.StandardCharsets;
/**
* A class which encapsulates the fancy curses-type stuff that you can do using
* standard ANSI terminal control sequences.
*/
public class AnsiTerminal {
+ /**
+ * An enumeration of all terminal colors, containing the escape sequences for both background and
+ * foreground settings.
+ */
+ public enum Color {
+ RED("^[31m", "^[41m"),
+ GREEN("^[32m", "^[42m"),
+ YELLOW("^[33m", "^[43m"),
+ BLUE("^[34m", "^[44m"),
+ MAGENTA("^[35m", "^[45m"),
+ CYAN("^[36m", "^[46m"),
+ GRAY("^[37m", "^[47m"),
+
+ DEFAULT("^[0m", "^[0m");
+
+ private final byte[] escapeSeq;
+ private final byte[] backgroundEscapeSeq;
+
+ private Color(String escapeSeq, String backgroundEscapeSeq) {
+ this.escapeSeq = escapeSeq.replace('^', (char) 27).getBytes(StandardCharsets.US_ASCII);
+ this.backgroundEscapeSeq =
+ backgroundEscapeSeq.replace('^', (char) 27).getBytes(StandardCharsets.US_ASCII);
+ }
+ }
+
private static final byte[] ESC = {27, (byte) '['};
private static final byte BEL = 7;
private static final byte UP = (byte) 'A';
@@ -29,23 +55,6 @@ public class AnsiTerminal {
private static final byte TEXT_BOLD = (byte) '1';
private static final byte[] SET_TERM_TITLE = {27, (byte) ']', (byte) '0', (byte) ';'};
- public static final String FG_BLACK = "30";
- public static final String FG_RED = "31";
- public static final String FG_GREEN = "32";
- public static final String FG_YELLOW = "33";
- public static final String FG_BLUE = "34";
- public static final String FG_MAGENTA = "35";
- public static final String FG_CYAN = "36";
- public static final String FG_GRAY = "37";
- public static final String BG_BLACK = "40";
- public static final String BG_RED = "41";
- public static final String BG_GREEN = "42";
- public static final String BG_YELLOW = "43";
- public static final String BG_BLUE = "44";
- public static final String BG_MAGENTA = "45";
- public static final String BG_CYAN = "46";
- public static final String BG_GRAY = "47";
-
public static byte[] CR = { 13 };
private final OutputStream out;
@@ -86,11 +95,19 @@ public class AnsiTerminal {
/**
* Set the color of the foreground or background of the terminal.
*
- * @param color one of the foreground or background color
- * constants
+ * @param color one of the foreground or background color constants
+ */
+ public void setTextColor(Color color) throws IOException {
+ writeBytes(color.escapeSeq);
+ }
+
+ /**
+ * Set the color of the foreground or background of the terminal.
+ *
+ * @param color one of the foreground or background color constants
*/
- public void setTextColor(String color) throws IOException {
- writeBytes(ESC, color.getBytes(), new byte[] { SET_GRAPHICS });
+ public void setBackgroundColor(Color color) throws IOException {
+ writeBytes(color.backgroundEscapeSeq);
}
/**
@@ -104,28 +121,28 @@ public class AnsiTerminal {
* Makes text print on the terminal in red.
*/
public void textRed() throws IOException {
- setTextColor(FG_RED);
+ setTextColor(Color.RED);
}
/**
* Makes text print on the terminal in blue.
*/
public void textBlue() throws IOException {
- setTextColor(FG_BLUE);
+ setTextColor(Color.BLUE);
}
/**
* Makes text print on the terminal in red.
*/
public void textGreen() throws IOException {
- setTextColor(FG_GREEN);
+ setTextColor(Color.GREEN);
}
/**
* Makes text print on the terminal in red.
*/
public void textMagenta() throws IOException {
- setTextColor(FG_MAGENTA);
+ setTextColor(Color.MAGENTA);
}
/**