All Downloads are FREE. Search and download functionalities are using the official Maven repository.

zio.test.sbt.package.scala Maven / Gradle / Ivy

There is a newer version: 2.1.11
Show newest version
package zio.test

import zio.{UIO, URIO, ZIO}

import scala.annotation.tailrec

package object sbt {

  type SendSummary = URIO[Summary, Unit]

  object SendSummary {
    def fromSend(send: Summary => Unit): SendSummary =
      ZIO.serviceWithZIO(summary => ZIO.succeed(send(summary)))

    def fromSendZIO(send: Summary => UIO[Unit]): SendSummary =
      ZIO.serviceWithZIO(send)

    def noop: SendSummary =
      ZIO.unit
  }

  /**
   * Inserts the ANSI escape code for the current color at the beginning of each
   * line of the specified string so the string will be displayed with the
   * correct color by the `SBTTestLogger`.
   */
  private[sbt] def colored(s: String): String = {
    @tailrec
    def loop(s: String, i: Int, color: Option[String]): String =
      if (i >= s.length) s
      else {
        val s1 = s.slice(i, i + 5)
        val isColor = s1 == Console.BLUE ||
          s1 == Console.CYAN ||
          s1 == Console.GREEN ||
          s1 == Console.RED ||
          s1 == Console.YELLOW
        if (isColor)
          loop(s, i + 5, Some(s1))
        else if (s.slice(i, i + 4) == Console.RESET)
          loop(s, i + 4, None)
        else if (s.slice(i, i + 1) == "\n" && color.isDefined)
          loop(s.patch(i + 1, color.get, 0), i + 6, color)
        else loop(s, i + 1, color)

      }
    loop(s, 0, None)
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy