7
7
*/
8
8
package org .seedstack .seed .core .internal .init ;
9
9
10
+ import org .fusesource .jansi .AnsiConsole ;
10
11
import org .fusesource .jansi .AnsiOutputStream ;
11
- import org .fusesource .jansi .WindowsAnsiOutputStream ;
12
12
13
13
import java .io .FilterOutputStream ;
14
14
import java .io .IOException ;
15
15
import java .io .OutputStream ;
16
16
import java .io .PrintStream ;
17
17
18
- import static org .fusesource .jansi .internal .CLibrary .STDOUT_FILENO ;
19
- import static org .fusesource .jansi .internal .CLibrary .isatty ;
20
-
21
18
public class ConsoleManager {
22
- private volatile boolean colorSupported ;
23
19
private PrintStream savedOut ;
24
20
private PrintStream savedErr ;
25
21
26
22
public synchronized void install () {
27
23
OutputStream out = wrapOutputStream (System .out );
28
24
OutputStream err = wrapOutputStream (System .err );
29
- colorSupported = isColorSupported (out ) && isColorSupported (err );
30
25
savedOut = System .out ;
31
26
System .setOut (new PrintStream (out ));
32
27
savedErr = System .err ;
@@ -36,45 +31,50 @@ public synchronized void install() {
36
31
public synchronized void uninstall () {
37
32
System .setOut (savedOut );
38
33
System .setErr (savedErr );
39
- colorSupported = false ;
40
34
}
41
35
42
36
public boolean isColorSupported () {
43
- return colorSupported ;
44
- }
45
-
46
- private boolean isColorSupported (OutputStream outputStream ) {
47
- return outputStream instanceof ColorOutputStream || outputStream instanceof WindowsAnsiOutputStream ;
37
+ // we cannot know if color is supported
38
+ return false ;
48
39
}
49
40
50
41
private OutputStream wrapOutputStream (final OutputStream stream ) {
51
42
try {
52
- if (isIntelliJ () || isTTY () || isCygwin ()) {
43
+ if (Boolean .getBoolean ("jansi.passthrough" )) {
44
+ // honor jansi passthrough
45
+ return stream ;
46
+ } else if (Boolean .getBoolean ("jansi.strip" )) {
47
+ // honor jansi strip
48
+ return basicOutput (stream );
49
+ } else if (isXtermColor ()) {
50
+ // enable color in recognized XTERM color modes
51
+ return ansiOutput (stream );
52
+ } else if (isIntelliJ ()) {
53
+ // enable color under Intellij
53
54
return ansiOutput (stream );
54
- } else if (isWindows ()) {
55
- return windowsOutput (stream );
56
55
} else {
57
- return basicOutput (stream );
56
+ // let Jansi handle other detection
57
+ return AnsiConsole .wrapOutputStream (stream );
58
58
}
59
59
} catch (Throwable e ) {
60
+ // If any error occurs, strip ANSI codes
60
61
return basicOutput (stream );
61
62
}
62
63
}
63
64
64
- private FilterOutputStream ansiOutput (OutputStream stream ) {
65
- return new ColorOutputStream (stream );
65
+ private FilterOutputStream basicOutput (OutputStream stream ) {
66
+ return new AnsiOutputStream (stream );
66
67
}
67
68
68
- private FilterOutputStream windowsOutput (OutputStream stream ) {
69
- try {
70
- return new WindowsAnsiOutputStream (stream );
71
- } catch (Exception e ) {
72
- return basicOutput (stream );
73
- }
69
+ private FilterOutputStream ansiOutput (OutputStream stream ) {
70
+ return new ColorOutputStream (stream );
74
71
}
75
72
76
- private FilterOutputStream basicOutput (OutputStream stream ) {
77
- return new AnsiOutputStream (stream );
73
+ private boolean isXtermColor () {
74
+ String term = System .getenv ("TERM" );
75
+ return "xterm-256color" .equals (term ) ||
76
+ "xterm-color" .equals (term ) ||
77
+ "xterm" .equals (term );
78
78
}
79
79
80
80
private boolean isIntelliJ () {
@@ -86,22 +86,6 @@ private boolean isIntelliJ() {
86
86
}
87
87
}
88
88
89
- private boolean isWindows () {
90
- return System .getProperty ("os.name" ).startsWith ("Windows" );
91
- }
92
-
93
- private boolean isCygwin () {
94
- return isWindows () && System .getenv ("TERM" ) != null ;
95
- }
96
-
97
- private boolean isTTY () {
98
- try {
99
- return isatty (STDOUT_FILENO ) != 0 ;
100
- } catch (Exception e ) {
101
- return false ;
102
- }
103
- }
104
-
105
89
private static class ColorOutputStream extends FilterOutputStream {
106
90
private ColorOutputStream (OutputStream stream ) {
107
91
super (stream );
0 commit comments