Skip to content

Commit

Permalink
Merge pull request #384 from outr/node-js-colors
Browse files Browse the repository at this point in the history
Added ANSI and ASCII support when running Scala.js under Node.js
  • Loading branch information
darkfrog26 authored Jan 22, 2023
2 parents 9956ec0 + 03d8a8d commit 04859f7
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 27 deletions.
19 changes: 16 additions & 3 deletions core/js/src/main/scala/scribe/Platform.scala
Original file line number Diff line number Diff line change
@@ -1,23 +1,36 @@
package scribe

import scribe.output.format.{OutputFormat, RichBrowserOutputFormat}
import scribe.output.format.{ANSIOutputFormat, ASCIIOutputFormat, OutputFormat, RichBrowserOutputFormat}
import scribe.writer.{BrowserConsoleWriter, Writer}

import scala.concurrent.ExecutionContext
import scala.scalajs.js
import scala.scalajs.js.Dictionary
import scala.util.Try

object Platform extends PlatformImplementation {
def isJVM: Boolean = false
def isJS: Boolean = true
def isNative: Boolean = false

// $COVERAGE-OFF$
lazy val isNodeJS: Boolean = Try(js.Dynamic.global.process.release.name.asInstanceOf[String]).toOption.contains("node")

def init(): Unit = {}

// $COVERAGE-OFF$
def console: JavaScriptConsole = js.Dynamic.global.console.asInstanceOf[JavaScriptConsole]

private def processEnv: Dictionary[Any] = Try(js.Dynamic.global.process.env.asInstanceOf[js.Dictionary[Any]])
.getOrElse(js.Dictionary.empty)
// $COVERAGE-ON$

def outputFormat(): OutputFormat = RichBrowserOutputFormat
override def env(key: String): Option[String] = processEnv.get(key).map(_.toString)

override def outputFormat(): OutputFormat = if (isNodeJS) {
super.outputFormat()
} else {
RichBrowserOutputFormat
}

override def consoleWriter: Writer = BrowserConsoleWriter

Expand Down
12 changes: 0 additions & 12 deletions core/jvm/src/main/scala/scribe/Platform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,6 @@ object Platform extends PlatformImplementation {
Moduload.load()
}

lazy val supportsANSI: Boolean = sys.env.contains("TERM")

def outputFormat(): OutputFormat = sys.env.get("SCRIBE_OUTPUT_FORMAT").map(_.toUpperCase) match {
case Some("ANSI") => ANSIOutputFormat
case Some("ASCII") => ASCIIOutputFormat
case None if supportsANSI => ANSIOutputFormat
case None => ASCIIOutputFormat
case f =>
System.err.println(s"Unexpected output format specified in SCRIBE_OUTPUT_FORMAT: $f, using ASCII")
ASCIIOutputFormat
}

override def consoleWriter: Writer = SystemWriter

override def columns: Int = columnsOverride.getOrElse {
Expand Down
12 changes: 0 additions & 12 deletions core/native/src/main/scala/scribe/Platform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,6 @@ object Platform extends PlatformImplementation {

def init(): Unit = {}

lazy val supportsANSI: Boolean = sys.env.contains("TERM")

def outputFormat(): OutputFormat = sys.env.get("SCRIBE_OUTPUT_FORMAT").map(_.toUpperCase) match {
case Some("ANSI") => ANSIOutputFormat
case Some("ASCII") => ASCIIOutputFormat
case None if supportsANSI => ANSIOutputFormat
case None => ASCIIOutputFormat
case f =>
System.err.println(s"Unexpected output format specified in SCRIBE_OUTPUT_FORMAT: $f, using ASCII")
ASCIIOutputFormat
}

override def consoleWriter: Writer = SystemWriter

override val columns: Int = 120 + columnsAdjust
Expand Down
15 changes: 15 additions & 0 deletions core/shared/src/main/scala/scribe/PlatformImplementation.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package scribe

import scribe.output.format.{ANSIOutputFormat, ASCIIOutputFormat, OutputFormat}
import scribe.writer.Writer

import scala.concurrent.ExecutionContext
Expand All @@ -17,4 +18,18 @@ trait PlatformImplementation {
def rows: Int

def executionContext: ExecutionContext

def env(key: String): Option[String] = sys.env.get(key)

lazy val supportsANSI: Boolean = env("TERM").nonEmpty

def outputFormat(): OutputFormat = env("SCRIBE_OUTPUT_FORMAT").map(_.toUpperCase) match {
case Some("ANSI") => ANSIOutputFormat
case Some("ASCII") => ASCIIOutputFormat
case None if supportsANSI => ANSIOutputFormat
case None => ASCIIOutputFormat
case f =>
System.err.println(s"Unexpected output format specified in SCRIBE_OUTPUT_FORMAT: $f, using ASCII")
ASCIIOutputFormat
}
}

0 comments on commit 04859f7

Please sign in to comment.