Skip to content

Commit

Permalink
Console color only if JDK 22 says isConsole
Browse files Browse the repository at this point in the history
  • Loading branch information
som-snytt committed Apr 11, 2024
1 parent 18b61a9 commit 2c78ad2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1009,7 +1009,7 @@ object ILoop {
def batchText: String = delegate.batchText
def batchMode: Boolean = delegate.batchMode
def doCompletion: Boolean = delegate.doCompletion
def haveInteractiveConsole: Boolean = delegate.haveInteractiveConsole
override def haveInteractiveConsole: Boolean = delegate.haveInteractiveConsole

def xsource: String = ""

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import scala.tools.nsc.Properties.{
shellBannerString, shellInterruptedString, shellPromptString, shellWelcomeString,
userHome, versionString, versionNumberString,
}
import scala.util.Properties.isJavaAtLeast

object ShellConfig {
val EDITOR = envOrNone("EDITOR")
Expand All @@ -38,7 +39,7 @@ object ShellConfig {
val batchText: String = if (settings.execute.isSetByUser) settings.execute.value else ""
val batchMode: Boolean = batchText.nonEmpty
val doCompletion: Boolean = !(settings.noCompletion.value || batchMode)
val haveInteractiveConsole: Boolean = !settings.Xnojline.value
override val haveInteractiveConsole: Boolean = super.haveInteractiveConsole && !settings.Xnojline.value
def xsource: String = if (settings.isScala3: @nowarn) settings.source.value.versionString else ""
}
case _ => new ShellConfig {
Expand All @@ -47,7 +48,7 @@ object ShellConfig {
val batchText: String = ""
val batchMode: Boolean = false
val doCompletion: Boolean = !settings.noCompletion.value
val haveInteractiveConsole: Boolean = !settings.Xnojline.value
override val haveInteractiveConsole: Boolean = super.haveInteractiveConsole && !settings.Xnojline.value
def xsource: String = if (settings.isScala3: @nowarn) settings.source.value.versionString else ""
}
}
Expand All @@ -59,7 +60,15 @@ trait ShellConfig {
def batchText: String
def batchMode: Boolean
def doCompletion: Boolean
def haveInteractiveConsole: Boolean
def haveInteractiveConsole: Boolean = System.console != null && consoleIsTerminal

// false if JDK 22 and the system console says !isTerminal
def consoleIsTerminal: Boolean = {
def isTerminal: Boolean =
try classOf[java.io.Console].getMethod("isTerminal", null).invoke(System.console).asInstanceOf[Boolean]
catch { case _: NoSuchMethodException => false }
!isJavaAtLeast(22) || isTerminal
}

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

// This property is used in TypeDebugging. Let's recycle it.
val colorOk = coloredOutputEnabled
val colorOk = coloredOutputEnabled && haveInteractiveConsole

val historyFile = s"$userHome/.scala_history_jline3"

Expand Down

0 comments on commit 2c78ad2

Please sign in to comment.