forked from jython/jython
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A substantial re-organisation of the interactive console.
Re-works (JLine, Readline or Plain) consoles, separating the console from the notion of an interpreter, and fixing (almost) a series of niggles with non-ascii encoding.
- Loading branch information
Showing
14 changed files
with
1,147 additions
and
308 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Copyright (c) 2013 Jython Developers | ||
package org.python.core; | ||
|
||
import java.io.EOFException; | ||
import java.io.IOException; | ||
import java.nio.ByteBuffer; | ||
|
||
/** | ||
* A class named in configuration as the value of <code>python.console</code> must implement this | ||
* interface, and provide a constructor with a single <code>String</code> argument, to be acceptable | ||
* during initialization of the interpreter. The argument to the constructor names the encoding in | ||
* use on the console. Such a class may provide line editing and history recall to an interactive | ||
* console. A default implementation (that does not provide any such facilities) is available as | ||
* {@link PlainConsole}. | ||
*/ | ||
public interface Console { | ||
|
||
/** | ||
* Complete initialization and (optionally) install a stream object with line-editing as the | ||
* replacement for <code>System.in</code>. | ||
* | ||
* @throws IOException in case of failure related to i/o | ||
*/ | ||
public void install() throws IOException; | ||
|
||
/** | ||
* Uninstall the Console (if possible). A Console that installs a replacement for | ||
* <code>System.in</code> should put back the original value. | ||
* | ||
* @throws UnsupportedOperationException if the Console cannot be uninstalled | ||
*/ | ||
public void uninstall() throws UnsupportedOperationException; | ||
|
||
/** | ||
* Write a prompt and read a line from standard input. The returned line does not include the | ||
* trailing newline. When the user enters the EOF key sequence, an EOFException should be | ||
* raised. The built-in function <code>raw_input</code> calls this method on the installed | ||
* console. | ||
* | ||
* @param prompt to output before reading a line | ||
* @return the line read in (encoded as bytes) | ||
* @throws IOException in case of failure related to i/o | ||
* @throws EOFException when the user enters the EOF key sequence | ||
*/ | ||
public ByteBuffer raw_input(CharSequence prompt) throws IOException, EOFException; | ||
|
||
/** | ||
* Write a prompt and read a line from standard input. The returned line does not include the | ||
* trailing newline. When the user enters the EOF key sequence, an EOFException should be | ||
* raised. The Py3k built-in function <code>input</code> calls this method on the installed | ||
* console. | ||
* | ||
* @param prompt to output before reading a line | ||
* @return the line read in | ||
* @throws IOException in case of failure related to i/o | ||
* @throws EOFException when the user enters the EOF key sequence | ||
*/ | ||
public CharSequence input(CharSequence prompt) throws IOException, EOFException; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// Copyright (c) 2013 Jython Developers | ||
package org.python.core; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.EOFException; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.nio.ByteBuffer; | ||
import java.nio.CharBuffer; | ||
import java.nio.charset.Charset; | ||
import java.nio.charset.IllegalCharsetNameException; | ||
import java.nio.charset.UnsupportedCharsetException; | ||
|
||
/** | ||
* A base class for classes that can install a console wrapper for a specific console-handling | ||
* library. The Jython command-line application, when it detects that the console is an interactive | ||
* session, chooses and installs a class named in registry item <code>python.console</code>, and | ||
* implementing interface {@link Console}. <code>PlainConsole</code> may be selected by the user | ||
* through that registry, and is the default console when the selected one fails to load. It will | ||
* also be installed by the Jython command-line application when in non-interactive mode. | ||
* <p> | ||
* Unlike some consoles, <code>PlainConsole</code> does not install a replacement for | ||
* <code>System.in</code> or use a native library. It prompts on <code>System.out</code> and reads | ||
* from <code>System.in</code> (wrapped with the console encoding). | ||
*/ | ||
public class PlainConsole implements Console { | ||
|
||
/** Encoding to use for line input. */ | ||
public final String encoding; | ||
|
||
/** Encoding to use for line input as a <code>Charset</code>. */ | ||
public final Charset encodingCharset; | ||
|
||
/** BufferedReader used by {@link #input(CharSequence)} */ | ||
private BufferedReader reader; | ||
|
||
/** | ||
* Construct an instance of the console class specifying the character encoding. This encoding | ||
* must be one supported by the JVM. The PlainConsole does not replace <code>System.in</code>, | ||
* and does not add any line-editing capability to what is standard for your OS console. | ||
* | ||
* @param encoding name of a supported encoding or <code>null</code> for | ||
* <code>Charset.defaultCharset()</code> | ||
*/ | ||
public PlainConsole(String encoding) throws IllegalCharsetNameException, | ||
UnsupportedCharsetException { | ||
if (encoding == null) { | ||
encoding = Charset.defaultCharset().name(); | ||
} | ||
this.encoding = encoding; | ||
encodingCharset = Charset.forName(encoding); | ||
} | ||
|
||
@Override | ||
public void install() { | ||
// Create a Reader with the right character encoding | ||
reader = new BufferedReader(new InputStreamReader(System.in, encodingCharset)); | ||
} | ||
|
||
/** | ||
* A <code>PlainConsole</code> may be uninstalled. This method assumes any sub-class may not be | ||
* uninstalled. Sub-classes that permit themselves to be uninstalled <b>must</b> override (and | ||
* not call) this method. | ||
* | ||
* @throws UnsupportedOperationException unless this class is exactly <code>PlainConsole</code> | ||
*/ | ||
@Override | ||
public void uninstall() throws UnsupportedOperationException { | ||
Class<? extends Console> myClass = this.getClass(); | ||
if (myClass != PlainConsole.class) { | ||
throw new UnsupportedOperationException(myClass.getSimpleName() | ||
+ " console may not be uninstalled."); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
* <p> | ||
* The base implementation calls {@link #input(CharSequence)} and applies the console encoding | ||
* to obtain the bytes. This may be a surprise. Line-editing consoles necessarily operate in | ||
* terms of characters rather than bytes, and therefore support a direct implementation of | ||
* <code>input</code>. | ||
*/ | ||
@Override | ||
public ByteBuffer raw_input(CharSequence prompt) throws IOException, EOFException { | ||
CharSequence line = input(prompt); | ||
return encodingCharset.encode(CharBuffer.wrap(line)); | ||
} | ||
|
||
// The base implementation simply uses <code>System.out</code> and <code>System.in</code>. | ||
@Override | ||
public CharSequence input(CharSequence prompt) throws IOException, EOFException { | ||
|
||
// Issue the prompt with no newline | ||
System.out.print(prompt); | ||
|
||
// Get the line from the console via java.io | ||
String line = reader.readLine(); | ||
if (line == null) { | ||
throw new EOFException(); | ||
} else { | ||
return line; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.