sbt/launch/SimpleReader.scala

43 lines
881 B
Scala
Raw Normal View History

2009-08-21 14:12:43 +02:00
/* sbt -- Simple Build Tool
2009-09-26 08:18:04 +02:00
* Copyright 2008, 2009 Mark Harrah
2009-08-21 14:12:43 +02:00
*/
package xsbt.boot
import jline.ConsoleReader
2009-09-26 08:18:04 +02:00
abstract class JLine extends NotNull
2009-08-21 14:12:43 +02:00
{
2009-09-26 08:18:04 +02:00
protected[this] val reader: ConsoleReader
def readLine(prompt: String) = JLine.withJLine { unsynchronizedReadLine(prompt) }
private[this] def unsynchronizedReadLine(prompt: String) =
2009-08-21 14:12:43 +02:00
reader.readLine(prompt) match
{
case null => None
2009-09-26 08:18:04 +02:00
case x => Some(x.trim)
}
}
private object JLine
{
def terminal = jline.Terminal.getTerminal
def createReader() =
terminal.synchronized
{
val cr = new ConsoleReader
terminal.enableEcho()
cr.setBellEnabled(false)
cr
2009-08-21 14:12:43 +02:00
}
2009-09-26 08:18:04 +02:00
def withJLine[T](action: => T): T =
{
val t = terminal
t.synchronized
{
t.disableEcho()
try { action }
finally { t.enableEcho() }
}
}
}
object SimpleReader extends JLine
{
protected[this] val reader = JLine.createReader()
2009-08-21 14:12:43 +02:00
}