Skip to content

Commit 0cb893f

Browse files
committed
[backport] Console.isTerminal on JDK 22
1 parent c79ff64 commit 0cb893f

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

library/src/scala/util/Properties.scala

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,17 @@ private[scala] trait PropertiesTrait {
158158
private[scala] def isAvian = javaVmName contains "Avian"
159159

160160
private[scala] def coloredOutputEnabled: Boolean = propOrElse("scala.color", "auto") match {
161-
case "auto" => System.console() != null && !isWin
162-
case a if a.toLowerCase() == "true" => true
163-
case _ => false
161+
case "auto" => !isWin && consoleIsTerminal
162+
case s => "" == s || "true".equalsIgnoreCase(s)
163+
}
164+
165+
/** System.console.isTerminal, or just check for null console on JDK < 22 */
166+
private[scala] lazy val consoleIsTerminal: Boolean = {
167+
val console = System.console
168+
def isTerminal: Boolean =
169+
try classOf[java.io.Console].getMethod("isTerminal", null).invoke(console).asInstanceOf[Boolean]
170+
catch { case _: NoSuchMethodException => false }
171+
console != null && (!isJavaAtLeast("22") || isTerminal)
164172
}
165173

166174
// This is looking for javac, tools.jar, etc.

0 commit comments

Comments
 (0)