implement sbt.progress

This implements a logger that grows upward, instead towards bottom.
This commit is contained in:
Eugene Yokota 2018-09-30 21:43:39 -04:00
parent 53c9b84858
commit 9bb244314d
3 changed files with 41 additions and 7 deletions

View File

@ -126,6 +126,7 @@ lazy val utilLogging = (project in internalPath / "util-logging")
// Private final class constructors changed
exclude[DirectMissingMethodProblem]("sbt.util.InterfaceUtil#ConcretePosition.this"),
exclude[DirectMissingMethodProblem]("sbt.util.InterfaceUtil#ConcreteProblem.this"),
exclude[ReversedMissingMethodProblem]("sbt.internal.util.ConsoleOut.flush"),
),
)
.configure(addSbtIO)

View File

@ -133,6 +133,21 @@ object ConsoleAppender {
}
}
/**
* Indicates whether the super shell is enabled.
*/
lazy val showProgress: Boolean =
formatEnabledInEnv && sys.props
.get("sbt.progress")
.flatMap({ s =>
parseLogOption(s) match {
case LogOption.Always => Some(true)
case LogOption.Never => Some(false)
case _ => None
}
})
.getOrElse(true)
private[sbt] def parseLogOption(s: String): LogOption =
s.toLowerCase match {
case "always" => LogOption.Always
@ -443,11 +458,17 @@ class ConsoleAppender private[ConsoleAppender] (
appendLog(SUCCESS_LABEL_COLOR, Level.SuccessLabel, SUCCESS_MESSAGE_COLOR, message)
}
private final val ScrollUp = "\u001B[S"
private final val DeleteLine = "\u001B[2K"
private final val CursorLeft1000 = "\u001B[1000D"
private def write(msg: String): Unit = {
val cleanedMsg =
if (!useFormat || !ansiCodesSupported) EscHelpers.removeEscapeSequences(msg)
else msg
out.println(cleanedMsg)
if (!useFormat || !ansiCodesSupported) out.println(EscHelpers.removeEscapeSequences(msg))
else {
if (ConsoleAppender.showProgress) {
out.print(s"$ScrollUp$DeleteLine$msg${CursorLeft1000}")
out.flush()
} else out.println(msg)
}
}
private def appendMessage(level: Level.Value, msg: Message): Unit =

View File

@ -7,6 +7,7 @@ sealed trait ConsoleOut {
def print(s: String): Unit
def println(s: String): Unit
def println(): Unit
def flush(): Unit
}
object ConsoleOut {
@ -39,6 +40,14 @@ object ConsoleOut {
last = Some(s)
current.setLength(0)
}
def flush(): Unit = synchronized {
val s = current.toString
if (ConsoleAppender.formatEnabledInEnv && last.exists(lmsg => f(s, lmsg)))
lockObject.print(OverwriteLine)
lockObject.print(s)
last = Some(s)
current.setLength(0)
}
}
def printStreamOut(out: PrintStream): ConsoleOut = new ConsoleOut {
@ -46,17 +55,20 @@ object ConsoleOut {
def print(s: String) = out.print(s)
def println(s: String) = out.println(s)
def println() = out.println()
def flush() = out.flush()
}
def printWriterOut(out: PrintWriter): ConsoleOut = new ConsoleOut {
val lockObject = out
def print(s: String) = out.print(s)
def println(s: String) = { out.println(s); out.flush() }
def println() = { out.println(); out.flush() }
def println(s: String) = { out.println(s); flush() }
def println() = { out.println(); flush() }
def flush() = { out.flush() }
}
def bufferedWriterOut(out: BufferedWriter): ConsoleOut = new ConsoleOut {
val lockObject = out
def print(s: String) = out.write(s)
def println(s: String) = { out.write(s); println() }
def println() = { out.newLine(); out.flush() }
def println() = { out.newLine(); flush() }
def flush() = { out.flush() }
}
}