Merge pull request #186 from alexarchambault/topic/no-ammonite-terminal

Remove ammonite-terminal dependency
This commit is contained in:
Alexandre Archambault 2016-02-29 00:12:36 +01:00
commit 07eafc8d86
2 changed files with 51 additions and 10 deletions

View File

@ -212,8 +212,7 @@ lazy val cache = project
.settings(
name := "coursier-cache",
libraryDependencies ++= Seq(
"org.scalaz" %% "scalaz-concurrent" % "7.1.2",
"com.lihaoyi" %% "ammonite-terminal" % "0.5.0"
"org.scalaz" %% "scalaz-concurrent" % "7.1.2"
),
previousArtifacts := Set(organization.value %% moduleName.value % binaryCompatibilityVersion),
binaryIssueFilters ++= {

View File

@ -3,17 +3,58 @@ package coursier
import java.io.{File, Writer}
import java.util.concurrent._
import ammonite.terminal.{ TTY, Ansi }
import scala.annotation.tailrec
import scala.collection.mutable.ArrayBuffer
import scala.util.Try
object Terminal {
// Cut-n-pasted and adapted from
// https://github.com/lihaoyi/Ammonite/blob/10854e3b8b454a74198058ba258734a17af32023/terminal/src/main/scala/ammonite/terminal/Utils.scala
private lazy val pathedTput = if (new File("/usr/bin/tput").exists()) "/usr/bin/tput" else "tput"
def consoleDim(s: String): Option[Int] =
if (new File("/dev/tty").exists()) {
import sys.process._
Try(Seq("bash", "-c", s"$pathedTput $s 2> /dev/tty").!!.trim.toInt).toOption
} else
None
class Ansi(val output: Writer) extends AnyVal {
private def control(n: Int, c: Char) = output.write(s"\033[" + n + c)
/**
* Move up `n` squares
*/
def up(n: Int): Unit = if (n == 0) "" else control(n, 'A')
/**
* Move down `n` squares
*/
def down(n: Int): Unit = if (n == 0) "" else control(n, 'B')
/**
* Move left `n` squares
*/
def left(n: Int): Unit = if (n == 0) "" else control(n, 'D')
/**
* Clear the current line
*
* n=0: clear from cursor to end of line
* n=1: clear from cursor to start of line
* n=2: clear entire line
*/
def clearLine(n: Int): Unit = control(n, 'K')
}
}
class TermDisplay(
out: Writer,
var fallbackMode: Boolean = sys.env.get("COURSIER_NO_TERM").nonEmpty
) extends Cache.Logger {
private val ansi = new Ansi(out)
private val ansi = new Terminal.Ansi(out)
private var width = 80
private val refreshInterval = 1000 / 60
private val fallbackRefreshInterval = 1000
@ -170,11 +211,12 @@ class TermDisplay(
t.setDaemon(true)
def init(): Unit = {
try {
width = TTY.consoleDim("cols")
ansi.clearLine(2)
} catch { case _: Exception =>
fallbackMode = true
Terminal.consoleDim("cols") match {
case Some(cols) =>
width = cols
ansi.clearLine(2)
case None =>
fallbackMode = true
}
t.start()