Skip to content

Commit ef60bc5

Browse files
committed
Use constants for properties, allow by-passing the new system
1 parent 11b52d2 commit ef60bc5

File tree

2 files changed

+44
-20
lines changed

2 files changed

+44
-20
lines changed

jansi/src/main/java/org/fusesource/jansi/AnsiConsole.java

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,22 @@ public class AnsiConsole {
8787

8888
static final int ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004;
8989

90+
public static final String JANSI_PASSTHROUGH = "jansi.passthrough";
91+
public static final String JANSI_STRIP = "jansi.strip";
92+
public static final String JANSI_FORCE = "jansi.force";
93+
public static final String JANSI_NO_OPTIMIZE = "jansi.no-optimize";
94+
public static final String JANSI_DO_WRAP = "jansi.do-wrap";
95+
96+
9097
private static JansiOutputType jansiOutputType;
9198
static final JansiOutputType JANSI_STDOUT_TYPE;
9299
static final JansiOutputType JANSI_STDERR_TYPE;
100+
93101
static {
94-
out = ansiSystem(true);
102+
boolean doWrap = getBoolean(JANSI_DO_WRAP);
103+
out = doWrap ? wrapSystemOut(system_out) : ansiSystem(true);
95104
JANSI_STDOUT_TYPE = jansiOutputType;
96-
err = ansiSystem(false);
105+
err = doWrap ? wrapSystemErr(system_err) : ansiSystem(false);
97106
JANSI_STDERR_TYPE = jansiOutputType;
98107
}
99108

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

110119
// If the jansi.passthrough property is set, then don't interpret
111120
// any of the ansi sequences.
112-
if (Boolean.getBoolean("jansi.passthrough")) {
121+
if (getBoolean(JANSI_PASSTHROUGH)) {
113122
jansiOutputType = JansiOutputType.PASSTHROUGH;
114123
return newPrintStream(out, enc);
115124
}
116125

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

220+
static boolean getBoolean(String name) {
221+
boolean result = false;
222+
try {
223+
String val = System.getProperty(name);
224+
result = val.isEmpty() || Boolean.parseBoolean(val);
225+
} catch (IllegalArgumentException e) {
226+
} catch (NullPointerException e) {
227+
}
228+
return result;
229+
}
230+
211231
@Deprecated
212232
public static OutputStream wrapOutputStream(final OutputStream stream) {
213233
try {
@@ -247,14 +267,14 @@ public static OutputStream wrapOutputStream(final OutputStream stream, int filen
247267

248268
// If the jansi.passthrough property is set, then don't interpret
249269
// any of the ansi sequences.
250-
if (Boolean.getBoolean("jansi.passthrough")) {
270+
if (getBoolean(JANSI_PASSTHROUGH)) {
251271
jansiOutputType = JansiOutputType.PASSTHROUGH;
252272
return stream;
253273
}
254274

255275
// If the jansi.strip property is set, then we just strip the
256276
// the ansi escapes.
257-
if (Boolean.getBoolean("jansi.strip")) {
277+
if (getBoolean(JANSI_STRIP)) {
258278
jansiOutputType = JansiOutputType.STRIP_ANSI;
259279
return new AnsiOutputStream(stream);
260280
}
@@ -280,7 +300,7 @@ public static OutputStream wrapOutputStream(final OutputStream stream, int filen
280300
try {
281301
// If the jansi.force property is set, then we force to output
282302
// the ansi escapes for piping it into ansi color aware commands (e.g. less -r)
283-
boolean forceColored = Boolean.getBoolean("jansi.force");
303+
boolean forceColored = getBoolean(JANSI_FORCE);
284304
// If we can detect that stdout is not a tty.. then setup
285305
// to strip the ANSI sequences..
286306
if (!forceColored && isatty(fileno) == 0) {
@@ -325,7 +345,7 @@ public void close() throws IOException {
325345
public static PrintStream wrapPrintStream(final PrintStream ps, int fileno) {
326346
PrintStream result = doWrapPrintStream(ps, fileno);
327347
if (result != ps) {
328-
if (!Boolean.getBoolean("jansi.no-optimize")) {
348+
if (!getBoolean(JANSI_NO_OPTIMIZE)) {
329349
result = optimize(ps, result);
330350
}
331351
}
@@ -335,14 +355,14 @@ public static PrintStream wrapPrintStream(final PrintStream ps, int fileno) {
335355
private static PrintStream doWrapPrintStream(final PrintStream ps, int fileno) {
336356
// If the jansi.passthrough property is set, then don't interpret
337357
// any of the ansi sequences.
338-
if (Boolean.getBoolean("jansi.passthrough")) {
358+
if (getBoolean(JANSI_PASSTHROUGH)) {
339359
jansiOutputType = JansiOutputType.PASSTHROUGH;
340360
return ps;
341361
}
342362

343363
// If the jansi.strip property is set, then we just strip the
344364
// the ansi escapes.
345-
if (Boolean.getBoolean("jansi.strip")) {
365+
if (getBoolean(JANSI_STRIP)) {
346366
jansiOutputType = JansiOutputType.STRIP_ANSI;
347367
return new AnsiPrintStream(ps);
348368
}
@@ -368,7 +388,7 @@ private static PrintStream doWrapPrintStream(final PrintStream ps, int fileno) {
368388
try {
369389
// If the jansi.force property is set, then we force to output
370390
// the ansi escapes for piping it into ansi color aware commands (e.g. less -r)
371-
boolean forceColored = Boolean.getBoolean("jansi.force");
391+
boolean forceColored = getBoolean(JANSI_FORCE);
372392
// If we can detect that stdout is not a tty.. then setup
373393
// to strip the ANSI sequences..
374394
if (!forceColored && isatty(fileno) == 0) {

jansi/src/main/java/org/fusesource/jansi/AnsiMain.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,18 +78,22 @@ public static void main(String... args) throws IOException {
7878

7979
System.out.println();
8080

81-
System.out.println("jansi.passthrough= " + Boolean.getBoolean("jansi.passthrough"));
82-
System.out.println("jansi.strip= " + Boolean.getBoolean("jansi.strip"));
83-
System.out.println("jansi.force= " + Boolean.getBoolean("jansi.force"));
84-
System.out.println(Ansi.DISABLE + "= " + Boolean.getBoolean(Ansi.DISABLE));
81+
System.out.println(AnsiConsole.JANSI_PASSTHROUGH + "= " + AnsiConsole.getBoolean(AnsiConsole.JANSI_PASSTHROUGH));
82+
System.out.println(AnsiConsole.JANSI_STRIP + "= " + AnsiConsole.getBoolean(AnsiConsole.JANSI_STRIP));
83+
System.out.println(AnsiConsole.JANSI_FORCE + "= " + AnsiConsole.getBoolean(AnsiConsole.JANSI_FORCE));
84+
System.out.println(AnsiConsole.JANSI_DO_WRAP + "= " + AnsiConsole.getBoolean(AnsiConsole.JANSI_DO_WRAP));
85+
System.out.println(AnsiConsole.JANSI_NO_OPTIMIZE + "= " + AnsiConsole.getBoolean(AnsiConsole.JANSI_NO_OPTIMIZE));
86+
System.out.println(Ansi.DISABLE + "= " + AnsiConsole.getBoolean(Ansi.DISABLE));
8587

8688
System.out.println();
8789

8890
System.out.println("IS_WINDOWS: " + AnsiConsole.IS_WINDOWS);
8991
if (AnsiConsole.IS_WINDOWS) {
90-
System.out.println("IS_CON_EMU_ANSI: " + AnsiConsole.IS_CON_EMU_ANSI);
92+
System.out.println("IS_CONEMU: " + AnsiConsole.IS_CONEMU);
9193
System.out.println("IS_CYGWIN: " + AnsiConsole.IS_CYGWIN);
92-
System.out.println("IS_MINGW_XTERM: " + AnsiConsole.IS_MINGW_XTERM);
94+
System.out.println("IS_MSYSTEM: " + AnsiConsole.IS_MSYSTEM);
95+
System.out.println("IS_CON_EMU_ANSI: " + AnsiConsole.IS_CON_EMU_ANSI + " (deprecated)");
96+
System.out.println("IS_MINGW_XTERM: " + AnsiConsole.IS_MINGW_XTERM + " (deprecated)");
9397
}
9498

9599
System.out.println();
@@ -106,8 +110,8 @@ public static void main(String... args) throws IOException {
106110
System.out.println(" - System.err: " + AnsiConsole.JANSI_STDERR_TYPE);
107111
System.out.println("modes description:");
108112
int n = 1;
109-
for(AnsiConsole.JansiOutputType type: AnsiConsole.JansiOutputType.values()) {
110-
System.out.println(n++ + ". " + type + ": " + type.getDescription());
113+
for (AnsiConsole.JansiOutputType type: AnsiConsole.JansiOutputType.values()) {
114+
System.out.println(" - " + n++ + ". " + type + ": " + type.getDescription());
111115
}
112116

113117
try {

0 commit comments

Comments
 (0)