Skip to content

Commit 6845f54

Browse files
authored
Merge pull request scala/scala#10753 from som-snytt/test/fixup
Move terminal test to properties
2 parents 6115e09 + a1d9238 commit 6845f54

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

library/src/scala/util/Properties.scala

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,17 @@ private[scala] trait PropertiesTrait {
142142
private[scala] lazy val isAvian = javaVmName.contains("Avian")
143143

144144
private[scala] def coloredOutputEnabled: Boolean = propOrElse("scala.color", "auto") match {
145-
case "auto" => System.console() != null && !isWin
146-
case s => s == "" || "true".equalsIgnoreCase(s)
145+
case "auto" => !isWin && consoleIsTerminal
146+
case s => "" == s || "true".equalsIgnoreCase(s)
147+
}
148+
149+
/** System.console.isTerminal, or just check for null console on JDK < 22 */
150+
private[scala] lazy val consoleIsTerminal: Boolean = {
151+
val console = System.console
152+
def isTerminal: Boolean =
153+
try classOf[java.io.Console].getMethod("isTerminal", null).invoke(console).asInstanceOf[Boolean]
154+
catch { case _: NoSuchMethodException => false }
155+
console != null && (!isJavaAtLeast("22") || isTerminal)
147156
}
148157

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

0 commit comments

Comments
 (0)