I want to write interactive shell in scala, with support for readline (Ctrl-l, arrow keys, line editing, history, etc.).
I know how to do it in python:
# enable support for Ctrl-l, arrow keys, line editing, history, etc.
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 behaviour. My closest solution until now is the following scala:
// used to support Ctrl-l, arrow keys, line editing, history, etc.
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)
}
}
}
The open questions are:
- how to handle ctrl-c?
- is it possible to use exceptions in a similar way to python?
- is this optimal solution or it can be improved?