Skip to content

Commit

Permalink
Merge pull request #40127 from Eng-Fouad/#40087
Browse files Browse the repository at this point in the history
Adapt new behavior of System.console() since JDK22
  • Loading branch information
gsmet authored Apr 22, 2024
2 parents c1108d0 + c881b5f commit 99d6c7b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ protected Boolean initialValue() {

public BasicConsole(boolean color, boolean inputSupport, PrintStream printStream, Console console) {
this(color, inputSupport, (s) -> {
if (console != null) {
if (TerminalUtils.isTerminal(console)) {
console.writer().print(s);
console.writer().flush();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public static boolean hasColorSupport() {
} else {
// on sane operating systems having a console is a good indicator
// you are attached to a TTY with colors.
return System.console() != null;
return TerminalUtils.isTerminal(System.console());
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.quarkus.dev.console;

import java.io.Console;
import java.util.logging.Level;
import java.util.logging.Logger;

public class TerminalUtils {

private static final Logger LOGGER = Logger.getLogger(TerminalUtils.class.getName());

public static boolean isTerminal(Console console) {
if (console == null) {
return false;
}

if (Runtime.version().feature() < 22) { // isTerminal was introduced in Java 22
return true;
}

try {
return (boolean) Console.class.getMethod("isTerminal").invoke(console);
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "Failed to invoke System.console().isTerminal() via Reflection API", e);
return false;
}
}
}

0 comments on commit 99d6c7b

Please sign in to comment.