Skip to content

Commit

Permalink
Use constants for properties, allow by-passing the new system
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Oct 23, 2020
1 parent 11b52d2 commit ef60bc5
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 20 deletions.
44 changes: 32 additions & 12 deletions jansi/src/main/java/org/fusesource/jansi/AnsiConsole.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,22 @@ public class AnsiConsole {

static final int ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004;

public static final String JANSI_PASSTHROUGH = "jansi.passthrough";
public static final String JANSI_STRIP = "jansi.strip";
public static final String JANSI_FORCE = "jansi.force";
public static final String JANSI_NO_OPTIMIZE = "jansi.no-optimize";
public static final String JANSI_DO_WRAP = "jansi.do-wrap";


private static JansiOutputType jansiOutputType;
static final JansiOutputType JANSI_STDOUT_TYPE;
static final JansiOutputType JANSI_STDERR_TYPE;

static {
out = ansiSystem(true);
boolean doWrap = getBoolean(JANSI_DO_WRAP);
out = doWrap ? wrapSystemOut(system_out) : ansiSystem(true);
JANSI_STDOUT_TYPE = jansiOutputType;
err = ansiSystem(false);
err = doWrap ? wrapSystemErr(system_err) : ansiSystem(false);
JANSI_STDERR_TYPE = jansiOutputType;
}

Expand All @@ -109,14 +118,14 @@ private static PrintStream ansiSystem(boolean stdout) {

// If the jansi.passthrough property is set, then don't interpret
// any of the ansi sequences.
if (Boolean.getBoolean("jansi.passthrough")) {
if (getBoolean(JANSI_PASSTHROUGH)) {
jansiOutputType = JansiOutputType.PASSTHROUGH;
return newPrintStream(out, enc);
}

// If the jansi.strip property is set, then we just strip the
// the ansi escapes.
if (Boolean.getBoolean("jansi.strip")) {
if (getBoolean(JANSI_STRIP)) {
jansiOutputType = JansiOutputType.STRIP_ANSI;
return new PrintStream(new AnsiNoSyncOutputStream(out, new AnsiProcessor(out)), true);
}
Expand Down Expand Up @@ -152,7 +161,7 @@ && SetConsoleMode(console, mode[0] | ENABLE_VIRTUAL_TERMINAL_PROCESSING) != 0) {
try {
// If the jansi.force property is set, then we force to output
// the ansi escapes for piping it into ansi color aware commands (e.g. less -r)
boolean forceColored = Boolean.getBoolean("jansi.force");
boolean forceColored = getBoolean(JANSI_FORCE);
// If we can detect that stdout is not a tty.. then setup
// to strip the ANSI sequences..
if (!forceColored && isatty(stdout ? 1 : 2) == 0) {
Expand Down Expand Up @@ -208,6 +217,17 @@ public void close() {
}
}

static boolean getBoolean(String name) {
boolean result = false;
try {
String val = System.getProperty(name);
result = val.isEmpty() || Boolean.parseBoolean(val);
} catch (IllegalArgumentException e) {
} catch (NullPointerException e) {
}
return result;
}

@Deprecated
public static OutputStream wrapOutputStream(final OutputStream stream) {
try {
Expand Down Expand Up @@ -247,14 +267,14 @@ public static OutputStream wrapOutputStream(final OutputStream stream, int filen

// If the jansi.passthrough property is set, then don't interpret
// any of the ansi sequences.
if (Boolean.getBoolean("jansi.passthrough")) {
if (getBoolean(JANSI_PASSTHROUGH)) {
jansiOutputType = JansiOutputType.PASSTHROUGH;
return stream;
}

// If the jansi.strip property is set, then we just strip the
// the ansi escapes.
if (Boolean.getBoolean("jansi.strip")) {
if (getBoolean(JANSI_STRIP)) {
jansiOutputType = JansiOutputType.STRIP_ANSI;
return new AnsiOutputStream(stream);
}
Expand All @@ -280,7 +300,7 @@ public static OutputStream wrapOutputStream(final OutputStream stream, int filen
try {
// If the jansi.force property is set, then we force to output
// the ansi escapes for piping it into ansi color aware commands (e.g. less -r)
boolean forceColored = Boolean.getBoolean("jansi.force");
boolean forceColored = getBoolean(JANSI_FORCE);
// If we can detect that stdout is not a tty.. then setup
// to strip the ANSI sequences..
if (!forceColored && isatty(fileno) == 0) {
Expand Down Expand Up @@ -325,7 +345,7 @@ public void close() throws IOException {
public static PrintStream wrapPrintStream(final PrintStream ps, int fileno) {
PrintStream result = doWrapPrintStream(ps, fileno);
if (result != ps) {
if (!Boolean.getBoolean("jansi.no-optimize")) {
if (!getBoolean(JANSI_NO_OPTIMIZE)) {
result = optimize(ps, result);
}
}
Expand All @@ -335,14 +355,14 @@ public static PrintStream wrapPrintStream(final PrintStream ps, int fileno) {
private static PrintStream doWrapPrintStream(final PrintStream ps, int fileno) {
// If the jansi.passthrough property is set, then don't interpret
// any of the ansi sequences.
if (Boolean.getBoolean("jansi.passthrough")) {
if (getBoolean(JANSI_PASSTHROUGH)) {
jansiOutputType = JansiOutputType.PASSTHROUGH;
return ps;
}

// If the jansi.strip property is set, then we just strip the
// the ansi escapes.
if (Boolean.getBoolean("jansi.strip")) {
if (getBoolean(JANSI_STRIP)) {
jansiOutputType = JansiOutputType.STRIP_ANSI;
return new AnsiPrintStream(ps);
}
Expand All @@ -368,7 +388,7 @@ private static PrintStream doWrapPrintStream(final PrintStream ps, int fileno) {
try {
// If the jansi.force property is set, then we force to output
// the ansi escapes for piping it into ansi color aware commands (e.g. less -r)
boolean forceColored = Boolean.getBoolean("jansi.force");
boolean forceColored = getBoolean(JANSI_FORCE);
// If we can detect that stdout is not a tty.. then setup
// to strip the ANSI sequences..
if (!forceColored && isatty(fileno) == 0) {
Expand Down
20 changes: 12 additions & 8 deletions jansi/src/main/java/org/fusesource/jansi/AnsiMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,22 @@ public static void main(String... args) throws IOException {

System.out.println();

System.out.println("jansi.passthrough= " + Boolean.getBoolean("jansi.passthrough"));
System.out.println("jansi.strip= " + Boolean.getBoolean("jansi.strip"));
System.out.println("jansi.force= " + Boolean.getBoolean("jansi.force"));
System.out.println(Ansi.DISABLE + "= " + Boolean.getBoolean(Ansi.DISABLE));
System.out.println(AnsiConsole.JANSI_PASSTHROUGH + "= " + AnsiConsole.getBoolean(AnsiConsole.JANSI_PASSTHROUGH));
System.out.println(AnsiConsole.JANSI_STRIP + "= " + AnsiConsole.getBoolean(AnsiConsole.JANSI_STRIP));
System.out.println(AnsiConsole.JANSI_FORCE + "= " + AnsiConsole.getBoolean(AnsiConsole.JANSI_FORCE));
System.out.println(AnsiConsole.JANSI_DO_WRAP + "= " + AnsiConsole.getBoolean(AnsiConsole.JANSI_DO_WRAP));
System.out.println(AnsiConsole.JANSI_NO_OPTIMIZE + "= " + AnsiConsole.getBoolean(AnsiConsole.JANSI_NO_OPTIMIZE));
System.out.println(Ansi.DISABLE + "= " + AnsiConsole.getBoolean(Ansi.DISABLE));

System.out.println();

System.out.println("IS_WINDOWS: " + AnsiConsole.IS_WINDOWS);
if (AnsiConsole.IS_WINDOWS) {
System.out.println("IS_CON_EMU_ANSI: " + AnsiConsole.IS_CON_EMU_ANSI);
System.out.println("IS_CONEMU: " + AnsiConsole.IS_CONEMU);
System.out.println("IS_CYGWIN: " + AnsiConsole.IS_CYGWIN);
System.out.println("IS_MINGW_XTERM: " + AnsiConsole.IS_MINGW_XTERM);
System.out.println("IS_MSYSTEM: " + AnsiConsole.IS_MSYSTEM);
System.out.println("IS_CON_EMU_ANSI: " + AnsiConsole.IS_CON_EMU_ANSI + " (deprecated)");
System.out.println("IS_MINGW_XTERM: " + AnsiConsole.IS_MINGW_XTERM + " (deprecated)");
}

System.out.println();
Expand All @@ -106,8 +110,8 @@ public static void main(String... args) throws IOException {
System.out.println(" - System.err: " + AnsiConsole.JANSI_STDERR_TYPE);
System.out.println("modes description:");
int n = 1;
for(AnsiConsole.JansiOutputType type: AnsiConsole.JansiOutputType.values()) {
System.out.println(n++ + ". " + type + ": " + type.getDescription());
for (AnsiConsole.JansiOutputType type: AnsiConsole.JansiOutputType.values()) {
System.out.println(" - " + n++ + ". " + type + ": " + type.getDescription());
}

try {
Expand Down

0 comments on commit ef60bc5

Please sign in to comment.