Skip to content

Commit

Permalink
Opt-out switch to disable colored console output
Browse files Browse the repository at this point in the history
  • Loading branch information
marcphilipp committed Nov 14, 2015
1 parent 90747ae commit 31ec7d0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
Expand Up @@ -29,9 +29,11 @@
public class ColoredPrintingTestListener implements TestPlanExecutionListener, TestExecutionListener {

private final PrintStream out;
private final boolean disableAnsiColors;

public ColoredPrintingTestListener(PrintStream out) {
public ColoredPrintingTestListener(PrintStream out, boolean disableAnsiColors) {
this.out = out;
this.disableAnsiColors = disableAnsiColors;
}

@Override
Expand Down Expand Up @@ -110,9 +112,18 @@ private void printlnException(Color color, Throwable throwable) {
println(color, " => Exception: %s", throwable.getLocalizedMessage());
}

void println(Color color, String format, Object... args) {
// Use string concatenation to avoid ansi disruption on console
out.println(color + String.format(format, args) + NONE);
private void println(Color color, String format, Object... args) {
println(color, String.format(format, args));
}

private void println(Color color, String message) {
if (disableAnsiColors) {
out.println(message);
}
else {
// Use string concatenation to avoid ansi disruption on console
out.println(color + message + NONE);
}
}

enum Color {
Expand Down
Expand Up @@ -41,6 +41,12 @@ public class ConsoleRunner {
private boolean enableExitCode;
// @formatter:on

// @formatter:off
@Option(name = { "-C", "--disable-ansi-colors" },
description = "Disable colored output (not supported by all terminals)")
private boolean disableAnsiColors;
// @formatter:on

@Arguments(description = "Test classes to execute")
private List<String> testClasses;

Expand All @@ -59,7 +65,7 @@ private void run() {
TestSummaryReportingTestListener testSummaryListener = new TestSummaryReportingTestListener(System.out);
launcher.registerTestPlanExecutionListeners(
// @formatter:off
new ColoredPrintingTestListener(System.out),
new ColoredPrintingTestListener(System.out, disableAnsiColors),
testSummaryListener
// @formatter:on
);
Expand Down

0 comments on commit 31ec7d0

Please sign in to comment.