public class

ConsoleReader

extends Object
java.lang.Object
   ↳ jline.console.ConsoleReader

Class Overview

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 init() for convenience methods for issuing platform-specific setup commands.

Summary

Constants
char BACKSPACE
String DEFAULT_INPUT_RC
String INPUT_RC
String JLINE_COMPLETION_THRESHOLD
String JLINE_ESC_TIMEOUT
String JLINE_INPUTRC
String JLINE_NOBELL
char KEYBOARD_BELL
char NULL_MASK
char RESET_LINE
int TAB_WIDTH
Fields
public static final String CR
Public Constructors
ConsoleReader()
ConsoleReader(InputStream in, OutputStream out)
ConsoleReader(InputStream in, OutputStream out, Terminal term)
ConsoleReader(String appName, InputStream in, OutputStream out, Terminal term)
ConsoleReader(String appName, InputStream in, OutputStream out, Terminal term, String encoding)
Public Methods
String accept()
The equivalent of hitting <RET>.
boolean addCompleter(Completer completer)
Add the specified Completer to the list of handlers for tab-completion.
void addTriggeredAction(char c, ActionListener listener)
Adding a triggered Action allows to give another curse of action if a character passed the pre-processing.
boolean backspace()
Issue a backspace.
void beep()
Issue an audible keyboard bell.
boolean clearScreen()
Clear the screen by issuing the ANSI "clear screen" code.
final boolean delete()
Issue a delete.
final void drawLine()
Output put the prompt + the current buffer
void flush()
Flush the console output stream.
int getAutoprintThreshold()
boolean getBellEnabled()
Get whether the console bell is enabled
String getCommentBegin()
Collection<Completer> getCompleters()
Returns an unmodifiable list of all the completers.
CompletionHandler getCompletionHandler()
CursorBuffer getCursorBuffer()
Character getEchoCharacter()
Returns the echo character.
boolean getExpandEvents()
boolean getHandleUserInterrupt()
Get whether user interrupt handling is enabled
History getHistory()
InputStream getInput()
String getKeyMap()
Returns the name of the current key mapping.
KeyMap getKeys()
Writer getOutput()
String getPrompt()
Terminal getTerminal()
boolean isCopyPasteDetectionEnabled()
boolean isHistoryEnabled()
Whether or not to add new commands to the history buffer.
boolean isKeyMap(String name)
boolean isPaginationEnabled()
Whether to use pagination when the number of rows of candidates exceeds the height of the terminal.
boolean killLine()
Kill the buffer ahead of the current cursor position.
int moveCursor(int num)
Move the cursor where characters.
boolean paste()
Paste the contents of the clipboard into the console buffer
final void print(CharSequence s)
Output the specified string to the output stream (but not the buffer).
void printColumns(Collection<? extends CharSequence> items)
Output the specified Collection in proper columns.
void printForwardSearchStatus(String searchTerm, String match)
void printSearchStatus(String searchTerm, String match)
final void println()
Output a platform-dependant newline.
final void println(CharSequence s)
final void putString(CharSequence str)
Write out the specified string to the buffer and the output stream.
final int readCharacter(char... allowed)
final int readCharacter()
Read a character from the console.
String readLine(String prompt, Character mask)
Read a line from the in InputStream, and return the line (without any trailing newlines).
String readLine(Character mask)
Read the next line with the specified character mask.
String readLine(String prompt)
String readLine()
Read the next line and return the contents of the buffer.
final void redrawLine()
Clear the line and redraw it.
boolean removeCompleter(Completer completer)
Remove the specified Completer from the list of handlers for tab-completion.
final boolean replace(int num, String replacement)
void resetPromptLine(String prompt, String buffer, int cursorDest)
Erases the current line with the existing prompt, then redraws the line with the provided prompt and buffer
void restoreLine(String originalPrompt, int cursorDest)
int searchBackwards(String searchTerm, int startIndex, boolean startsWith)
int searchBackwards(String searchTerm, int startIndex)
Search backward in history from a given position.
int searchBackwards(String searchTerm)
Search backwards in history from the current position.
int searchForwards(String searchTerm, int startIndex, boolean startsWith)
int searchForwards(String searchTerm)
Search forwards in history from the current position.
int searchForwards(String searchTerm, int startIndex)
Search forward in history from a given position.
void setAutoprintThreshold(int threshold)
void setBellEnabled(boolean enabled)
Set whether the console bell is enabled.
void setCommentBegin(String commentBegin)
Sets the string that will be used to start a comment when the insert-comment key is struck.
void setCompletionHandler(CompletionHandler handler)
void setCopyPasteDetection(boolean onoff)
Enables or disables copy and paste detection.
final boolean setCursorPosition(int position)
Move the cursor position to the specified absolute index.
void setEchoCharacter(Character c)
Set the echo character.
void setExpandEvents(boolean expand)
void setHandleUserInterrupt(boolean enabled)
Set whether user interrupts (ctrl-C) are handled by having JLine throw UserInterruptException from readLine().
void setHistory(History history)
void setHistoryEnabled(boolean enabled)
Whether or not to add new commands to the history buffer.
boolean setKeyMap(String name)
Sets the current keymap by name.
void setPaginationEnabled(boolean enabled)
Whether to use pagination when the number of rows of candidates exceeds the height of the terminal.
void setParenBlinkTimeout(int timeout)
void setPrompt(String prompt)
void shutdown()
Shuts the console reader down.
boolean yank()
boolean yankPop()
Protected Methods
void back(int num)
Move the visual cursor backwards without modifying the buffer cursor.
boolean complete()
Use the completers to modify the buffer with the appropriate completions.
String expandEvents(String str)
Expand event designator such as !!, !#, !3, etc...
void finalize()
Shuts down the ConsoleReader if the JVM attempts to clean it up.
boolean moveToEnd()
void printCompletionCandidates()
final boolean resetLine()
Erase the current line.
[Expand]
Inherited Methods
From class java.lang.Object

Constants

public static final char BACKSPACE

Constant Value: 8 (0x00000008)

public static final String DEFAULT_INPUT_RC

Constant Value: "/etc/inputrc"

public static final String INPUT_RC

Constant Value: ".inputrc"

public static final String JLINE_COMPLETION_THRESHOLD

Constant Value: "jline.completion.threshold"

public static final String JLINE_ESC_TIMEOUT

Constant Value: "jline.esc.timeout"

public static final String JLINE_INPUTRC

Constant Value: "jline.inputrc"

public static final String JLINE_NOBELL

Constant Value: "jline.nobell"

public static final char KEYBOARD_BELL

Constant Value: 7 (0x00000007)

public static final char NULL_MASK

Constant Value: 0 (0x00000000)

public static final char RESET_LINE

Constant Value: 13 (0x0000000d)

public static final int TAB_WIDTH

Constant Value: 4 (0x00000004)

Fields

public static final String CR

Public Constructors

public ConsoleReader ()

Throws
IOException

public ConsoleReader (InputStream in, OutputStream out)

Throws
IOException

public ConsoleReader (InputStream in, OutputStream out, Terminal term)

Throws
IOException

public ConsoleReader (String appName, InputStream in, OutputStream out, Terminal term)

Throws
IOException

public ConsoleReader (String appName, InputStream in, OutputStream out, Terminal term, String encoding)

Throws
IOException

Public Methods

public String accept ()

The equivalent of hitting <RET>. The line is considered complete and is returned.

Returns
  • The completed line of text.
Throws
IOException

public boolean addCompleter (Completer completer)

Add the specified Completer to the list of handlers for tab-completion.

Parameters
completer the Completer to add
Returns
  • true if it was successfully added

public void addTriggeredAction (char c, ActionListener listener)

Adding a triggered Action allows to give another curse of action if a character passed the pre-processing.

Say you want to close the application if the user enter q. addTriggerAction('q', new ActionListener(){ System.exit(0); }); would do the trick.

public boolean backspace ()

Issue a backspace.

Returns
  • true if successful
Throws
IOException

public void beep ()

Issue an audible keyboard bell.

Throws
IOException

public boolean clearScreen ()

Clear the screen by issuing the ANSI "clear screen" code.

Throws
IOException

public final boolean delete ()

Issue a delete.

Returns
  • true if successful
Throws
IOException

public final void drawLine ()

Output put the prompt + the current buffer

Throws
IOException

public void flush ()

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 immediately.

Throws
IOException

public int getAutoprintThreshold ()

Returns
  • the number of candidates to print without issuing a warning.

public boolean getBellEnabled ()

Get whether the console bell is enabled

Returns
  • true if enabled; false otherwise

public String getCommentBegin ()

Returns
  • the string that will be used to start a comment when the insert-comment key is struck.

public Collection<Completer> getCompleters ()

Returns an unmodifiable list of all the completers.

public CompletionHandler getCompletionHandler ()

public CursorBuffer getCursorBuffer ()

public Character getEchoCharacter ()

Returns the echo character.

public boolean getExpandEvents ()

public boolean getHandleUserInterrupt ()

Get whether user interrupt handling is enabled

Returns
  • true if enabled; false otherwise

public History getHistory ()

public InputStream getInput ()

public String getKeyMap ()

Returns the name of the current key mapping.

Returns
  • the name of the key mapping. This will be the canonical name of the current mode of the key map and may not reflect the name that was used with setKeyMap(String).

public KeyMap getKeys ()

public Writer getOutput ()

public String getPrompt ()

public Terminal getTerminal ()

public boolean isCopyPasteDetectionEnabled ()

Returns
  • true if copy and paste detection is enabled.

public boolean isHistoryEnabled ()

Whether or not to add new commands to the history buffer.

public boolean isKeyMap (String name)

public boolean isPaginationEnabled ()

Whether to use pagination when the number of rows of candidates exceeds the height of the terminal.

public boolean killLine ()

Kill the buffer ahead of the current cursor position.

Returns
  • true if successful
Throws
IOException

public int moveCursor (int num)

Move the cursor where characters.

Parameters
num If less than 0, move abs(where) to the left, otherwise move where to the right.
Returns
  • The number of spaces we moved
Throws
IOException

public boolean paste ()

Paste the contents of the clipboard into the console buffer

Returns
  • true if clipboard contents pasted
Throws
IOException

public final void print (CharSequence s)

Output the specified string to the output stream (but not the buffer).

Throws
IOException

public void printColumns (Collection<? extends CharSequence> items)

Output the specified Collection in proper columns.

Throws
IOException

public void printForwardSearchStatus (String searchTerm, String match)

Throws
IOException

public void printSearchStatus (String searchTerm, String match)

Throws
IOException

public final void println ()

Output a platform-dependant newline.

Throws
IOException

public final void println (CharSequence s)

Throws
IOException

public final void putString (CharSequence str)

Write out the specified string to the buffer and the output stream.

Throws
IOException

public final int readCharacter (char... allowed)

Throws
IOException

public final int readCharacter ()

Read a character from the console.

Returns
  • the character, or -1 if an EOF is received.
Throws
IOException

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).
Throws
IOException

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.

Throws
IOException

public String readLine (String prompt)

Throws
IOException

public String readLine ()

Read the next line and return the contents of the buffer.

Throws
IOException

public final void redrawLine ()

Clear the line and redraw it.

Throws
IOException

public boolean removeCompleter (Completer completer)

Remove the specified Completer from the list of handlers for tab-completion.

Parameters
completer The Completer to remove
Returns
  • True if it was successfully removed

public final boolean replace (int num, String replacement)

public void resetPromptLine (String prompt, String buffer, int cursorDest)

Erases the current line with the existing prompt, then redraws the line with the provided prompt and buffer

Parameters
prompt the new prompt
buffer the buffer to be drawn
cursorDest where you want the cursor set when the line has been drawn. -1 for end of line.
Throws
IOException

public void restoreLine (String originalPrompt, int cursorDest)

Throws
IOException

public int searchBackwards (String searchTerm, int startIndex, boolean startsWith)

public int searchBackwards (String searchTerm, int startIndex)

Search backward in history from a given position.

Parameters
searchTerm substring to search for.
startIndex the index from which on to search
Returns
  • index where this substring has been found, or -1 else.

public int searchBackwards (String searchTerm)

Search backwards in history from the current position.

Parameters
searchTerm substring to search for.
Returns
  • index where the substring has been found, or -1 else.

public int searchForwards (String searchTerm, int startIndex, boolean startsWith)

public int searchForwards (String searchTerm)

Search forwards in history from the current position.

Parameters
searchTerm substring to search for.
Returns
  • index where the substring has been found, or -1 else.

public int searchForwards (String searchTerm, int startIndex)

Search forward in history from a given position.

Parameters
searchTerm substring to search for.
startIndex the index from which on to search
Returns
  • index where this substring has been found, or -1 else.

public void setAutoprintThreshold (int threshold)

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

public void setBellEnabled (boolean enabled)

Set whether the console bell is enabled.

Parameters
enabled true if enabled; false otherwise

public void setCommentBegin (String commentBegin)

Sets the string that will be used to start a comment when the insert-comment key is struck.

Parameters
commentBegin The begin comment string.

public void setCompletionHandler (CompletionHandler handler)

public void setCopyPasteDetection (boolean onoff)

Enables or disables copy and paste detection. The effect of enabling this this setting is that when a tab is received immediately followed by another character, the tab will not be treated as a completion, but as a tab literal.

Parameters
onoff true if detection is enabled

public final boolean setCursorPosition (int position)

Move the cursor position to the specified absolute index.

Throws
IOException

public void setEchoCharacter (Character c)

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
c the character to echo to the console in place of the typed character.

public void setExpandEvents (boolean expand)

public void setHandleUserInterrupt (boolean enabled)

Set whether user interrupts (ctrl-C) are handled by having JLine throw UserInterruptException from readLine(). Otherwise, the JVM will handle SIGINT as normal, which usually causes it to exit. The default is false.

public void setHistory (History history)

public void setHistoryEnabled (boolean enabled)

Whether or not to add new commands to the history buffer.

public boolean setKeyMap (String name)

Sets the current keymap by name. Supported keymaps are "emacs", "vi-insert", "vi-move".

Parameters
name The name of the keymap to switch to
Returns
  • true if the keymap was set, or false if the keymap is not recognized.

public void setPaginationEnabled (boolean enabled)

Whether to use pagination when the number of rows of candidates exceeds the height of the terminal.

public void setParenBlinkTimeout (int timeout)

public void setPrompt (String prompt)

public void shutdown ()

Shuts the console reader down. This method should be called when you have completed using the reader as it shuts down and cleans up resources that would otherwise be "leaked".

public boolean yank ()

Throws
IOException

public boolean yankPop ()

Throws
IOException

Protected Methods

protected void back (int num)

Move the visual cursor backwards without modifying the buffer cursor.

Throws
IOException

protected boolean complete ()

Use the completers to modify the buffer with the appropriate completions.

Returns
  • true if successful
Throws
IOException

protected String expandEvents (String str)

Expand event designator such as !!, !#, !3, etc... See http://www.gnu.org/software/bash/manual/html_node/Event-Designators.html

Throws
IOException

protected void finalize ()

Shuts down the ConsoleReader if the JVM attempts to clean it up.

Throws
Throwable

protected boolean moveToEnd ()

Throws
IOException

protected void printCompletionCandidates ()

Throws
IOException

protected final boolean resetLine ()

Erase the current line.

Returns
  • false if we failed (e.g., the buffer was empty)
Throws
IOException