From 3462f7c8d69c8ac7c425130b8690748d5860e17e Mon Sep 17 00:00:00 2001 From: Mark Harrah Date: Tue, 4 May 2010 20:12:48 -0400 Subject: [PATCH] show history with !: or !:n --- sbt/src/main/scala/sbt/LineReader.scala | 9 +++--- sbt/src/main/scala/sbt/Main.scala | 31 ++++++++++++------- sbt/src/main/scala/sbt/complete/History.scala | 5 ++- 3 files changed, 28 insertions(+), 17 deletions(-) diff --git a/sbt/src/main/scala/sbt/LineReader.scala b/sbt/src/main/scala/sbt/LineReader.scala index f6c19373e..fa064c3b9 100644 --- a/sbt/src/main/scala/sbt/LineReader.scala +++ b/sbt/src/main/scala/sbt/LineReader.scala @@ -36,10 +36,6 @@ abstract class JLine extends LineReader case null => None case x => Some(x.trim) } - def getHistory: Array[String] = - JLine.synchronized { - reader.getHistory.getHistoryList.toArray(new Array[String](0)) - } } private object JLine { @@ -71,10 +67,13 @@ private object JLine Control.trapAndLog(log) { historyFile.getParentFile.mkdirs() - cr.getHistory.setHistoryFile(historyFile) + val history = cr.getHistory + history.setMaxSize(MaxHistorySize) + history.setHistoryFile(historyFile) } } def simple(historyPath: Option[Path], log: Logger): SimpleReader = new SimpleReader(historyPath, log) + val MaxHistorySize = 500 } class SimpleReader private[sbt] (historyPath: Option[Path], log: Logger) extends JLine { diff --git a/sbt/src/main/scala/sbt/Main.scala b/sbt/src/main/scala/sbt/Main.scala index b33808dea..955c81158 100755 --- a/sbt/src/main/scala/sbt/Main.scala +++ b/sbt/src/main/scala/sbt/Main.scala @@ -199,11 +199,11 @@ class xMain extends xsbti.AppMain } case action :: tail if action.startsWith(HistoryPrefix) => - historyCommand(action.substring(HistoryPrefix.length).trim, baseProject.historyPath, project.log) match + historyCommand(action.substring(HistoryPrefix.length).trim, baseProject.historyPath, JLine.MaxHistorySize, project.log) match { - case Some(command) => - println(command) //better to print it than to log it - continue(project, command :: tail, failAction) + case Some(commands) => + commands.foreach(println) //better to print it than to log it + continue(project, commands ::: tail, failAction) case None => failed(UsageErrorExitCode) } @@ -280,7 +280,7 @@ class xMain extends xsbti.AppMain if(message eq null) None else Some(message) } } - private def historyCommand(s: String, historyPath: Option[Path], log: Logger): Option[String] = + private def historyCommand(s: String, historyPath: Option[Path], maxLines: Int, log: Logger): Option[List[String]] = { val lines = historyPath.toList.flatMap(h => xsbt.FileUtilities.readLines(h.asFile) ).toArray if(lines.isEmpty) @@ -290,15 +290,24 @@ class xMain extends xsbti.AppMain } else { - def replaceLastCommand(s: String) { - lines(lines.length - 1) = s - historyPath foreach { h => xsbt.FileUtilities.writeLines(h.asFile, lines) } + val ListPrefix = ":" + val history = complete.History(lines, log) + if(s.startsWith(ListPrefix)) + { + val rest = s.substring(ListPrefix.length) + val show = complete.History.number(rest).getOrElse(lines.length) + printHistory(history, maxLines, show) + Some(Nil) + } + else { + val command = historyCommand(history, s, log) + command.foreach(lines(lines.length - 1) = _) + historyPath foreach { h => xsbt.FileUtilities.writeLines(h.asFile, lines) } + Some(command.toList) } - val command = historyCommand(complete.History(lines, log), s, log) - command.foreach(replaceLastCommand) - command } } + private def printHistory(history: complete.History, historySize: Int, show: Int): Unit = history.list(historySize, show).foreach(println) private def historyCommand(history: complete.History, s: String, log: Logger): Option[String] = { val ContainsPrefix = "?" diff --git a/sbt/src/main/scala/sbt/complete/History.scala b/sbt/src/main/scala/sbt/complete/History.scala index 6d2e81041..cb4fae033 100644 --- a/sbt/src/main/scala/sbt/complete/History.scala +++ b/sbt/src/main/scala/sbt/complete/History.scala @@ -30,13 +30,16 @@ final class History private(lines: Array[String], log: Logger) extends NotNull } else act + + def list(historySize: Int, show: Int): Seq[String] = + lines.toList.drop((lines.size - historySize) max 0).zipWithIndex.map { case (line, number) => " " + number + " " + line }.takeRight(show max 1) } object History { def apply(lines: Seq[String], log: Logger): History = new History(lines.toArray, log) - private def number(s: String): Option[Int] = + def number(s: String): Option[Int] = try { Some(s.toInt) } catch { case e: NumberFormatException => None } } \ No newline at end of file