Skip to content

Commit

Permalink
ansi color log
Browse files Browse the repository at this point in the history
  • Loading branch information
reminia committed Dec 8, 2023
1 parent 196f0db commit a992875
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions src/main/scala/sbtcurl/CurlPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package sbtcurl

import sbt._
import sbt.Keys._
import sbtcurl.Curl.AnsiLogger

import scala.io.Source
import scala.sys.process._
Expand All @@ -21,7 +22,7 @@ object CurlPlugin extends AutoPlugin {
lazy val curlSettings: Seq[Def.Setting[_]] = Seq(
curl := {
val input = Def.spaceDelimited().parsed.mkString(" ")
implicit val log: Logger = streams.value.log
implicit val log: AnsiLogger = streams.value.log
log.info(Curl(input))
},
)
Expand All @@ -42,11 +43,11 @@ object CurlPlugin extends AutoPlugin {
},
curlTest := {
val someFile = curlTestScript.value
implicit val log: Logger = streams.value.log
implicit val log: AnsiLogger = streams.value.log
if (someFile.isDefined) {
Curl(someFile.get).foreach(println)
Curl(someFile.get).foreach(log.info)
} else {
log.error("No any curl script file found!")
log.colorError("No any curl script file found!")
}
}
)
Expand All @@ -58,16 +59,32 @@ object CurlPlugin extends AutoPlugin {
}

object Curl {
def apply(cmd: String)(implicit logger: Logger): String = {
var curl = cmd
if(!cmd.startsWith("curl")) {
curl = s"curl $cmd"
private val ANSI_RESET = "\u001B[0m"

private val ANSI_GREEN = "\u001B[32m"

private val ANSI_RED = "\u001B[31m"

implicit class AnsiLogger(log: Logger) {
def colorInfo(msg: => String, ansi: String = ANSI_GREEN): Unit =
log.info(s"${ansi}$msg${ANSI_RESET}")

def colorError(msg: => String, ansi: String = ANSI_RED): Unit =
log.error(s"${ansi}$msg${ANSI_RESET}")

def info(msg: String): Unit = log.info(msg)
}

def apply(cmd: String)(implicit logger: AnsiLogger): String = {
val curl = cmd match {
case s if s.startsWith("curl") => cmd
case _ => s"curl $cmd"
}
logger.info(curl)
logger.colorInfo(curl)
curl.!!
}

def apply(file: File)(implicit logger: Logger): Seq[String] = {
def apply(file: File)(implicit logger: AnsiLogger): Seq[String] = {
val source = Source.fromFile(file)
try {
val (list, _) = source
Expand Down

0 comments on commit a992875

Please sign in to comment.