Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows: Use virtual terminal processing if available #177

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.io.BufferedWriter;
import java.io.IOError;
import java.io.IOException;
import java.io.Writer;
import java.nio.charset.Charset;
import java.util.function.IntConsumer;

Expand All @@ -23,25 +24,45 @@
import org.jline.terminal.Size;
import org.jline.terminal.impl.AbstractWindowsTerminal;
import org.jline.utils.InfoCmp;
import org.jline.utils.Log;

import static org.fusesource.jansi.internal.Kernel32.GetConsoleScreenBufferInfo;
import static org.fusesource.jansi.internal.Kernel32.GetStdHandle;
import static org.fusesource.jansi.internal.Kernel32.STD_OUTPUT_HANDLE;

public class JansiWinSysTerminal extends AbstractWindowsTerminal {

private static final int ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004;

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

public JansiWinSysTerminal(String name, Charset encoding, int codepage, boolean nativeSignals, SignalHandler signalHandler) throws IOException {
super(new WindowsAnsiWriter(new BufferedWriter(new JansiWinConsoleWriter())),
super(createAnsiWriter(new BufferedWriter(new JansiWinConsoleWriter())),
name, encoding, codepage, nativeSignals, signalHandler);

// Start input pump thread
pump.start();
}

private static Writer createAnsiWriter(Writer writer) throws IOException {
long console = GetStdHandle(STD_OUTPUT_HANDLE);

int[] mode = new int[1];
if (Kernel32.GetConsoleMode(console, mode) == 0) {
throw new IOException("Failed to get console mode: " + WindowsSupport.getLastErrorMessage());
}

if (Kernel32.SetConsoleMode(console, mode[0] | ENABLE_VIRTUAL_TERMINAL_PROCESSING) != 0) {
return writer;
}

Log.debug("Unable to enable virtual terminal processing, using AnsiWriter instead: " + WindowsSupport.getLastErrorMessage());
return new WindowsAnsiWriter(writer);
}


@Override
protected int getConsoleOutputCP() {
return Kernel32.GetConsoleOutputCP();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,18 @@

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.Writer;
import java.nio.charset.Charset;
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;
import org.jline.terminal.Size;
import org.jline.terminal.impl.AbstractWindowsTerminal;
import org.jline.utils.InfoCmp;
import org.jline.utils.Log;

public class JnaWinSysTerminal extends AbstractWindowsTerminal {

Expand All @@ -30,14 +33,28 @@ public JnaWinSysTerminal(String name, boolean nativeSignals) throws IOException
}

public JnaWinSysTerminal(String name, Charset encoding, int codepage, boolean nativeSignals, SignalHandler signalHandler) throws IOException {
super(new WindowsAnsiWriter(new BufferedWriter(new JnaWinConsoleWriter(consoleOut)), consoleOut),
super(createAnsiWriter(new BufferedWriter(new JnaWinConsoleWriter(consoleOut))),
name, encoding, codepage, nativeSignals, signalHandler);
strings.put(InfoCmp.Capability.key_mouse, "\\E[M");

// Start input pump thread
pump.start();
}

private static Writer createAnsiWriter(Writer writer) throws IOException {
IntByReference mode = new IntByReference();
Kernel32.INSTANCE.GetConsoleMode(consoleOut, mode);

try {
Kernel32.INSTANCE.SetConsoleMode(consoleOut, mode.getValue() | Kernel32.ENABLE_VIRTUAL_TERMINAL_PROCESSING);
return writer;
} catch (LastErrorException e) {
Log.debug("Unable to enable virtual terminal processing, using AnsiWriter instead", e);
}

return new WindowsAnsiWriter(writer, consoleOut);
}

@Override
protected int getConsoleOutputCP() {
return Kernel32.INSTANCE.GetConsoleOutputCP();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ interface Kernel32 extends StdCallLibrary {
int STD_OUTPUT_HANDLE = -11;
int STD_ERROR_HANDLE = -12;

// Input modes
int ENABLE_PROCESSED_INPUT = 0x0001;
int ENABLE_LINE_INPUT = 0x0002;
int ENABLE_ECHO_INPUT = 0x0004;
Expand All @@ -36,6 +37,9 @@ interface Kernel32 extends StdCallLibrary {
int ENABLE_QUICK_EDIT_MODE = 0x0040;
int ENABLE_EXTENDED_FLAGS = 0x0080;

// Output modes
int ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004;

int RIGHT_ALT_PRESSED = 0x0001;
int LEFT_ALT_PRESSED = 0x0002;
int RIGHT_CTRL_PRESSED = 0x0004;
Expand Down