Skip to content

Instantly share code, notes, and snippets.

@missionarydev
Created February 9, 2018 02:40
Show Gist options
  • Save missionarydev/b89afd42709295a817453ab997d9e1e7 to your computer and use it in GitHub Desktop.
Save missionarydev/b89afd42709295a817453ab997d9e1e7 to your computer and use it in GitHub Desktop.
private static void initializeTerminal() {
if (!initialized) {
initialized = true;
// A system property can be used to override our automatic detection
Boolean jlineOverride = getOptionalBooleanProperty(JLINE_OVERRIDE_PROPERTY);
// By default, we disable JLine if there is no terminal attached
// (e.g. if the program output is redirected to a file or if it's
// started by some kind of control panel)
// The same applies to IDEs, they usually provide only a very basic
// console implementation without support for ANSI escape codes
// (used for colors) or characters like \r.
// There are two exceptions:
// 1. IntelliJ IDEA supports colors and control characters
// (We try to detect it using an additional JAR it adds to the classpath)
// 2. The system property forces the use of JLine.
boolean dumb = jlineOverride == Boolean.TRUE || System.getProperty("java.class.path").contains("idea_rt.jar");
if (jlineOverride != Boolean.FALSE) {
try {
terminal = TerminalBuilder.builder().dumb(dumb).build();
} catch (IllegalStateException e) {
// Unless disabled using one of the exceptions above,
// JLine throws an exception before creating a dumb terminal
// Dumb terminals are used if there is no real terminal attached
// to the application.
if (LOGGER.isDebugEnabled()) {
// Log with stacktrace
LOGGER.warn("Disabling terminal, you're running in an unsupported environment.", e);
} else {
LOGGER.warn("Disabling terminal, you're running in an unsupported environment.");
}
} catch (IOException e) {
LOGGER.error("Failed to initialize terminal. Falling back to standard output", e);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment