Skip to content

Commit 86b970e

Browse files
committed
Move terminal test to properties
1 parent b68ac48 commit 86b970e

File tree

3 files changed

+15
-15
lines changed

3 files changed

+15
-15
lines changed

src/library/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.

src/repl-frontend/scala/tools/nsc/interpreter/shell/ShellConfig.scala

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,10 @@ import scala.sys.Prop._
2121

2222
import scala.tools.nsc.{GenericRunnerSettings, Settings}
2323
import scala.tools.nsc.Properties.{
24-
coloredOutputEnabled, envOrNone, javaVersion, javaVmName,
24+
coloredOutputEnabled, consoleIsTerminal, envOrNone, javaVersion, javaVmName,
2525
shellBannerString, shellInterruptedString, shellPromptString, shellWelcomeString,
2626
userHome, versionString, versionNumberString,
2727
}
28-
import scala.util.Properties.isJavaAtLeast
2928

3029
object ShellConfig {
3130
val EDITOR = envOrNone("EDITOR")
@@ -60,15 +59,7 @@ trait ShellConfig {
6059
def batchText: String
6160
def batchMode: Boolean
6261
def doCompletion: Boolean
63-
def haveInteractiveConsole: Boolean = System.console != null && consoleIsTerminal
64-
65-
// false if JDK 22 and the system console says !isTerminal
66-
def consoleIsTerminal: Boolean = {
67-
def isTerminal: Boolean =
68-
try classOf[java.io.Console].getMethod("isTerminal", null).invoke(System.console).asInstanceOf[Boolean]
69-
catch { case _: NoSuchMethodException => false }
70-
!isJavaAtLeast(22) || isTerminal
71-
}
62+
def haveInteractiveConsole: Boolean = consoleIsTerminal
7263

7364
// source compatibility, i.e., -Xsource
7465
def xsource: String
@@ -77,7 +68,7 @@ trait ShellConfig {
7768
private def int(name: String) = Prop[Int](name)
7869

7970
// This property is used in TypeDebugging. Let's recycle it.
80-
val colorOk = coloredOutputEnabled && haveInteractiveConsole
71+
val colorOk = coloredOutputEnabled
8172

8273
val historyFile = s"$userHome/.scala_history_jline3"
8374

test/files/neg/t6323a.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// scalac: -Vimplicits
1+
//> using options -Vimplicits
22
//
33
import scala.reflect.runtime.universe._
44
import scala.reflect.runtime.{currentMirror => m}

0 commit comments

Comments
 (0)