Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Jansi auto detection. #486

Merged
merged 2 commits into from
Sep 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/main/java/picocli/CommandLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -9182,8 +9182,18 @@ static final boolean calcTTY() {
try { return System.class.getDeclaredMethod("console").invoke(null) != null; }
catch (Throwable reflectionFailed) { return true; }
}
private static boolean ansiPossible() { return ISATTY && (!isWindows || isXterm); }
private static boolean ansiPossible() { return (ISATTY && (!isWindows || isXterm)) || isJansiEnabled(); }

private static boolean isJansiEnabled() {
try {
Class<?> ansiConsole = Class.forName("org.fusesource.jansi.AnsiConsole");
Field out = ansiConsole.getField("out");
return out.get(null) == System.out;
} catch (Exception reflectionFailed) {
return false;
}
}

/** Returns {@code true} if ANSI escape codes should be emitted, {@code false} otherwise.
* @return ON: {@code true}, OFF: {@code false}, AUTO: if system property {@code "picocli.ansi"} is
* defined then return its boolean value, otherwise return whether the platform supports ANSI escape codes */
Expand Down
8 changes: 8 additions & 0 deletions src/test/java/picocli/CommandLineHelpTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@
import java.util.concurrent.TimeUnit;

import static java.lang.String.format;
import org.fusesource.jansi.AnsiConsole;
import static org.junit.Assert.*;
import static picocli.CommandLine.Help.Ansi.ISATTY;
import static picocli.CommandLine.Help.Visibility.*;
import static picocli.HelpTestUtil.textArray;
import static picocli.HelpTestUtil.usageString;
Expand Down Expand Up @@ -2800,6 +2802,12 @@ public void testAnsiEnabled() {
boolean isAtty = (isWindows && isXterm) // cygwin pseudo-tty
|| hasConsole();
assertEquals(isAtty && (!isWindows || isXterm), Help.Ansi.AUTO.enabled());

if (isWindows) {
AnsiConsole.systemInstall();
assertTrue(Help.Ansi.AUTO.enabled());
AnsiConsole.systemUninstall();
}
}

private boolean hasConsole() {
Expand Down