jline

Class WindowsTerminal

Implemented Interfaces:
ConsoleOperations

public class WindowsTerminal
extends Terminal

Terminal implementation for Microsoft Windows. Terminal initialization in initializeTerminal() is accomplished by extracting the jline_version.dll, saving it to the system temporary directoy (determined by the setting of the java.io.tmpdir System property), loading the library, and then calling the Win32 APIs SetConsoleMode and GetConsoleMode to disable character echoing.

By default, the readCharacter(InputStream) method will attempt to test to see if the specified InputStream is System.in or a wrapper around FileDescriptor.in, and if so, will bypass the character reading to directly invoke the readc() method in the JNI library. This is so the class can read special keys (like arrow keys) which are otherwise inaccessible via the System.in stream. Using JNI reading can be bypassed by setting the jline.WindowsTerminal.disableDirectConsole system property to true.

Author:
Marc Prud'hommeaux

Nested Class Summary

(package private) static class
WindowsTerminal.ReplayPrefixOneCharInputStream
This is awkward and inefficient, but probably the minimal way to add UTF-8 support to JLine

Field Summary

static int
DELETE_KEY
When following the SPECIAL_KEY_INDICATOR or NUMPAD_KEY_INDICATOR this character indicates that the delete key was pressed.
static int
DOWN_ARROW_KEY
When following the SPECIAL_KEY_INDICATOR or NUMPAD_KEY_INDICATOR this character indicates an down arrow key press.
private static int
ENABLE_ECHO_INPUT
Characters read by the ReadFile or ReadConsole function are written to the active screen buffer as they are read.
private static int
ENABLE_LINE_INPUT
The ReadFile or ReadConsole function returns only when a carriage return character is read.
private static int
ENABLE_MOUSE_INPUT
If the mouse pointer is within the borders of the console window and the window has the keyboard focus, mouse events generated by mouse movement and button presses are placed in the input buffer.
private static int
ENABLE_PROCESSED_INPUT
CTRL+C is processed by the system and is not placed in the input buffer.
private static int
ENABLE_PROCESSED_OUTPUT
When enabled, text entered in a console window will be inserted at the current cursor location and all text following that location will not be overwritten.
private static int
ENABLE_WINDOW_INPUT
User interactions that change the size of the console screen buffer are reported in the console's input buffee.
private static int
ENABLE_WRAP_AT_EOL_OUTPUT
This flag enables the user to use the mouse to select and edit text.
static char
END_KEY
When following the SPECIAL_KEY_INDICATOR or NUMPAD_KEY_INDICATOR this character indicates that the end key was pressed.
static char
ESCAPE_KEY
When following the SPECIAL_KEY_INDICATOR or NUMPAD_KEY_INDICATOR, this character indicates that the escape key was pressed.
static int
HOME_KEY
When following the SPECIAL_KEY_INDICATOR or NUMPAD_KEY_INDICATOR this character indicates that the home key was pressed.
static char
INSERT_KEY
When following the SPECIAL_KEY_INDICATOR or NUMPAD_KEY_INDICATOR this character indicates that the insert key was pressed.
static int
LEFT_ARROW_KEY
When following the SPECIAL_KEY_INDICATOR or NUMPAD_KEY_INDICATOR, this character indicates an left arrow key press.
static int
NUMPAD_KEY_INDICATOR
On windows terminals, this character indicates that a special key on the number pad has been pressed.
static char
PAGE_DOWN_KEY
When following the SPECIAL_KEY_INDICATOR or NUMPAD_KEY_INDICATOR this character indicates that the page down key was pressed.
static char
PAGE_UP_KEY
When following the SPECIAL_KEY_INDICATOR or NUMPAD_KEY_INDICATOR this character indicates that the page up key was pressed.
static int
RIGHT_ARROW_KEY
When following the SPECIAL_KEY_INDICATOR or NUMPAD_KEY_INDICATOR this character indicates an right arrow key press.
static int
SPECIAL_KEY_INDICATOR
On windows terminals, this character indicates that a 'special' key has been pressed.
static int
UP_ARROW_KEY
When following the SPECIAL_KEY_INDICATOR or NUMPAD_KEY_INDICATOR this character indicates an up arrow key press.
private Boolean
directConsole
private boolean
echoEnabled
(package private) String
encoding
(package private) InputStreamReader
replayReader
(package private) UnixTerminal.ReplayPrefixOneCharInputStream
replayStream

Fields inherited from class jline.Terminal

term

Fields inherited from interface jline.ConsoleOperations

ADD, BACKSPACE, CHANGE_CASE, CHANGE_META, CLEAR_LINE, CLEAR_SCREEN, COMPLETE, CR, CTRL_A, CTRL_B, CTRL_C, CTRL_D, CTRL_E, CTRL_F, CTRL_K, CTRL_L, CTRL_N, CTRL_OB, CTRL_P, CTRL_QM, DELETE, DELETE_META, DELETE_NEXT_CHAR, DELETE_PREV_CHAR, DELETE_PREV_WORD, END_OF_HISTORY, END_WORD, EXIT, INSERT, KEYBOARD_BELL, KILL_LINE, KILL_LINE_PREV, MOVE_TO_BEG, MOVE_TO_END, NEWLINE, NEXT_CHAR, NEXT_HISTORY, NEXT_SPACE_WORD, NEXT_WORD, PASTE, PASTE_NEXT, PASTE_PREV, PREV_CHAR, PREV_HISTORY, PREV_SPACE_WORD, PREV_WORD, REDISPLAY, REPEAT_NEXT_CHAR, REPEAT_PREV_CHAR, REPEAT_SEARCH_NEXT, REPEAT_SEARCH_PREV, REPLACE_CHAR, REPLACE_MODE, RESET_LINE, SEARCH_NEXT, SEARCH_PREV, START_OF_HISTORY, SUBSTITUTE_CHAR, SUBSTITUTE_LINE, TO_END_WORD, TO_NEXT_CHAR, TO_PREV_CHAR, UNDO, UNKNOWN

Constructor Summary

WindowsTerminal()

Method Summary

private void
consumeException(Throwable e)
No-op for exceptions we want to silently consume.
void
disableEcho()
void
enableEcho()
private int
getConsoleMode()
InputStream
getDefaultBindings()
Boolean
getDirectConsole()
Whether or not to allow the use of the JNI console interaction.
boolean
getEcho()
int
getTerminalHeight()
Unsupported; return the default.
int
getTerminalWidth()
Unsupported; return the default.
private int
getWindowsTerminalHeight()
private int
getWindowsTerminalWidth()
void
initializeTerminal()
boolean
isANSISupported()
Windows doesn't support ANSI codes by default; disable them.
boolean
isEchoEnabled()
boolean
isSupported()
private void
loadLibrary(String name)
private int
readByte()
int
readCharacter(InputStream in)
int
readVirtualKey(InputStream in)
private void
setConsoleMode(int mode)
void
setDirectConsole(Boolean directConsole)
Whether or not to allow the use of the JNI console interaction.

Methods inherited from class jline.Terminal

afterReadLine, beforeReadLine, disableEcho, enableEcho, getDefaultBindings, getEcho, getTerminal, getTerminalHeight, getTerminalWidth, initializeTerminal, isANSISupported, isEchoEnabled, isSupported, readCharacter, readVirtualKey, resetTerminal, setupTerminal

Field Details

DELETE_KEY

public static final int DELETE_KEY
When following the SPECIAL_KEY_INDICATOR or NUMPAD_KEY_INDICATOR this character indicates that the delete key was pressed.
Field Value:
83

DOWN_ARROW_KEY

public static final int DOWN_ARROW_KEY
When following the SPECIAL_KEY_INDICATOR or NUMPAD_KEY_INDICATOR this character indicates an down arrow key press.
Field Value:
80

ENABLE_ECHO_INPUT

private static final int ENABLE_ECHO_INPUT
Characters read by the ReadFile or ReadConsole function are written to the active screen buffer as they are read. This mode can be used only if the ENABLE_LINE_INPUT mode is also enabled.
Field Value:
4

ENABLE_LINE_INPUT

private static final int ENABLE_LINE_INPUT
The ReadFile or ReadConsole function returns only when a carriage return character is read. If this mode is disable, the functions return when one or more characters are available.
Field Value:
2

ENABLE_MOUSE_INPUT

private static final int ENABLE_MOUSE_INPUT
If the mouse pointer is within the borders of the console window and the window has the keyboard focus, mouse events generated by mouse movement and button presses are placed in the input buffer. These events are discarded by ReadFile or ReadConsole, even when this mode is enabled.
Field Value:
16

ENABLE_PROCESSED_INPUT

private static final int ENABLE_PROCESSED_INPUT
CTRL+C is processed by the system and is not placed in the input buffer. If the input buffer is being read by ReadFile or ReadConsole, other control keys are processed by the system and are not returned in the ReadFile or ReadConsole buffer. If the ENABLE_LINE_INPUT mode is also enabled, backspace, carriage return, and linefeed characters are handled by the system.
Field Value:
1

ENABLE_PROCESSED_OUTPUT

private static final int ENABLE_PROCESSED_OUTPUT
When enabled, text entered in a console window will be inserted at the current cursor location and all text following that location will not be overwritten. When disabled, all following text will be overwritten. An OR operation must be performed with this flag and the ENABLE_EXTENDED_FLAGS flag to enable this functionality.
Field Value:
1

ENABLE_WINDOW_INPUT

private static final int ENABLE_WINDOW_INPUT
User interactions that change the size of the console screen buffer are reported in the console's input buffee. Information about these events can be read from the input buffer by applications using theReadConsoleInput function, but not by those using ReadFile orReadConsole.
Field Value:
8

ENABLE_WRAP_AT_EOL_OUTPUT

private static final int ENABLE_WRAP_AT_EOL_OUTPUT
This flag enables the user to use the mouse to select and edit text. To enable this option, use the OR to combine this flag with ENABLE_EXTENDED_FLAGS.
Field Value:
2

END_KEY

public static final char END_KEY
When following the SPECIAL_KEY_INDICATOR or NUMPAD_KEY_INDICATOR this character indicates that the end key was pressed.
Field Value:
'O'

ESCAPE_KEY

public static final char ESCAPE_KEY
When following the SPECIAL_KEY_INDICATOR or NUMPAD_KEY_INDICATOR, this character indicates that the escape key was pressed.
Field Value:
'\u0000'

HOME_KEY

public static final int HOME_KEY
When following the SPECIAL_KEY_INDICATOR or NUMPAD_KEY_INDICATOR this character indicates that the home key was pressed.
Field Value:
71

INSERT_KEY

public static final char INSERT_KEY
When following the SPECIAL_KEY_INDICATOR or NUMPAD_KEY_INDICATOR this character indicates that the insert key was pressed.
Field Value:
'R'

LEFT_ARROW_KEY

public static final int LEFT_ARROW_KEY
When following the SPECIAL_KEY_INDICATOR or NUMPAD_KEY_INDICATOR, this character indicates an left arrow key press.
Field Value:
75

NUMPAD_KEY_INDICATOR

public static final int NUMPAD_KEY_INDICATOR
On windows terminals, this character indicates that a special key on the number pad has been pressed.
Field Value:
0

PAGE_DOWN_KEY

public static final char PAGE_DOWN_KEY
When following the SPECIAL_KEY_INDICATOR or NUMPAD_KEY_INDICATOR this character indicates that the page down key was pressed.
Field Value:
'Q'

PAGE_UP_KEY

public static final char PAGE_UP_KEY
When following the SPECIAL_KEY_INDICATOR or NUMPAD_KEY_INDICATOR this character indicates that the page up key was pressed.
Field Value:
'I'

RIGHT_ARROW_KEY

public static final int RIGHT_ARROW_KEY
When following the SPECIAL_KEY_INDICATOR or NUMPAD_KEY_INDICATOR this character indicates an right arrow key press.
Field Value:
77

SPECIAL_KEY_INDICATOR

public static final int SPECIAL_KEY_INDICATOR
On windows terminals, this character indicates that a 'special' key has been pressed. This means that a key such as an arrow key, or delete, or home, etc. will be indicated by the next character.
Field Value:
224

UP_ARROW_KEY

public static final int UP_ARROW_KEY
When following the SPECIAL_KEY_INDICATOR or NUMPAD_KEY_INDICATOR this character indicates an up arrow key press.
Field Value:
72

directConsole

private Boolean directConsole

echoEnabled

private boolean echoEnabled

encoding

(package private)  String encoding

replayReader

(package private)  InputStreamReader replayReader

replayStream

(package private)  UnixTerminal.ReplayPrefixOneCharInputStream replayStream

Constructor Details

WindowsTerminal

public WindowsTerminal()

Method Details

consumeException

private void consumeException(Throwable e)
No-op for exceptions we want to silently consume.

disableEcho

public void disableEcho()
Overrides:
disableEcho in interface Terminal

enableEcho

public void enableEcho()
Overrides:
enableEcho in interface Terminal

getConsoleMode

private int getConsoleMode()

getDefaultBindings

public InputStream getDefaultBindings()
Overrides:
getDefaultBindings in interface Terminal

getDirectConsole

public Boolean getDirectConsole()
Whether or not to allow the use of the JNI console interaction.

getEcho

public boolean getEcho()
Overrides:
getEcho in interface Terminal

getTerminalHeight

public int getTerminalHeight()
Unsupported; return the default.
Overrides:
getTerminalHeight in interface Terminal

getTerminalWidth

public int getTerminalWidth()
Unsupported; return the default.
Overrides:
getTerminalWidth in interface Terminal

getWindowsTerminalHeight

private int getWindowsTerminalHeight()

getWindowsTerminalWidth

private int getWindowsTerminalWidth()

initializeTerminal

public void initializeTerminal()
            throws Exception
Overrides:
initializeTerminal in interface Terminal

isANSISupported

public boolean isANSISupported()
Windows doesn't support ANSI codes by default; disable them.
Overrides:
isANSISupported in interface Terminal

isEchoEnabled

public boolean isEchoEnabled()
Overrides:
isEchoEnabled in interface Terminal

isSupported

public boolean isSupported()
Overrides:
isSupported in interface Terminal

loadLibrary

private void loadLibrary(String name)
            throws IOException

readByte

private int readByte()

readCharacter

public int readCharacter(InputStream in)
            throws IOException
Overrides:
readCharacter in interface Terminal

readVirtualKey

public int readVirtualKey(InputStream in)
            throws IOException
Overrides:
readVirtualKey in interface Terminal

setConsoleMode

private void setConsoleMode(int mode)

setDirectConsole

public void setDirectConsole(Boolean directConsole)
Whether or not to allow the use of the JNI console interaction.