How can I stop a REPL-like console application by pressing Ctrl-d, without waiting for user to type Ctr-d then enter?
Here is a code sample :
def isExit(s: String): Boolean = s.head.toInt == 4 || s == "exit"
def main(args: Array[String]) = {
val continue: Boolean = true
while(continue){
println "> "
io.StdIn.readLine match {
case x if isExit(x) => println "> Bye!" ; continue = false
case x => evaluate(x)
}
}
}
The s.head.toInt == 4
is to test if the first char of the input line is a ctrl d.
EDIT : Complete source code to run it :
object Test {
def isExit(s: String): Boolean = s.headOption.map(_.toInt) == Some(4) || s == "exit"
def evaluate(s: String) = println(s"Evaluation : $s")
def main(args: Array[String]) = {
var continue = true
while(continue){
print("> ")
io.StdIn.readLine match {
case x if isExit(x) => println("> Bye!") ; continue = false
case x => evaluate(x)
}
}
}
}
With this, I got a NullPointerException on the s.headOption (because of a null s)