Skip to content

Commit c571b14

Browse files
authoredMar 8, 2023
Refine color support in various environments, fixes #814 (#829)
1 parent f3fa703 commit c571b14

File tree

1 file changed

+33
-17
lines changed

1 file changed

+33
-17
lines changed
 

‎terminal/src/main/java/org/jline/terminal/TerminalBuilder.java

+33-17
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.nio.charset.Charset;
1919
import java.nio.charset.StandardCharsets;
2020
import java.nio.charset.UnsupportedCharsetException;
21+
import java.nio.file.Paths;
2122
import java.util.ArrayList;
2223
import java.util.Arrays;
2324
import java.util.Comparator;
@@ -491,30 +492,45 @@ private Terminal doBuild() throws IOException {
491492
// forced colored dumb terminal
492493
Boolean color = this.color;
493494
if (color == null) {
494-
color = getBoolean(PROP_DUMB_COLOR, false);
495+
color = getBoolean(PROP_DUMB_COLOR, null);
496+
}
497+
if (dumb == null) {
495498
// detect emacs using the env variable
496-
if (!color) {
499+
if (color == null) {
497500
String emacs = System.getenv("INSIDE_EMACS");
498-
color = emacs != null && emacs.contains("comint");
501+
if (emacs != null && emacs.contains("comint")) {
502+
color = true;
503+
}
499504
}
500505
// detect Intellij Idea
501-
if (!color) {
502-
String command = getParentProcessCommand();
503-
color = command != null && command.contains("idea");
504-
}
505-
if (!color) {
506-
color = system.get(TerminalProvider.Stream.Output) && System.getenv("TERM") != null;
507-
}
508-
if (!color && dumb == null) {
509-
if (Log.isDebugEnabled()) {
510-
Log.warn("input is tty: {}", system.get(TerminalProvider.Stream.Input));
511-
Log.warn("output is tty: {}", system.get(TerminalProvider.Stream.Output));
512-
Log.warn("error is tty: {}", system.get(TerminalProvider.Stream.Error));
513-
Log.warn("Creating a dumb terminal", exception);
506+
if (color == null) {
507+
// using the env variable on windows
508+
String ideHome = System.getenv("IDE_HOME");
509+
if (ideHome != null) {
510+
color = true;
514511
} else {
515-
Log.warn("Unable to create a system terminal, creating a dumb terminal (enable debug logging for more information)");
512+
// using the parent process command on unix/mac
513+
String command = getParentProcessCommand();
514+
if (command != null && command.endsWith("/idea")) {
515+
color = true;
516+
}
516517
}
517518
}
519+
if (color == null) {
520+
color = console != null && System.getenv("TERM") != null;
521+
}
522+
if (Log.isDebugEnabled()) {
523+
Log.warn("input is tty: {}", system.get(TerminalProvider.Stream.Input));
524+
Log.warn("output is tty: {}", system.get(TerminalProvider.Stream.Output));
525+
Log.warn("error is tty: {}", system.get(TerminalProvider.Stream.Error));
526+
Log.warn("Creating a dumb terminal", exception);
527+
} else {
528+
Log.warn("Unable to create a system terminal, creating a dumb terminal (enable debug logging for more information)");
529+
}
530+
} else {
531+
if (color == null) {
532+
color = false;
533+
}
518534
}
519535
terminal = new DumbTerminal(name, color ? Terminal.TYPE_DUMB_COLOR : Terminal.TYPE_DUMB,
520536
new FileInputStream(FileDescriptor.in),

0 commit comments

Comments
 (0)
Please sign in to comment.