Skip to content

Commit

Permalink
Big refactoring of the jline key binding support to be closer to read…
Browse files Browse the repository at this point in the history
…line()

Unicode is also now fully supported
  • Loading branch information
gnodet committed Apr 11, 2011
1 parent 7a4d274 commit f728bf8
Show file tree
Hide file tree
Showing 23 changed files with 1,816 additions and 1,207 deletions.
7 changes: 2 additions & 5 deletions src/main/java/jline/Terminal.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public interface Terminal
*/
OutputStream wrapOutIfNeeded(OutputStream out);

InputStream wrapInIfNeeded(InputStream in) throws IOException;

/**
* For terminals that don't wrap when character is written in last column,
* only when the next character is written.
Expand All @@ -51,9 +53,4 @@ public interface Terminal

void setEchoEnabled(boolean enabled);

int readCharacter(InputStream in) throws IOException;

int readVirtualKey(InputStream in) throws IOException;

InputStream getDefaultBindings();
}
11 changes: 4 additions & 7 deletions src/main/java/jline/TerminalFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@

package jline;

import jline.internal.Configuration;
import jline.internal.Log;

import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;

import jline.internal.Configuration;
import jline.internal.Log;

/**
* Creates terminal instances.
*
Expand Down Expand Up @@ -46,10 +46,7 @@ public static synchronized Terminal create() {
Log.trace(new Throwable("CREATE MARKER"));
}

String type = Configuration.getString(JLINE_TERMINAL);
if (type == null) {
type = AUTO;
}
String type = Configuration.getString(JLINE_TERMINAL, AUTO);

Log.debug("Creating terminal; type=", type);

Expand Down
22 changes: 7 additions & 15 deletions src/main/java/jline/TerminalSupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@

package jline;

import jline.internal.Log;
import jline.internal.Configuration;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import jline.internal.Configuration;
import jline.internal.Log;

/**
* Provides support for {@link Terminal} instances.
*
Expand All @@ -23,8 +23,6 @@
public abstract class TerminalSupport
implements Terminal
{
public static String DEFAULT_KEYBINDINGS_PROPERTIES = "keybindings.properties";

public static final String JLINE_SHUTDOWNHOOK = "jline.shutdownhook";

public static final int DEFAULT_WIDTH = 80;
Expand All @@ -41,6 +39,8 @@ public abstract class TerminalSupport

private boolean ansiSupported;

private Configuration configuration;

protected TerminalSupport(final boolean supported) {
this.supported = supported;
this.shutdownHookEnabled = Configuration.getBoolean(JLINE_SHUTDOWNHOOK, false);
Expand Down Expand Up @@ -148,16 +148,8 @@ public synchronized void setEchoEnabled(final boolean enabled) {
Log.debug("Echo enabled: ", enabled);
}

public int readCharacter(final InputStream in) throws IOException {
return in.read();
}

public int readVirtualKey(final InputStream in) throws IOException {
return readCharacter(in);
}

public InputStream getDefaultBindings() {
return TerminalSupport.class.getResourceAsStream(DEFAULT_KEYBINDINGS_PROPERTIES);
public InputStream wrapInIfNeeded(InputStream in) throws IOException {
return in;
}

//
Expand Down
128 changes: 0 additions & 128 deletions src/main/java/jline/UnixTerminal.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,9 @@

package jline;

import jline.console.Key;
import jline.internal.Configuration;
import jline.internal.Log;
import jline.internal.ReplayPrefixOneCharInputStream;
import jline.internal.TerminalLineSettings;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

import static jline.UnixTerminal.UnixKey.*;
import static jline.console.Key.*;

/**
* Terminal that is used for unix platforms. Terminal initialization
* is handled by issuing the <em>stty</em> command against the
Expand All @@ -41,15 +29,8 @@ public class UnixTerminal
{
private final TerminalLineSettings settings = new TerminalLineSettings();

private final ReplayPrefixOneCharInputStream replayStream;

private final InputStreamReader replayReader;

public UnixTerminal() throws Exception {
super(true);

this.replayStream = new ReplayPrefixOneCharInputStream(Configuration.getInputEncoding());
this.replayReader = new InputStreamReader(replayStream, replayStream.getEncoding());
}

protected TerminalLineSettings getSettings() {
Expand Down Expand Up @@ -120,113 +101,4 @@ public synchronized void setEchoEnabled(final boolean enabled) {
}
}

@Override
public int readVirtualKey(final InputStream in) throws IOException {
int c = readCharacter(in);

if (Key.valueOf(c) == DELETE && settings.getProperty("erase") == DELETE.code) {
c = BACKSPACE.code;
}

UnixKey key = UnixKey.valueOf(c);

// in Unix terminals, arrow keys are represented by a sequence of 3 characters. E.g., the up arrow key yields 27, 91, 68
if (key == ARROW_START) {
// also the escape key is 27 thats why we read until we have something different than 27
// this is a bugfix, because otherwise pressing escape and than an arrow key was an undefined state
while (key == ARROW_START) {
c = readCharacter(in);
key = UnixKey.valueOf(c);
}

if (key == ARROW_PREFIX || key == O_PREFIX) {
c = readCharacter(in);
key = UnixKey.valueOf(c);

if (key == ARROW_UP) {
return CTRL_P.code;
}
else if (key == ARROW_DOWN) {
return CTRL_N.code;
}
else if (key == ARROW_LEFT) {
return CTRL_B.code;
}
else if (key == ARROW_RIGHT) {
return CTRL_F.code;
}
else if (key == HOME_CODE) {
return CTRL_A.code;
}
else if (key == END_CODE) {
return CTRL_E.code;
}
else if (key == DEL_THIRD) {
readCharacter(in); // read 4th & ignore
return DELETE.code;
}
}
}

// handle unicode characters, thanks for a patch from amyi@inf.ed.ac.uk
if (c > 128) {
// handle unicode characters longer than 2 bytes,
// thanks to Marc.Herbert@continuent.com
replayStream.setInput(c, in);
// replayReader = new InputStreamReader(replayStream, encoding);
c = replayReader.read();
}

return c;
}

/**
* Unix keys.
*/
public static enum UnixKey
{
ARROW_START(27),

ARROW_PREFIX(91),

ARROW_LEFT(68),

ARROW_RIGHT(67),

ARROW_UP(65),

ARROW_DOWN(66),

O_PREFIX(79),

HOME_CODE(72),

END_CODE(70),

DEL_THIRD(51),

DEL_SECOND(126),;

public final short code;

UnixKey(final int code) {
this.code = (short) code;
}

private static final Map<Short, UnixKey> codes;

static {
Map<Short, UnixKey> map = new HashMap<Short, UnixKey>();

for (UnixKey key : UnixKey.values()) {
map.put(key.code, key);
}

codes = map;
}

public static UnixKey valueOf(final int code) {
return codes.get((short) code);
}
}
}

0 comments on commit f728bf8

Please sign in to comment.