2010-06-14 04:59:29 +02:00
|
|
|
/* sbt -- Simple Build Tool
|
|
|
|
|
* Copyright 2010 Mark Harrah
|
|
|
|
|
*/
|
|
|
|
|
package sbt
|
|
|
|
|
package complete
|
2010-05-05 04:22:30 +02:00
|
|
|
|
2010-12-19 18:03:10 +01:00
|
|
|
import java.io.File
|
|
|
|
|
|
2010-05-05 04:22:30 +02:00
|
|
|
object HistoryCommands
|
|
|
|
|
{
|
|
|
|
|
val Start = "!"
|
|
|
|
|
// second characters
|
|
|
|
|
val Contains = "?"
|
|
|
|
|
val Last = "!"
|
|
|
|
|
val ListCommands = ":"
|
|
|
|
|
|
|
|
|
|
def ContainsFull = h(Contains)
|
|
|
|
|
def LastFull = h(Last)
|
|
|
|
|
def ListFull = h(ListCommands)
|
|
|
|
|
|
|
|
|
|
def ListN = ListFull + "n"
|
2010-05-16 20:42:41 +02:00
|
|
|
def ContainsString = ContainsFull + "string"
|
|
|
|
|
def StartsWithString = Start + "string"
|
2010-05-05 04:22:30 +02:00
|
|
|
def Previous = Start + "-n"
|
|
|
|
|
def Nth = Start + "n"
|
|
|
|
|
|
|
|
|
|
private def h(s: String) = Start + s
|
|
|
|
|
def plainCommands = Seq(ListFull, Start, LastFull, ContainsFull)
|
|
|
|
|
|
|
|
|
|
def descriptions = Seq(
|
|
|
|
|
LastFull -> "Execute the last command again",
|
|
|
|
|
ListFull -> "Show all previous commands",
|
|
|
|
|
ListN -> "Show the last n commands",
|
2010-05-16 20:42:41 +02:00
|
|
|
Nth -> ("Execute the command with index n, as shown by the " + ListFull + " command"),
|
2010-05-05 04:22:30 +02:00
|
|
|
Previous -> "Execute the nth command before this one",
|
2010-05-16 20:42:41 +02:00
|
|
|
StartsWithString -> "Execute the most recent command starting with 'string'",
|
|
|
|
|
ContainsString -> "Execute the most recent command containing 'string'"
|
2010-05-05 04:22:30 +02:00
|
|
|
)
|
2010-06-14 04:59:29 +02:00
|
|
|
def helpString = "History commands:\n " + (descriptions.map{ case (c,d) => c + " " + d}).mkString("\n ")
|
|
|
|
|
def printHelp(): Unit =
|
|
|
|
|
println(helpString)
|
2011-03-11 22:52:44 +01:00
|
|
|
def printHistory(history: complete.History, historySize: Int, show: Int): Unit =
|
|
|
|
|
history.list(historySize, show).foreach(println)
|
2010-05-05 04:22:30 +02:00
|
|
|
|
2011-03-11 22:52:44 +01:00
|
|
|
import DefaultParsers._
|
|
|
|
|
|
|
|
|
|
val MaxLines = 500
|
|
|
|
|
lazy val num = token(NatBasic, "<integer>")
|
|
|
|
|
lazy val last = Last ^^^ { execute(_ !!) }
|
|
|
|
|
lazy val list = ListCommands ~> (num ?? Int.MaxValue) map { show =>
|
|
|
|
|
(h: History) => { printHistory(h, MaxLines, show); Some(Nil) }
|
|
|
|
|
}
|
|
|
|
|
lazy val execStr = flag('?') ~ token(any.+.string, "<string>") map { case (contains, str) =>
|
|
|
|
|
execute(h => if(contains) h !? str else h ! str)
|
2010-05-05 04:22:30 +02:00
|
|
|
}
|
2011-03-11 22:52:44 +01:00
|
|
|
lazy val execInt = flag('-') ~ num map { case (neg, value) =>
|
|
|
|
|
execute(h => if(neg) h !- value else h ! value)
|
|
|
|
|
}
|
|
|
|
|
lazy val help = success( (h: History) => { printHelp(); Some(Nil) } )
|
|
|
|
|
|
|
|
|
|
def execute(f: History => Option[String]): History => Option[List[String]] = (h: History) =>
|
2011-01-22 20:01:59 +01:00
|
|
|
{
|
2011-03-11 22:52:44 +01:00
|
|
|
val command = f(h)
|
|
|
|
|
val lines = h.lines.toArray
|
|
|
|
|
command.foreach(lines(lines.length - 1) = _)
|
|
|
|
|
h.path foreach { h => IO.writeLines(h, lines) }
|
|
|
|
|
Some(command.toList)
|
2011-01-22 20:01:59 +01:00
|
|
|
}
|
2011-03-11 22:52:44 +01:00
|
|
|
|
|
|
|
|
val actionParser: Parser[complete.History => Option[List[String]]] =
|
|
|
|
|
Start ~> (help | last | execInt | list | execStr ) // execStr must come last
|
2010-05-05 04:22:30 +02:00
|
|
|
}
|