| 1 |
/* |
| 2 |
* Copyright (c) 2002-2017, the original author or authors. |
| 3 |
* |
| 4 |
* This software is distributable under the BSD license. See the terms of the |
| 5 |
* BSD license in the documentation provided with this software. |
| 6 |
* |
| 7 |
* http://www.opensource.org/licenses/bsd-license.php |
| 8 |
*/ |
| 9 |
package org.jline.terminal.impl.jansi; |
| 10 |
|
| 11 |
import org.fusesource.jansi.Ansi; |
| 12 |
import org.jline.terminal.Attributes; |
| 13 |
import org.jline.terminal.Size; |
| 14 |
import org.jline.terminal.Terminal; |
| 15 |
import org.jline.terminal.impl.jansi.freebsd.FreeBsdNativePty; |
| 16 |
import org.jline.terminal.impl.jansi.linux.LinuxNativePty; |
| 17 |
import org.jline.terminal.impl.jansi.osx.OsXNativePty; |
| 18 |
import org.jline.terminal.impl.jansi.win.JansiWinSysTerminal; |
| 19 |
import org.jline.terminal.spi.JansiSupport; |
| 20 |
import org.jline.terminal.spi.Pty; |
| 21 |
|
| 22 |
import java.io.IOException; |
| 23 |
import java.util.regex.Matcher; |
| 24 |
import java.util.regex.Pattern; |
| 25 |
|
| 26 |
public class JansiSupportImpl implements JansiSupport { |
| 27 |
|
| 28 |
static final int JANSI_MAJOR_VERSION; |
| 29 |
static final int JANSI_MINOR_VERSION; |
| 30 |
static { |
| 31 |
int major = 0, minor = 0; |
| 32 |
try { |
| 33 |
String v = Ansi.class.getPackage().getImplementationVersion(); |
| 34 |
if (v != null) { |
| 35 |
Matcher m = Pattern.compile("([0-9]+)\\.([0-9]+)([\\.-]\\S+)?").matcher(v); |
| 36 |
if (m.matches()) { |
| 37 |
major = Integer.parseInt(m.group(1)); |
| 38 |
minor = Integer.parseInt(m.group(2)); |
| 39 |
} |
| 40 |
} |
| 41 |
} catch (Throwable t) { |
| 42 |
// Ignore |
| 43 |
} |
| 44 |
JANSI_MAJOR_VERSION = major; |
| 45 |
JANSI_MINOR_VERSION = minor; |
| 46 |
} |
| 47 |
|
| 48 |
@Override |
| 49 |
public Pty current() throws IOException { |
| 50 |
String osName = System.getProperty("os.name"); |
| 51 |
if (osName.startsWith("Linux")) { |
| 52 |
if (JANSI_MAJOR_VERSION > 1 || JANSI_MAJOR_VERSION == 1 && JANSI_MINOR_VERSION >= 16) { |
| 53 |
return LinuxNativePty.current(); |
| 54 |
} |
| 55 |
} |
| 56 |
else if (osName.startsWith("Mac") || osName.startsWith("Darwin")) { |
| 57 |
if (JANSI_MAJOR_VERSION > 1 || JANSI_MAJOR_VERSION == 1 && JANSI_MINOR_VERSION >= 12) { |
| 58 |
return OsXNativePty.current(); |
| 59 |
} |
| 60 |
} |
| 61 |
else if (osName.startsWith("Solaris") || osName.startsWith("SunOS")) { |
| 62 |
// Solaris is not supported by jansi |
| 63 |
// return SolarisNativePty.current(); |
| 64 |
} |
| 65 |
else if (osName.startsWith("FreeBSD")) { |
| 66 |
if (JANSI_MAJOR_VERSION > 1 || JANSI_MAJOR_VERSION == 1 && JANSI_MINOR_VERSION >= 16) { |
| 67 |
return FreeBsdNativePty.current(); |
| 68 |
} |
| 69 |
} |
| 70 |
throw new UnsupportedOperationException(); |
| 71 |
} |
| 72 |
|
| 73 |
@Override |
| 74 |
public Pty open(Attributes attributes, Size size) throws IOException { |
| 75 |
if (JANSI_MAJOR_VERSION > 1 || JANSI_MAJOR_VERSION == 1 && JANSI_MINOR_VERSION >= 16) { |
| 76 |
String osName = System.getProperty("os.name"); |
| 77 |
if (osName.startsWith("Linux")) { |
| 78 |
return LinuxNativePty.open(attributes, size); |
| 79 |
} |
| 80 |
else if (osName.startsWith("Mac") || osName.startsWith("Darwin")) { |
| 81 |
return OsXNativePty.open(attributes, size); |
| 82 |
} |
| 83 |
else if (osName.startsWith("Solaris") || osName.startsWith("SunOS")) { |
| 84 |
// Solaris is not supported by jansi |
| 85 |
// return SolarisNativePty.current(); |
| 86 |
} |
| 87 |
else if (osName.startsWith("FreeBSD")) { |
| 88 |
return FreeBsdNativePty.open(attributes, size); |
| 89 |
} |
| 90 |
} |
| 91 |
throw new UnsupportedOperationException(); |
| 92 |
} |
| 93 |
|
| 94 |
@Override |
| 95 |
public Terminal winSysTerminal(String name, boolean nativeSignals, Terminal.SignalHandler signalHandler) throws IOException { |
| 96 |
if (JANSI_MAJOR_VERSION > 1 || JANSI_MAJOR_VERSION == 1 && JANSI_MINOR_VERSION >= 12) { |
| 97 |
JansiWinSysTerminal terminal = new JansiWinSysTerminal(name, nativeSignals, signalHandler); |
| 98 |
if (JANSI_MAJOR_VERSION == 1 && JANSI_MINOR_VERSION < 16) { |
| 99 |
terminal.disableScrolling(); |
| 100 |
} |
| 101 |
return terminal; |
| 102 |
} |
| 103 |
throw new UnsupportedOperationException(); |
| 104 |
} |
| 105 |
|
| 106 |
} |