Added shutdown hook for restoring the terminal

This commit is contained in:
James Roper 2013-07-30 18:53:24 +10:00
parent 5139a2f148
commit 1eaf548bcd
1 changed files with 18 additions and 2 deletions

View File

@ -5,12 +5,28 @@ package sbt
import scala.annotation.tailrec
import java.io.{File, PrintWriter}
import jline.TerminalFactory
object MainLoop
{
/** Entry point to run the remaining commands in State with managed global logging.*/
def runLogged(state: State): xsbti.MainResult =
runLoggedLoop(state, state.globalLogging.backing)
def runLogged(state: State): xsbti.MainResult = {
// We've disabled jline shutdown hooks to prevent classloader leaks, and have been careful to always restore
// the jline terminal in finally blocks, but hitting ctrl+c prevents finally blocks from being executed, in that
// case the only way to restore the terminal is in a shutdown hook.
val shutdownHook = new Thread(new Runnable {
def run() {
TerminalFactory.get().restore()
}
})
try {
Runtime.getRuntime.addShutdownHook(shutdownHook)
runLoggedLoop(state, state.globalLogging.backing)
} finally {
Runtime.getRuntime.removeShutdownHook(shutdownHook)
}
}
/** Run loop that evaluates remaining commands and manages changes to global logging configuration.*/
@tailrec def runLoggedLoop(state: State, logBacking: GlobalLogBacking): xsbti.MainResult =