Skip to content

Commit

Permalink
Enable colored output for Scala 2
Browse files Browse the repository at this point in the history
  • Loading branch information
lolgab committed Jan 6, 2024
1 parent bd7137e commit 524dae6
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package example

trait Show[A]

object Show {
implicit def option[A](implicit s: Show[A]): Show[Option[A]] = ???
}

object Example {
def main(args: Array[String]): Unit =
println(implicitly[Show[Option[String]]])
}
31 changes: 28 additions & 3 deletions scalalib/test/src/mill/scalalib/HelloWorldTests.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package mill.scalalib

import java.io.ByteArrayOutputStream
import java.io.{ByteArrayOutputStream, PrintStream}
import java.util.jar.JarFile
import scala.jdk.CollectionConverters._
import scala.util.{Properties, Using}
Expand Down Expand Up @@ -283,6 +283,16 @@ object HelloWorldTests extends TestSuite {
}
}

object HelloWorldColorOutput extends HelloBase {
object core extends ScalaModule {
def scalaVersion = scala213Version

override def scalacOptions = super.scalacOptions() ++ Seq(
"-Vimplicits"
)
}
}

object HelloScalacheck extends HelloBase {
object foo extends ScalaModule {
def scalaVersion = scala212Version
Expand Down Expand Up @@ -420,9 +430,10 @@ object HelloWorldTests extends TestSuite {
m: TestUtil.BaseModule,
resourcePath: os.Path = resourcePath,
env: Map[String, String] = Evaluator.defaultEnv,
debug: Boolean = false
debug: Boolean = false,
errStream: PrintStream = System.err
)(t: TestEvaluator => T)(implicit tp: TestPath): T = {
val eval = new TestEvaluator(m, env = env, debugEnabled = debug)
val eval = new TestEvaluator(m, env = env, debugEnabled = debug, errStream = errStream)
os.remove.all(m.millSourcePath)
os.remove.all(eval.outPath)
os.makeDir.all(m.millSourcePath / os.up)
Expand Down Expand Up @@ -1189,6 +1200,20 @@ object HelloWorldTests extends TestSuite {
assert(evalCount > 0)
}
}
"color-output" - {
val errStream = new ByteArrayOutputStream()
workspaceTest(
HelloWorldColorOutput,
os.pwd / "scalalib" / "test" / "resources" / "hello-world-color-output",
errStream = new PrintStream(errStream, true)
) { eval =>

val Left(Result.Failure("Compilation failed", _)) = eval.apply(HelloWorldColorOutput.core.compile)
val output = errStream.toString
assert(output.contains(s"${Console.RED}!${Console.RESET}${Console.BLUE}I"))
assert(output.contains(s"${Console.GREEN}example.Show[scala.Option[java.lang.String]]${Console.RESET}"))
}
}

"scalacheck" - workspaceTest(
HelloScalacheck,
Expand Down
4 changes: 4 additions & 0 deletions scalalib/worker/src/mill/scalalib/worker/ZincWorkerImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,10 @@ class ZincWorkerImpl(
stampReader = Stamps.timeWrapBinaryStamps(converter)
)

val scalaColorProp = "scala.color"
val previousScalaColor = sys.props.get(scalaColorProp).getOrElse("auto")
try {
sys.props(scalaColorProp) = if (ctx.log.colored) "true" else "false"
val newResult = ic.compile(
in = inputs,
logger = logger
Expand All @@ -557,6 +560,7 @@ class ZincWorkerImpl(
} finally {
reporter.foreach(r => sources.foreach(r.fileVisited(_)))
reporter.foreach(_.finish())
sys.props(scalaColorProp) = previousScalaColor
}
}

Expand Down

0 comments on commit 524dae6

Please sign in to comment.