I want to write an interactive shell in scala, with readline support (Ctrl-l, arrow keys, line editing, history, etc.).
I know how to do this in python:
import readline
finished = False
while not finished:
try:
line = raw_input('> ')
if line:
if line == 'q':
finished = True
else:
print line
except KeyboardInterrupt:
print 'Ctrl-c'; finished = True
except EOFError:
print 'Ctrl-d'; finished = True
I want to write a simple scala program with exactly the same behavior. My nearest solution so far is the following scala:
import scala.tools.jline
val consoleReader = new jline.console.ConsoleReader()
var finished = false
while (!finished) {
val line = consoleReader.readLine("> ")
if (line == null) {
println("Ctrl-d")
finished = true
} else if (line.size > 0) {
if (line == "q") {
finished = true
} else {
println(line)
}
}
}
Open questions:
- how to handle ctrl-c?
- Is it possible to use exceptions like python?
- Is this an optimal solution or can it be improved?
source
share