Skip to content

Commit

Permalink
Support for cygwin recent versions, fix for #520
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed May 5, 2020
1 parent 496492e commit 0de635b
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2002-2017, the original author or authors.
* Copyright (c) 2002-2020, the original author or authors.
*
* This software is distributable under the BSD license. See the terms of the
* BSD license in the documentation provided with this software.
Expand Down Expand Up @@ -135,4 +135,9 @@ public Terminal winSysTerminal(String name, String type, boolean ansiPassThrough
throw new UnsupportedOperationException();
}

@Override
public boolean isWindowsConsole() {
return JansiWinSysTerminal.isWindowsConsole();
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2002-2019, the original author or authors.
* Copyright (c) 2002-2020, the original author or authors.
*
* This software is distributable under the BSD license. See the terms of the
* BSD license in the documentation provided with this software.
Expand All @@ -22,7 +22,6 @@
import org.fusesource.jansi.internal.WindowsSupport;
import org.jline.terminal.Cursor;
import org.jline.terminal.Size;
import org.jline.terminal.Terminal;
import org.jline.terminal.impl.AbstractWindowsTerminal;
import org.jline.terminal.impl.jansi.JansiSupportImpl;
import org.jline.utils.InfoCmp;
Expand Down Expand Up @@ -72,6 +71,12 @@ public static JansiWinSysTerminal createTerminal(String name, String type, boole
return terminal;
}

public static boolean isWindowsConsole() {
long console = GetStdHandle(STD_OUTPUT_HANDLE);
int[] mode = new int[1];
return Kernel32.GetConsoleMode(console, mode) == 0;
}

JansiWinSysTerminal(Writer writer, String name, String type, Charset encoding, int codepage, boolean nativeSignals, SignalHandler signalHandler) throws IOException {
super(writer, name, type, encoding, codepage, nativeSignals, signalHandler);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2002-2019, the original author or authors.
* Copyright (c) 2002-2020, the original author or authors.
*
* This software is distributable under the BSD license. See the terms of the
* BSD license in the documentation provided with this software.
Expand Down Expand Up @@ -38,4 +38,10 @@ public Terminal winSysTerminal(String name, String type, boolean ansiPassThrough
public Terminal winSysTerminal(String name, String type, boolean ansiPassThrough, Charset encoding, int codepage, boolean nativeSignals, Terminal.SignalHandler signalHandler, boolean paused) throws IOException {
return JnaWinSysTerminal.createTerminal(name, type, ansiPassThrough, encoding, codepage, nativeSignals, signalHandler, paused);
}

@Override
public boolean isWindowsConsole() {
return JnaWinSysTerminal.isWindowsConsole();
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2002-2019, the original author or authors.
* Copyright (c) 2002-2020, the original author or authors.
*
* This software is distributable under the BSD license. See the terms of the
* BSD license in the documentation provided with this software.
Expand All @@ -19,7 +19,6 @@
import com.sun.jna.ptr.IntByReference;
import org.jline.terminal.Cursor;
import org.jline.terminal.Size;
import org.jline.terminal.Terminal;
import org.jline.terminal.impl.AbstractWindowsTerminal;
import org.jline.utils.InfoCmp;
import org.jline.utils.OSUtils;
Expand Down Expand Up @@ -67,6 +66,16 @@ public static JnaWinSysTerminal createTerminal(String name, String type, boolean
return terminal;
}

public static boolean isWindowsConsole() {
try {
IntByReference mode = new IntByReference();
Kernel32.INSTANCE.GetConsoleMode(consoleOut, mode);
return true;
} catch (LastErrorException e) {
return false;
}
}

JnaWinSysTerminal(Writer writer, String name, String type, Charset encoding, int codepage, boolean nativeSignals, SignalHandler signalHandler) throws IOException {
super(writer, name, type, encoding, codepage, nativeSignals, signalHandler);
strings.put(InfoCmp.Capability.key_mouse, "\\E[M");
Expand Down
40 changes: 22 additions & 18 deletions terminal/src/main/java/org/jline/terminal/TerminalBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -317,12 +317,32 @@ private Terminal doBuild() throws IOException {
IllegalStateException exception = new IllegalStateException("Unable to create a system terminal");
Terminal terminal = null;
if (OSUtils.IS_WINDOWS) {
boolean cygwinTerm = "cygwin".equals(System.getenv("TERM"));
boolean ansiPassThrough = OSUtils.IS_CONEMU;
boolean winConsole = true;
if (terminal == null && jna) {
try {
JnaSupport support = load(JnaSupport.class);
winConsole = support.isWindowsConsole();
terminal = support.winSysTerminal(name, type, ansiPassThrough, encoding, codepage, nativeSignals, signalHandler, paused);
} catch (Throwable t) {
Log.debug("Error creating JNA based terminal: ", t.getMessage(), t);
exception.addSuppressed(t);
}
}
if (terminal == null && jansi) {
try {
JansiSupport support = load(JansiSupport.class);
winConsole = support.isWindowsConsole();
terminal = support.winSysTerminal(name, type, ansiPassThrough, encoding, codepage, nativeSignals, signalHandler, paused);
} catch (Throwable t) {
Log.debug("Error creating JANSI based terminal: ", t.getMessage(), t);
exception.addSuppressed(t);
}
}
//
// Cygwin support
//
if ((OSUtils.IS_CYGWIN || OSUtils.IS_MSYSTEM) && exec && !cygwinTerm) {
if (terminal == null && exec && (OSUtils.IS_CYGWIN || OSUtils.IS_MSYSTEM) && winConsole) {
try {
Pty pty = ExecPty.current();
// Cygwin defaults to XTERM, but actually supports 256 colors,
Expand All @@ -337,22 +357,6 @@ private Terminal doBuild() throws IOException {
exception.addSuppressed(e);
}
}
if (terminal == null && jna) {
try {
terminal = load(JnaSupport.class).winSysTerminal(name, type, ansiPassThrough, encoding, codepage, nativeSignals, signalHandler, paused);
} catch (Throwable t) {
Log.debug("Error creating JNA based terminal: ", t.getMessage(), t);
exception.addSuppressed(t);
}
}
if (terminal == null && jansi) {
try {
terminal = load(JansiSupport.class).winSysTerminal(name, type, ansiPassThrough, encoding, codepage, nativeSignals, signalHandler, paused);
} catch (Throwable t) {
Log.debug("Error creating JANSI based terminal: ", t.getMessage(), t);
exception.addSuppressed(t);
}
}
if (terminal == null && !jna && !jansi && (dumb == null || !dumb)) {
throw new IllegalStateException("Unable to create a system terminal. On windows, either "
+ "JNA or JANSI library is required. Make sure to add one of those in the classpath.");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2002-2019, the original author or authors.
* Copyright (c) 2002-2020, the original author or authors.
*
* This software is distributable under the BSD license. See the terms of the
* BSD license in the documentation provided with this software.
Expand All @@ -25,4 +25,6 @@ public interface JansiSupport {

Terminal winSysTerminal(String name, String type, boolean ansiPassThrough, Charset encoding, int codepage, boolean nativeSignals, Terminal.SignalHandler signalHandler, boolean paused) throws IOException;

boolean isWindowsConsole();

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2002-2019, the original author or authors.
* Copyright (c) 2002-2020, the original author or authors.
*
* This software is distributable under the BSD license. See the terms of the
* BSD license in the documentation provided with this software.
Expand All @@ -25,4 +25,6 @@ public interface JnaSupport {

Terminal winSysTerminal(String name, String type, boolean ansiPassThrough, Charset encoding, int codepage, boolean nativeSignals, Terminal.SignalHandler signalHandler, boolean paused) throws IOException;

boolean isWindowsConsole();

}

0 comments on commit 0de635b

Please sign in to comment.