jline.console
Class ConsoleReader

java.lang.Object
  extended by jline.console.ConsoleReader

public class ConsoleReader
extends java.lang.Object

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

Author:
Marc Prud'hommeaux, Jason Dillon

Field Summary
static char BACKSPACE
           
static java.lang.String CR
           
static java.lang.String JLINE_COMPLETION_THRESHOLD
           
static java.lang.String JLINE_KEYBINDINGS
           
static java.lang.String JLINE_NOBELL
           
static java.lang.String JLINEBINDINGS_PROPERTIES
           
static char KEYBOARD_BELL
           
static char NULL_MASK
           
static char RESET_LINE
           
static int TAB_WIDTH
           
 
Constructor Summary
ConsoleReader()
          Create a new reader using FileDescriptor.in for input and System.out for output.
ConsoleReader(java.io.InputStream in, java.io.Writer out)
           
ConsoleReader(java.io.InputStream in, java.io.Writer out, java.io.InputStream bindings, Terminal term)
           
ConsoleReader(java.io.InputStream in, java.io.Writer out, Terminal term)
           
 
Method Summary
 boolean addCompleter(Completer completer)
          Add the specified Completer to the list of handlers for tab-completion.
 void addTriggeredAction(char c, java.awt.event.ActionListener listener)
          Adding a triggered Action allows to give another curse of action if a character passed the pre-processing.
protected  void back(int num)
          Move the visual cursor backwards without modifying the buffer cursor.
 boolean backspace()
          Issue a backspace.
 void beep()
          Issue an audible keyboard bell, if isBellEnabled() return true.
 boolean clearScreen()
          Clear the screen by issuing the ANSI "clear screen" code.
 boolean delete()
          Issue a delete.
 void drawLine()
          Output put the prompt + the current buffer
 void flush()
          Flush the console output stream.
 int getAutoprintThreshold()
           
 java.util.Collection<Completer> getCompleters()
          Returns an unmodifiable list of all the completers.
 CompletionHandler getCompletionHandler()
           
 CursorBuffer getCursorBuffer()
           
 java.lang.Character getEchoCharacter()
          Returns the echo character.
 History getHistory()
           
 java.io.InputStream getInput()
           
 java.io.Writer getOutput()
           
 java.lang.String getPrompt()
           
 Terminal getTerminal()
           
 boolean isBellEnabled()
           
 boolean isHistoryEnabled()
          Whether or not to add new commands to the history buffer.
 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
 void print(java.lang.CharSequence s)
          Output the specified string to the output stream (but not the buffer).
 void printColumns(java.util.Collection<? extends java.lang.CharSequence> items)
          Output the specified Collection in proper columns.
 void println()
          Output a platform-dependant newline.
 void println(java.lang.CharSequence s)
           
 void printSearchStatus(java.lang.String searchTerm, java.lang.String match)
           
 void putString(java.lang.CharSequence str)
          Write out the specified string to the buffer and the output stream.
 int readCharacter(char... allowed)
           
 java.lang.String readLine()
          Read the next line and return the contents of the buffer.
 java.lang.String readLine(java.lang.Character mask)
          Read the next line with the specified character mask.
 java.lang.String readLine(java.lang.String prompt)
           
 java.lang.String readLine(java.lang.String prompt, java.lang.Character mask)
          Read a line from the in InputStream, and return the line (without any trailing newlines).
 int readVirtualKey()
          Read a character from the console.
 void redrawLine()
          Clear the line and redraw it.
 boolean removeCompleter(Completer completer)
          Remove the specified Completer from the list of handlers for tab-completion.
 boolean replace(int num, java.lang.String replacement)
           
 void resetPromptLine(java.lang.String prompt, java.lang.String buffer, int cursorDest)
          Erases the current line with the existing prompt, then redraws the line with the provided prompt and buffer
 void restoreLine(java.lang.String originalPrompt, int cursorDest)
           
 int searchBackwards(java.lang.String searchTerm)
          Search backwards in history from the current position.
 int searchBackwards(java.lang.String searchTerm, int startIndex)
          Search backward in history from a given position.
 int searchBackwards(java.lang.String searchTerm, int startIndex, boolean startsWith)
           
 void setAutoprintThreshold(int threshold)
           
 void setBellEnabled(boolean enabled)
           
 void setCompletionHandler(CompletionHandler handler)
           
 boolean setCursorPosition(int position)
          Move the cursor position to the specified absolute index.
 void setEchoCharacter(java.lang.Character c)
          Set the echo character.
 void setHistory(History history)
           
 void setHistoryEnabled(boolean enabled)
          Whether or not to add new commands to the history buffer.
 void setPaginationEnabled(boolean enabled)
          Whether to use pagination when the number of rows of candidates exceeds the height of the terminal.
 void setPrompt(java.lang.String prompt)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

JLINE_NOBELL

public static final java.lang.String JLINE_NOBELL
See Also:
Constant Field Values

BACKSPACE

public static final char BACKSPACE
See Also:
Constant Field Values

RESET_LINE

public static final char RESET_LINE
See Also:
Constant Field Values

KEYBOARD_BELL

public static final char KEYBOARD_BELL
See Also:
Constant Field Values

NULL_MASK

public static final char NULL_MASK
See Also:
Constant Field Values

TAB_WIDTH

public static final int TAB_WIDTH
See Also:
Constant Field Values

JLINE_COMPLETION_THRESHOLD

public static final java.lang.String JLINE_COMPLETION_THRESHOLD
See Also:
Constant Field Values

JLINE_KEYBINDINGS

public static final java.lang.String JLINE_KEYBINDINGS
See Also:
Constant Field Values

JLINEBINDINGS_PROPERTIES

public static final java.lang.String JLINEBINDINGS_PROPERTIES
See Also:
Constant Field Values

CR

public static final java.lang.String CR
Constructor Detail

ConsoleReader

public ConsoleReader(java.io.InputStream in,
                     java.io.Writer out,
                     java.io.InputStream bindings,
                     Terminal term)
              throws java.io.IOException
Throws:
java.io.IOException

ConsoleReader

public ConsoleReader(java.io.InputStream in,
                     java.io.Writer out,
                     Terminal term)
              throws java.io.IOException
Throws:
java.io.IOException

ConsoleReader

public ConsoleReader(java.io.InputStream in,
                     java.io.Writer out)
              throws java.io.IOException
Throws:
java.io.IOException

ConsoleReader

public ConsoleReader()
              throws java.io.IOException
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 not being buffered.

Throws:
java.io.IOException
Method Detail

getInput

public java.io.InputStream getInput()

getOutput

public java.io.Writer getOutput()

getTerminal

public Terminal getTerminal()

getCursorBuffer

public CursorBuffer getCursorBuffer()

setBellEnabled

public void setBellEnabled(boolean enabled)

isBellEnabled

public boolean isBellEnabled()

setPrompt

public void setPrompt(java.lang.String prompt)

getPrompt

public java.lang.String getPrompt()

setEchoCharacter

public void setEchoCharacter(java.lang.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.

getEchoCharacter

public java.lang.Character getEchoCharacter()
Returns the echo character.


setCursorPosition

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

Throws:
java.io.IOException

drawLine

public final void drawLine()
                    throws java.io.IOException
Output put the prompt + the current buffer

Throws:
java.io.IOException

redrawLine

public final void redrawLine()
                      throws java.io.IOException
Clear the line and redraw it.

Throws:
java.io.IOException

putString

public final void putString(java.lang.CharSequence str)
                     throws java.io.IOException
Write out the specified string to the buffer and the output stream.

Throws:
java.io.IOException

back

protected void back(int num)
             throws java.io.IOException
Move the visual cursor backwards without modifying the buffer cursor.

Throws:
java.io.IOException

flush

public void flush()
           throws java.io.IOException
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:
java.io.IOException

backspace

public boolean backspace()
                  throws java.io.IOException
Issue a backspace.

Returns:
true if successful
Throws:
java.io.IOException

moveCursor

public int moveCursor(int num)
               throws java.io.IOException
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:
java.io.IOException

replace

public final boolean replace(int num,
                             java.lang.String replacement)

readVirtualKey

public final int readVirtualKey()
                         throws java.io.IOException
Read a character from the console.

Returns:
the character, or -1 if an EOF is received.
Throws:
java.io.IOException

readCharacter

public final int readCharacter(char... allowed)
                        throws java.io.IOException
Throws:
java.io.IOException

readLine

public java.lang.String readLine()
                          throws java.io.IOException
Read the next line and return the contents of the buffer.

Throws:
java.io.IOException

readLine

public java.lang.String readLine(java.lang.Character mask)
                          throws java.io.IOException
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:
java.io.IOException

readLine

public java.lang.String readLine(java.lang.String prompt)
                          throws java.io.IOException
Throws:
java.io.IOException

readLine

public java.lang.String readLine(java.lang.String prompt,
                                 java.lang.Character mask)
                          throws java.io.IOException
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:
java.io.IOException

addCompleter

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

removeCompleter

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

getCompleters

public java.util.Collection<Completer> getCompleters()
Returns an unmodifiable list of all the completers.


setCompletionHandler

public void setCompletionHandler(CompletionHandler handler)

getCompletionHandler

public CompletionHandler getCompletionHandler()

setAutoprintThreshold

public void setAutoprintThreshold(int threshold)
Parameters:
threshold - the number of candidates to print without issuing a warning.

getAutoprintThreshold

public int getAutoprintThreshold()
Returns:
the number of candidates to print without issuing a warning.

setPaginationEnabled

public void setPaginationEnabled(boolean enabled)
Whether to use pagination when the number of rows of candidates exceeds the height of the terminal.


isPaginationEnabled

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


setHistory

public void setHistory(History history)

getHistory

public History getHistory()

setHistoryEnabled

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


isHistoryEnabled

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


print

public final void print(java.lang.CharSequence s)
                 throws java.io.IOException
Output the specified string to the output stream (but not the buffer).

Throws:
java.io.IOException

println

public final void println(java.lang.CharSequence s)
                   throws java.io.IOException
Throws:
java.io.IOException

println

public final void println()
                   throws java.io.IOException
Output a platform-dependant newline.

Throws:
java.io.IOException

delete

public final boolean delete()
                     throws java.io.IOException
Issue a delete.

Returns:
true if successful
Throws:
java.io.IOException

killLine

public boolean killLine()
                 throws java.io.IOException
Kill the buffer ahead of the current cursor position.

Returns:
true if successful
Throws:
java.io.IOException

clearScreen

public boolean clearScreen()
                    throws java.io.IOException
Clear the screen by issuing the ANSI "clear screen" code.

Throws:
java.io.IOException

beep

public void beep()
          throws java.io.IOException
Issue an audible keyboard bell, if isBellEnabled() return true.

Throws:
java.io.IOException

paste

public boolean paste()
              throws java.io.IOException
Paste the contents of the clipboard into the console buffer

Returns:
true if clipboard contents pasted
Throws:
java.io.IOException

addTriggeredAction

public void addTriggeredAction(char c,
                               java.awt.event.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.


printColumns

public void printColumns(java.util.Collection<? extends java.lang.CharSequence> items)
                  throws java.io.IOException
Output the specified Collection in proper columns.

Throws:
java.io.IOException

resetPromptLine

public void resetPromptLine(java.lang.String prompt,
                            java.lang.String buffer,
                            int cursorDest)
                     throws java.io.IOException
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:
java.io.IOException

printSearchStatus

public void printSearchStatus(java.lang.String searchTerm,
                              java.lang.String match)
                       throws java.io.IOException
Throws:
java.io.IOException

restoreLine

public void restoreLine(java.lang.String originalPrompt,
                        int cursorDest)
                 throws java.io.IOException
Throws:
java.io.IOException

searchBackwards

public int searchBackwards(java.lang.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.

searchBackwards

public int searchBackwards(java.lang.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.

searchBackwards

public int searchBackwards(java.lang.String searchTerm,
                           int startIndex,
                           boolean startsWith)