Skip to content

Commit b68ac48

Browse files
authored
Merge pull request #10751 from som-snytt/sd/867-jdk22-tests
Console color only if JDK 22 says isConsole
2 parents 770e6a0 + 2c78ad2 commit b68ac48

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1009,7 +1009,7 @@ object ILoop {
10091009
def batchText: String = delegate.batchText
10101010
def batchMode: Boolean = delegate.batchMode
10111011
def doCompletion: Boolean = delegate.doCompletion
1012-
def haveInteractiveConsole: Boolean = delegate.haveInteractiveConsole
1012+
override def haveInteractiveConsole: Boolean = delegate.haveInteractiveConsole
10131013

10141014
def xsource: String = ""
10151015

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

+13-4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import scala.tools.nsc.Properties.{
2525
shellBannerString, shellInterruptedString, shellPromptString, shellWelcomeString,
2626
userHome, versionString, versionNumberString,
2727
}
28+
import scala.util.Properties.isJavaAtLeast
2829

2930
object ShellConfig {
3031
val EDITOR = envOrNone("EDITOR")
@@ -38,7 +39,7 @@ object ShellConfig {
3839
val batchText: String = if (settings.execute.isSetByUser) settings.execute.value else ""
3940
val batchMode: Boolean = batchText.nonEmpty
4041
val doCompletion: Boolean = !(settings.noCompletion.value || batchMode)
41-
val haveInteractiveConsole: Boolean = !settings.Xnojline.value
42+
override val haveInteractiveConsole: Boolean = super.haveInteractiveConsole && !settings.Xnojline.value
4243
def xsource: String = if (settings.isScala3: @nowarn) settings.source.value.versionString else ""
4344
}
4445
case _ => new ShellConfig {
@@ -47,7 +48,7 @@ object ShellConfig {
4748
val batchText: String = ""
4849
val batchMode: Boolean = false
4950
val doCompletion: Boolean = !settings.noCompletion.value
50-
val haveInteractiveConsole: Boolean = !settings.Xnojline.value
51+
override val haveInteractiveConsole: Boolean = super.haveInteractiveConsole && !settings.Xnojline.value
5152
def xsource: String = if (settings.isScala3: @nowarn) settings.source.value.versionString else ""
5253
}
5354
}
@@ -59,7 +60,15 @@ trait ShellConfig {
5960
def batchText: String
6061
def batchMode: Boolean
6162
def doCompletion: Boolean
62-
def haveInteractiveConsole: 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+
}
6372

6473
// source compatibility, i.e., -Xsource
6574
def xsource: String
@@ -68,7 +77,7 @@ trait ShellConfig {
6877
private def int(name: String) = Prop[Int](name)
6978

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

7382
val historyFile = s"$userHome/.scala_history_jline3"
7483

0 commit comments

Comments
 (0)