From 82e64fe7a8049defd69a391c6b480c5df956330e Mon Sep 17 00:00:00 2001 From: Alexandre Archambault Date: Sun, 28 Feb 2016 17:43:57 +0100 Subject: [PATCH] Remove ammonite-terminal dependency --- build.sbt | 3 +- .../src/main/scala/coursier/TermDisplay.scala | 58 ++++++++++++++++--- 2 files changed, 51 insertions(+), 10 deletions(-) diff --git a/build.sbt b/build.sbt index 954449aa0..67c333192 100644 --- a/build.sbt +++ b/build.sbt @@ -207,8 +207,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 ++= { diff --git a/cache/src/main/scala/coursier/TermDisplay.scala b/cache/src/main/scala/coursier/TermDisplay.scala index 3c6d67ac9..de981e46b 100644 --- a/cache/src/main/scala/coursier/TermDisplay.scala +++ b/cache/src/main/scala/coursier/TermDisplay.scala @@ -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()