jline

Class ConsoleReader

public class ConsoleReader extends Object implements ConsoleOperations

A reader for console applications. It supports custom tab-completion, saveable command history, and command line editing. On some platforms, platform-specific commands will need to be issued before the reader will function properly. See Terminal for convenience methods for issuing platform-specific setup commands.

Author: Marc Prud'hommeaux

Field Summary
static StringCR
static SortedMapKEYMAP_NAMES
Map that contains the operation name to keymay operation mapping.
Constructor Summary
ConsoleReader()
Create a new reader using FileDescriptor#in for input and System#out for output. FileDescriptor#in is used because it has a better chance of being unbuffered.
ConsoleReader(InputStream in, Writer out)
Create a new reader using the specified InputStream for input and the specific writer for output, using the default keybindings resource.
ConsoleReader(InputStream in, Writer out, InputStream bindings)
ConsoleReader(InputStream in, Writer out, InputStream bindings, Terminal term)
Create a new reader.
Method Summary
booleanaddCompletor(Completor completor)
Add the specified Completor to the list of handlers for tab-completion.
voidaddTriggeredAction(char c, ActionListener listener)
Adding a triggered Action allows to give another curse of action if a character passed the preprocessing.
booleanbackspace()
Issue a backspace.
voidbeep()
Issue an audible keyboard bell, if ConsoleReader return true.
booleanclearScreen()
Clear the screen by issuing the ANSI "clear screen" code.
static voiddebug(String str)
debug.
booleandelete()
Issue a delete.
voiddrawLine()
Output put the prompt + the current buffer
voidflushConsole()
Flush the console output stream.
intgetAutoprintThreshhold()
booleangetBellEnabled()
CompletionHandlergetCompletionHandler()
CollectiongetCompletors()
Returns an unmodifiable list of all the completors.
CursorBuffergetCursorBuffer()
StringgetDefaultPrompt()
The default prompt that will be issued.
CharactergetEchoCharacter()
Returns the echo character.
HistorygetHistory()
InputStreamgetInput()
Returns the stream used for console input.
intgetTermheight()
Query the terminal to find the current width;
TerminalgetTerminal()
Loads the key bindings.
intgetTermwidth()
Query the terminal to find the current width;
booleangetUseHistory()
Whether or not to add new commands to the history buffer.
booleangetUsePagination()
Whether to use pagination when the number of rows of candidates exceeds the height of the temrinal.
booleankillLine()
Kill the buffer ahead of the current cursor position.
intmoveCursor(int num)
Move the cursor where characters.
booleanpaste()
Paste the contents of the clipboard into the console buffer
voidprintColumns(Collection stuff)
Output the specified Collection in proper columns.
voidprintNewline()
Output a platform-dependant newline.
voidprintString(String str)
Output the specified string to the output stream (but not the buffer).
voidputString(String str)
Write out the specified string to the buffer and the output stream.
intreadCharacter(char[] allowed)
StringreadLine()
Read the next line and return the contents of the buffer.
StringreadLine(Character mask)
Read the next line with the specified character mask.
StringreadLine(String prompt)
StringreadLine(String prompt, Character mask)
Read a line from the in InputStream, and return the line (without any trailing newlines).
intreadVirtualKey()
Read a character from the console.
voidredrawLine()
Clear the line and redraw it.
booleanremoveCompletor(Completor completor)
Remove the specified Completor from the list of handlers for tab-completion.
booleanreplace(int num, String replacement)
voidsetAutoprintThreshhold(int autoprintThreshhold)
voidsetBellEnabled(boolean bellEnabled)
voidsetCompletionHandler(CompletionHandler completionHandler)
booleansetCursorPosition(int position)
Move the cursor position to the specified absolute index.
voidsetDebug(PrintWriter debugger)
Set the stream for debugging.
voidsetDefaultPrompt(String prompt)
The default prompt that will be issued.
voidsetEchoCharacter(Character echoCharacter)

Set the echo character.

voidsetHistory(History history)
voidsetInput(InputStream in)
Set the stream to be used for console input.
voidsetUseHistory(boolean useHistory)
Whether or not to add new commands to the history buffer.
voidsetUsePagination(boolean usePagination)
Whether to use pagination when the number of rows of candidates exceeds the height of the temrinal.

Field Detail

CR

public static final String CR

KEYMAP_NAMES

public static SortedMap KEYMAP_NAMES
Map that contains the operation name to keymay operation mapping.

Constructor Detail

ConsoleReader

public ConsoleReader()
Create a new reader using FileDescriptor#in for input and System#out for output. FileDescriptor#in is used because it has a better chance of being unbuffered.

ConsoleReader

public ConsoleReader(InputStream in, Writer out)
Create a new reader using the specified InputStream for input and the specific writer for output, using the default keybindings resource.

ConsoleReader

public ConsoleReader(InputStream in, Writer out, InputStream bindings)

ConsoleReader

public ConsoleReader(InputStream in, Writer out, InputStream bindings, Terminal term)
Create a new reader.

Parameters: in the input out the output bindings the key bindings to use term the terminal to use

Method Detail

addCompletor

public boolean addCompletor(Completor completor)
Add the specified Completor to the list of handlers for tab-completion.

Parameters: completor the Completor to add

Returns: true if it was successfully added

addTriggeredAction

public void addTriggeredAction(char c, ActionListener listener)
Adding a triggered Action allows to give another curse of action if a character passed the preprocessing. Say you want to close the application if the user enter q. addTriggerAction('q', new ActionListener(){ System.exit(0); }); would do the trick.

Parameters: c listener

backspace

public final boolean backspace()
Issue a backspace.

Returns: true if successful

beep

public final void beep()
Issue an audible keyboard bell, if ConsoleReader return true.

clearScreen

public boolean clearScreen()
Clear the screen by issuing the ANSI "clear screen" code.

debug

public static void debug(String str)
debug.

Parameters: str the message to issue.

delete

public final boolean delete()
Issue a delete.

Returns: true if successful

drawLine

public final void drawLine()
Output put the prompt + the current buffer

flushConsole

public final void flushConsole()
Flush the console output stream. This is important for printout out single characters (like a backspace or keyboard) that we want the console to handle immedately.

getAutoprintThreshhold

public int getAutoprintThreshhold()

Returns: the number of candidates to print without issing a warning.

getBellEnabled

public boolean getBellEnabled()

Returns: true is audible keyboard bell is enabled.

getCompletionHandler

public CompletionHandler getCompletionHandler()

getCompletors

public Collection getCompletors()
Returns an unmodifiable list of all the completors.

getCursorBuffer

public CursorBuffer getCursorBuffer()

getDefaultPrompt

public String getDefaultPrompt()
The default prompt that will be issued.

getEchoCharacter

public Character getEchoCharacter()
Returns the echo character.

getHistory

public History getHistory()

getInput

public InputStream getInput()
Returns the stream used for console input.

getTermheight

public int getTermheight()
Query the terminal to find the current width;

Returns: the height of the current terminal.

See Also: Terminal

getTerminal

public Terminal getTerminal()
Loads the key bindings. Bindings file is in the format: keycode: operation name

getTermwidth

public int getTermwidth()
Query the terminal to find the current width;

Returns: the width of the current terminal.

See Also: Terminal

getUseHistory

public boolean getUseHistory()
Whether or not to add new commands to the history buffer.

getUsePagination

public boolean getUsePagination()
Whether to use pagination when the number of rows of candidates exceeds the height of the temrinal.

killLine

public boolean killLine()
Kill the buffer ahead of the current cursor position.

Returns: true if successful

moveCursor

public final int moveCursor(int num)
Move the cursor where characters.

Parameters: where if less than 0, move abs(where) to the left, otherwise move where to the right.

Returns: the number of spaces we moved

paste

public boolean paste()
Paste the contents of the clipboard into the console buffer

Returns: true if clipboard contents pasted

printColumns

public void printColumns(Collection stuff)
Output the specified Collection in proper columns.

Parameters: stuff the stuff to print

printNewline

public final void printNewline()
Output a platform-dependant newline.

printString

public final void printString(String str)
Output the specified string to the output stream (but not the buffer).

putString

public final void putString(String str)
Write out the specified string to the buffer and the output stream.

readCharacter

public final int readCharacter(char[] allowed)

readLine

public String readLine()
Read the next line and return the contents of the buffer.

readLine

public String readLine(Character mask)
Read the next line with the specified character mask. If null, then characters will be echoed. If 0, then no characters will be echoed.

readLine

public String readLine(String prompt)

readLine

public String readLine(String prompt, Character mask)
Read a line from the in InputStream, and return the line (without any trailing newlines).

Parameters: prompt the prompt to issue to the console, may be null.

Returns: a line that is read from the terminal, or null if there was null input (e.g., CTRL-D was pressed).

readVirtualKey

public final int readVirtualKey()
Read a character from the console.

Returns: the character, or -1 if an EOF is received.

redrawLine

public final void redrawLine()
Clear the line and redraw it.

removeCompletor

public boolean removeCompletor(Completor completor)
Remove the specified Completor from the list of handlers for tab-completion.

Parameters: completor the Completor to remove

Returns: true if it was successfully removed

replace

public final boolean replace(int num, String replacement)

setAutoprintThreshhold

public void setAutoprintThreshhold(int autoprintThreshhold)

Parameters: autoprintThreshhold the number of candidates to print without issuing a warning.

setBellEnabled

public void setBellEnabled(boolean bellEnabled)

Parameters: bellEnabled if true, enable audible keyboard bells if an alert is required.

setCompletionHandler

public void setCompletionHandler(CompletionHandler completionHandler)

setCursorPosition

public final boolean setCursorPosition(int position)
Move the cursor position to the specified absolute index.

setDebug

public void setDebug(PrintWriter debugger)
Set the stream for debugging. Development use only.

setDefaultPrompt

public void setDefaultPrompt(String prompt)
The default prompt that will be issued.

setEchoCharacter

public void setEchoCharacter(Character echoCharacter)

Set the echo character. For example, to have "*" entered when a password is typed:

 myConsoleReader.setEchoCharacter(new Character('*'));
 

Setting the character to

 null
 
will restore normal character echoing. Setting the character to
 new Character(0)
 
will cause nothing to be echoed.

Parameters: echoCharacter the character to echo to the console in place of the typed character.

setHistory

public void setHistory(History history)

setInput

public void setInput(InputStream in)
Set the stream to be used for console input.

setUseHistory

public void setUseHistory(boolean useHistory)
Whether or not to add new commands to the history buffer.

setUsePagination

public void setUsePagination(boolean usePagination)
Whether to use pagination when the number of rows of candidates exceeds the height of the temrinal.
Copyright © 2002-2008 null. All Rights Reserved.