From 3212b4a3b5bf24ac412c3275270c512585b08ca9 Mon Sep 17 00:00:00 2001 From: Liam Miller-Cushon Date: Thu, 10 Aug 2023 19:57:11 -0700 Subject: [PATCH] Update logic for detecting if the output stream is connected to a terminal https://github.com/remkop/picocli/issues/2083 --- src/main/java/picocli/CommandLine.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/main/java/picocli/CommandLine.java b/src/main/java/picocli/CommandLine.java index 3b71030e0..ac17e669b 100644 --- a/src/main/java/picocli/CommandLine.java +++ b/src/main/java/picocli/CommandLine.java @@ -17840,7 +17840,18 @@ static final boolean forceEnabled() { return System.getenv("CLICOLOR_FORCE") != && !"0".equals(System.getenv("CLICOLOR_FORCE"));} /** http://stackoverflow.com/questions/1403772/how-can-i-check-if-a-java-programs-input-output-streams-are-connected-to-a-term */ static boolean calcTTY() { - try { return System.class.getDeclaredMethod("console").invoke(null) != null; } + try { + Object console = System.class.getDeclaredMethod("console").invoke(null); + if (console == null) { + return false; + } + try { + Method isTerminal = Console.class.getDeclaredMethod("isTerminal"); + return (boolean) isTerminal.invoke(console); + } catch (NoSuchMethodException e) { + return true; + } + } catch (Throwable reflectionFailed) { return true; } } /** Cygwin and MSYS use pseudo-tty and console is always null... */