01: package org.python.util;
02:
03: import java.io.File;
04: import java.io.IOException;
05: import jline.ConsoleReader;
06: import jline.Terminal;
07: import org.python.core.Py;
08: import org.python.core.PyObject;
09:
10: /**
11: * This class uses <a href="http://jline.sourceforge.net/">JLine</a> to provide
12: * readline like functionality to its console without requiring native readline
13: * support.
14: */
15: public class JLineConsole extends InteractiveConsole {
16:
17: public JLineConsole() {
18: this (null);
19: }
20:
21: public JLineConsole(PyObject locals) {
22: this (locals, CONSOLE_FILENAME);
23: try {
24: File historyFile = new File(
25: System.getProperty("user.home"),
26: ".jline-jython.history");
27: reader.getHistory().setHistoryFile(historyFile);
28: } catch (IOException e) {
29: // oh well, no history from file
30: }
31: }
32:
33: public JLineConsole(PyObject locals, String filename) {
34: super (locals, filename, true);
35: Terminal.setupTerminal();
36: try {
37: reader = new ConsoleReader();
38: } catch (IOException e) {
39: throw new RuntimeException(e);
40: }
41: }
42:
43: public String raw_input(PyObject prompt) {
44: String line = null;
45: try {
46: line = reader.readLine(prompt.toString());
47: } catch (IOException io) {
48: throw Py.IOError(io);
49: }
50: if (line == null) {
51: throw Py.EOFError("Ctrl-D exit");
52: }
53: return line.endsWith("\n") ? line.substring(0,
54: line.length() - 1) : line;
55: }
56:
57: protected ConsoleReader reader;
58: }
|