Skip to content

Commit

Permalink
Attempt to fix both #133 and #164 ...
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Sep 7, 2017
1 parent f320221 commit a947144
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ else if (osName.startsWith("FreeBSD")) {
}

@Override
public Terminal winSysTerminal(String name, boolean nativeSignals, Terminal.SignalHandler signalHandler) throws IOException {
public Terminal winSysTerminal(String name, int codepage, boolean nativeSignals, Terminal.SignalHandler signalHandler) throws IOException {
if (JANSI_MAJOR_VERSION > 1 || JANSI_MAJOR_VERSION == 1 && JANSI_MINOR_VERSION >= 12) {
JansiWinSysTerminal terminal = new JansiWinSysTerminal(name, nativeSignals, signalHandler);
JansiWinSysTerminal terminal = new JansiWinSysTerminal(name, codepage, nativeSignals, signalHandler);
if (JANSI_MAJOR_VERSION == 1 && JANSI_MINOR_VERSION < 16) {
terminal.disableScrolling();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@
public class JansiWinSysTerminal extends AbstractWindowsTerminal {

public JansiWinSysTerminal(String name, boolean nativeSignals) throws IOException {
this(name, nativeSignals, SignalHandler.SIG_DFL);
this(name, 0, nativeSignals, SignalHandler.SIG_DFL);
}

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

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public Pty open(Attributes attributes, Size size) throws IOException {
}

@Override
public Terminal winSysTerminal(String name, boolean nativeSignals, Terminal.SignalHandler signalHandler) throws IOException {
return new JnaWinSysTerminal(name, nativeSignals, signalHandler);
public Terminal winSysTerminal(String name, int codepage, boolean nativeSignals, Terminal.SignalHandler signalHandler) throws IOException {
return new JnaWinSysTerminal(name, codepage, nativeSignals, signalHandler);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ public class JnaWinSysTerminal extends AbstractWindowsTerminal {
private int prevButtonState;

public JnaWinSysTerminal(String name, boolean nativeSignals) throws IOException {
this(name, nativeSignals, SignalHandler.SIG_DFL);
this(name, 0, nativeSignals, SignalHandler.SIG_DFL);
}

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

Expand Down
13 changes: 11 additions & 2 deletions terminal/src/main/java/org/jline/terminal/TerminalBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public final class TerminalBuilder {
//

public static final String PROP_ENCODING = "org.jline.terminal.encoding";
public static final String PROP_CODEPAGE = "org.jline.terminal.codepage";
public static final String PROP_TYPE = "org.jline.terminal.type";
public static final String PROP_JNA = "org.jline.terminal.jna";
public static final String PROP_JANSI = "org.jline.terminal.jansi";
Expand Down Expand Up @@ -69,6 +70,7 @@ public static TerminalBuilder builder() {
private OutputStream out;
private String type;
private String encoding;
private int codepage;
private Boolean system;
private Boolean jna;
private Boolean jansi;
Expand Down Expand Up @@ -189,6 +191,13 @@ private Terminal doBuild() throws IOException {
if (encoding == null) {
encoding = Charset.defaultCharset().name();
}
int codepage = this.codepage;
if (codepage <= 0) {
String str = System.getProperty(PROP_CODEPAGE);
if (str != null) {
codepage = Integer.parseInt(str);
}
}
String type = this.type;
if (type == null) {
type = System.getProperty(PROP_TYPE);
Expand Down Expand Up @@ -240,15 +249,15 @@ private Terminal doBuild() throws IOException {
else if (OSUtils.IS_WINDOWS) {
if (jna) {
try {
return load(JnaSupport.class).winSysTerminal(name, nativeSignals, signalHandler);
return load(JnaSupport.class).winSysTerminal(name, codepage, nativeSignals, signalHandler);
} catch (Throwable t) {
Log.debug("Error creating JNA based terminal: ", t.getMessage(), t);
exception.addSuppressed(t);
}
}
if (jansi) {
try {
return load(JansiSupport.class).winSysTerminal(name, nativeSignals, signalHandler);
return load(JansiSupport.class).winSysTerminal(name, codepage, nativeSignals, signalHandler);
} catch (Throwable t) {
Log.debug("Error creating JANSI based terminal: ", t.getMessage(), t);
exception.addSuppressed(t);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,33 @@ 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;

public AbstractWindowsTerminal(OutputStream output, String name, boolean nativeSignals, SignalHandler signalHandler) throws IOException {
public AbstractWindowsTerminal(OutputStream output, String name, int codepage, boolean nativeSignals, SignalHandler signalHandler) throws IOException {
super(name, TYPE_WINDOWS, signalHandler);
PipedInputStream input = new PipedInputStream(PIPE_SIZE); // UTF-8 encoded
this.slaveInputPipe = new PipedOutputStream(input); // UTF-8 encoded
this.input = new FilterInputStream(input) {}; // UTF-8 encoded
this.output = output;
// Try to set it to UTF-8
if (codepage == 0) {
codepage = UTF8_CODE_PAGE;
}
if (codepage > 0) {
// Find out the console code page and save it
this.consoleOutputCP = getConsoleOutputCP();
// Try to set the code page
if (this.consoleOutputCP != codepage) {
setConsoleOutputCP(codepage);
}
} else {
this.consoleOutputCP = 0;
}
// Whether the above call succeeded or failed, grab the console
// code page and find a matching charset to encode
String encoding = getConsoleEncoding();
if (encoding == null) {
encoding = Charset.defaultCharset().name();
Expand Down Expand Up @@ -192,6 +209,9 @@ public void close() throws IOException {
}
reader.close();
writer.close();
if (consoleOutputCP > 0) {
setConsoleOutputCP(consoleOutputCP);
}
}

static final int SHIFT_FLAG = 0x01;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ public interface JansiSupport {

Pty open(Attributes attributes, Size size) throws IOException;

Terminal winSysTerminal(String name, boolean nativeSignals, Terminal.SignalHandler signalHandler) throws IOException;
Terminal winSysTerminal(String name, int codepage, boolean nativeSignals, Terminal.SignalHandler signalHandler) throws IOException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ public interface JnaSupport {

Pty open(Attributes attributes, Size size) throws IOException;

Terminal winSysTerminal(String name, boolean nativeSignals, Terminal.SignalHandler signalHandler) throws IOException;
Terminal winSysTerminal(String name, int codepage, boolean nativeSignals, Terminal.SignalHandler signalHandler) throws IOException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void testBoldOnWindows() throws IOException {
private static class DumbWindowsTerminal extends AbstractWindowsTerminal {

public DumbWindowsTerminal() throws IOException {
super(new ByteArrayOutputStream(), "windows", false, SignalHandler.SIG_DFL);
super(new ByteArrayOutputStream(), "windows", 0, false, SignalHandler.SIG_DFL);
}

@Override
Expand Down

0 comments on commit a947144

Please sign in to comment.