Skip to content

Commit

Permalink
Merge branch 'issue-133', fixes #133
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Jun 16, 2017
2 parents d36ee25 + de2f031 commit b120987
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/
package org.jline.terminal.impl.jansi.win;

import java.io.BufferedOutputStream;
import java.io.FileDescriptor;
import java.io.FileOutputStream;
import java.io.IOError;
Expand Down Expand Up @@ -36,14 +37,20 @@ public JansiWinSysTerminal(String name, boolean nativeSignals) throws IOExceptio
}

public JansiWinSysTerminal(String name, boolean nativeSignals, SignalHandler signalHandler) throws IOException {
super(new WindowsAnsiOutputStream(new FileOutputStream(FileDescriptor.out)),
super(new WindowsAnsiOutputStream(new BufferedOutputStream(new FileOutputStream(FileDescriptor.out))),
name, nativeSignals, signalHandler);
}

@Override
protected int getConsoleOutputCP() {
return Kernel32.GetConsoleOutputCP();
}

@Override
protected void setConsoleOutputCP(int cp) {
Kernel32.SetConsoleOutputCP(cp);
}

@Override
protected int getConsoleMode() {
return WindowsSupport.getConsoleMode();
Expand All @@ -61,10 +68,10 @@ public Size getSize() {
return size;
}

protected byte[] readConsoleInput() throws IOException {
protected String readConsoleInput() throws IOException {
INPUT_RECORD[] events = WindowsSupport.readConsoleInput(1);
if (events == null) {
return new byte[0];
return "";
}
StringBuilder sb = new StringBuilder();
for (INPUT_RECORD event : events) {
Expand Down Expand Up @@ -112,7 +119,7 @@ protected byte[] readConsoleInput() throws IOException {
}
}
}
return sb.toString().getBytes();
return sb.toString();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
*/
package org.jline.terminal.impl.jna.win;

import java.io.BufferedOutputStream;
import java.io.FileDescriptor;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.function.IntConsumer;

import com.sun.jna.LastErrorException;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.IntByReference;
import org.jline.terminal.Cursor;
Expand All @@ -33,15 +35,26 @@ public JnaWinSysTerminal(String name, boolean nativeSignals) throws IOException
}

public JnaWinSysTerminal(String name, boolean nativeSignals, SignalHandler signalHandler) throws IOException {
super(new WindowsAnsiOutputStream(new FileOutputStream(FileDescriptor.out), consoleOut),
super(new WindowsAnsiOutputStream(new BufferedOutputStream(new FileOutputStream(FileDescriptor.out)), consoleOut),
name, nativeSignals, signalHandler);
strings.put(InfoCmp.Capability.key_mouse, "\\E[M");
}

@Override
protected int getConsoleOutputCP() {
return Kernel32.INSTANCE.GetConsoleOutputCP();
}

@Override
protected void setConsoleOutputCP(int cp) {
try {
Kernel32.INSTANCE.SetConsoleOutputCP(cp);
} catch (LastErrorException e) {
// Not sure why it throws exceptions, just log at trace
Log.trace("Error setting console output code page", e);
}
}

@Override
protected int getConsoleMode() {
IntByReference mode = new IntByReference();
Expand All @@ -62,10 +75,10 @@ public Size getSize() {

private char[] mouse = new char[] { '\033', '[', 'M', ' ', ' ', ' ' };

protected byte[] readConsoleInput() throws IOException {
protected String readConsoleInput() throws IOException {
Kernel32.INPUT_RECORD[] events = doReadConsoleInput();
if (events == null) {
return new byte[0];
return "";
}
StringBuilder sb = new StringBuilder();
for (Kernel32.INPUT_RECORD event : events) {
Expand Down Expand Up @@ -148,7 +161,7 @@ protected byte[] readConsoleInput() throws IOException {
prevButtonState = dwButtonState;
}
}
return sb.toString().getBytes();
return sb.toString();
}

private Kernel32.INPUT_RECORD[] doReadConsoleInput() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ interface Kernel32 extends StdCallLibrary {
// HWND WINAPI GetConsoleWindow(void);
Pointer GetConsoleWindow();

// UINT WINAPI GetConsoleCP(void)
int GetConsoleCP();

// UINT WINAPI GetConsoleOutputCP(void)
int GetConsoleOutputCP();

Expand Down Expand Up @@ -188,6 +191,10 @@ void SetConsoleCursorInfo(Pointer in_hConsoleOutput,
// _In_ UINT wCodePageID);
void SetConsoleCP(int in_wCodePageID) throws LastErrorException;

// BOOL WINAPI SetConsoleOutputCP(
// _In_ UINT wCodePageID);
void SetConsoleOutputCP(int in_wCodePageID) throws LastErrorException;

// BOOL WINAPI SetConsoleCursorPosition(
// _In_ HANDLE hConsoleOutput,
// _In_ COORD dwCursorPosition);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public abstract class AbstractWindowsTerminal extends AbstractTerminal {

private static final int PIPE_SIZE = 1024;

private static final Charset CHARSET = Charset.forName("UTF-8");
private static final int CODE_PAGE = 65001;

protected static final int ENABLE_PROCESSED_INPUT = 0x0001;
protected static final int ENABLE_LINE_INPUT = 0x0002;
protected static final int ENABLE_ECHO_INPUT = 0x0004;
Expand All @@ -52,6 +55,7 @@ public abstract class AbstractWindowsTerminal extends AbstractTerminal {
protected final ShutdownHooks.Task closer;
protected final Attributes attributes = new Attributes();
protected final Thread pump;
protected final int consoleOutputCP;

protected MouseTracking tracking = MouseTracking.Off;
private volatile boolean closing;
Expand All @@ -62,12 +66,10 @@ public AbstractWindowsTerminal(OutputStream output, String name, boolean nativeS
this.slaveInputPipe = new PipedOutputStream(input);
this.input = new FilterInputStream(input) {};
this.output = output;
String encoding = getConsoleEncoding();
if (encoding == null) {
encoding = Charset.defaultCharset().name();
}
this.reader = new NonBlockingReader(getName(), new org.jline.utils.InputStreamReader(input, encoding));
this.writer = new PrintWriter(new OutputStreamWriter(output, encoding));
this.consoleOutputCP = getConsoleOutputCP();
setConsoleOutputCP(CODE_PAGE);
this.reader = new NonBlockingReader(getName(), new org.jline.utils.InputStreamReader(input, CHARSET));
this.writer = new PrintWriter(new OutputStreamWriter(output, CHARSET));
parseInfoCmp();
// Attributes
attributes.setLocalFlag(Attributes.LocalFlag.ISIG, true);
Expand Down Expand Up @@ -104,22 +106,6 @@ public SignalHandler handle(Signal signal, SignalHandler handler) {
return prev;
}

protected String getConsoleEncoding() {
int codepage = getConsoleOutputCP();
//http://docs.oracle.com/javase/6/docs/technotes/guides/intl/encoding.doc.html
String charsetMS = "ms" + codepage;
if (java.nio.charset.Charset.isSupported(charsetMS)) {
return charsetMS;
}
String charsetCP = "cp" + codepage;
if (java.nio.charset.Charset.isSupported(charsetCP)) {
return charsetCP;
}
return null;
}

protected abstract int getConsoleOutputCP();

public NonBlockingReader reader() {
return reader;
}
Expand Down Expand Up @@ -172,10 +158,6 @@ protected int ctrl(char key) {
return (Character.toUpperCase(key) & 0x1f);
}

protected abstract int getConsoleMode();

protected abstract void setConsoleMode(int mode);

public void setSize(Size size) {
throw new UnsupportedOperationException("Can not resize windows terminal");
}
Expand All @@ -189,10 +171,9 @@ public void close() throws IOException {
}
reader.close();
writer.close();
setConsoleOutputCP(consoleOutputCP);
}

protected abstract byte[] readConsoleInput() throws IOException;

protected String getEscapeSequence(short keyCode) {
// virtual keycodes: http://msdn.microsoft.com/en-us/library/windows/desktop/dd375731(v=vs.85).aspx
// TODO: numpad keys, modifiers
Expand Down Expand Up @@ -290,8 +271,8 @@ protected String getSequence(InfoCmp.Capability cap) {
protected void pump() {
try {
while (!closing) {
byte[] buf = readConsoleInput();
for (byte b : buf) {
String buf = readConsoleInput();
for (byte b : buf.getBytes(CHARSET)) {
processInputByte(b);
}
}
Expand Down Expand Up @@ -342,5 +323,15 @@ public boolean trackMouse(MouseTracking tracking) {
return true;
}

protected abstract int getConsoleOutputCP();

protected abstract void setConsoleOutputCP(int cp);

protected abstract int getConsoleMode();

protected abstract void setConsoleMode(int mode);

protected abstract String readConsoleInput() throws IOException;

}

0 comments on commit b120987

Please sign in to comment.