This commit is contained in:
Alexandre Archambault 2016-03-13 22:57:23 +01:00
parent 301aab7f5c
commit 32a4ccc072
1 changed files with 17 additions and 16 deletions

View File

@ -22,21 +22,21 @@ object Terminal {
} else } else
None None
class Ansi(val output: Writer) extends AnyVal { implicit class Ansi(val output: Writer) extends AnyVal {
private def control(n: Int, c: Char) = output.write(s"\033[" + n + c) private def control(n: Int, c: Char) = output.write(s"\033[" + n + c)
/** /**
* Move up `n` squares * Move up `n` squares
*/ */
def up(n: Int): Unit = if (n == 0) "" else control(n, 'A') def up(n: Int): Unit = if (n > 0) control(n, 'A')
/** /**
* Move down `n` squares * Move down `n` squares
*/ */
def down(n: Int): Unit = if (n == 0) "" else control(n, 'B') def down(n: Int): Unit = if (n > 0) control(n, 'B')
/** /**
* Move left `n` squares * Move left `n` squares
*/ */
def left(n: Int): Unit = if (n == 0) "" else control(n, 'D') def left(n: Int): Unit = if (n > 0) control(n, 'D')
/** /**
* Clear the current line * Clear the current line
@ -55,7 +55,8 @@ class TermDisplay(
var fallbackMode: Boolean = sys.env.get("COURSIER_NO_TERM").nonEmpty var fallbackMode: Boolean = sys.env.get("COURSIER_NO_TERM").nonEmpty
) extends Cache.Logger { ) extends Cache.Logger {
private val ansi = new Terminal.Ansi(out) import Terminal.Ansi
private var width = 80 private var width = 80
private val refreshInterval = 1000 / 60 private val refreshInterval = 1000 / 60
private val fallbackRefreshInterval = 1000 private val fallbackRefreshInterval = 1000
@ -110,7 +111,7 @@ class TermDisplay(
def truncatedPrintln(s: String): Unit = { def truncatedPrintln(s: String): Unit = {
ansi.clearLine(2) out.clearLine(2)
if (s.length <= width) if (s.length <= width)
out.write(s + "\n") out.write(s + "\n")
@ -150,7 +151,7 @@ class TermDisplay(
assert(info != null, s"Incoherent state ($url)") assert(info != null, s"Incoherent state ($url)")
truncatedPrintln(url) truncatedPrintln(url)
ansi.clearLine(2) out.clearLine(2)
out.write(s" ${info.display()}\n") out.write(s" ${info.display()}\n")
} }
@ -158,18 +159,18 @@ class TermDisplay(
if (displayedCount < lineCount) { if (displayedCount < lineCount) {
for (_ <- 1 to 2; _ <- displayedCount until lineCount) { for (_ <- 1 to 2; _ <- displayedCount until lineCount) {
ansi.clearLine(2) out.clearLine(2)
ansi.down(1) out.down(1)
} }
for (_ <- displayedCount until lineCount) for (_ <- displayedCount until lineCount)
ansi.up(2) out.up(2)
} }
for (_ <- downloads0.indices) for (_ <- downloads0.indices)
ansi.up(2) out.up(2)
ansi.left(10000) out.left(10000)
out.flush() out.flush()
Thread.sleep(refreshInterval) Thread.sleep(refreshInterval)
@ -221,7 +222,7 @@ class TermDisplay(
Terminal.consoleDim("cols") match { Terminal.consoleDim("cols") match {
case Some(cols) => case Some(cols) =>
width = cols width = cols
ansi.clearLine(2) out.clearLine(2)
case None => case None =>
fallbackMode = true fallbackMode = true
} }
@ -231,11 +232,11 @@ class TermDisplay(
def stop(): Unit = { def stop(): Unit = {
for (_ <- 1 to 2; _ <- 0 until currentHeight) { for (_ <- 1 to 2; _ <- 0 until currentHeight) {
ansi.clearLine(2) out.clearLine(2)
ansi.down(1) out.down(1)
} }
for (_ <- 0 until currentHeight) { for (_ <- 0 until currentHeight) {
ansi.up(2) out.up(2)
} }
q.put(Left(())) q.put(Left(()))
lock.synchronized(()) lock.synchronized(())