Package jline
Class ConsoleReader
java.lang.Object
jline.ConsoleReader
- All Implemented Interfaces:
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.initializeTerminal() for convenience
methods for issuing platform-specific setup commands.- Author:
- Marc Prud'hommeaux
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Stringstatic SortedMapMap that contains the operation name to keymay operation mapping.Fields inherited from interface jline.ConsoleOperations
ABORT, ADD, BACKSPACE, CHANGE_CASE, CHANGE_META, CLEAR_LINE, CLEAR_SCREEN, COMPLETE, CTRL_A, CTRL_B, CTRL_C, CTRL_D, CTRL_E, CTRL_F, CTRL_G, 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
ConstructorsConstructorDescriptionCreate a new reader usingFileDescriptor.infor input andSystem.outfor output.ConsoleReader(InputStream in, Writer out) Create a new reader using the specifiedInputStreamfor 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
Modifier and TypeMethodDescriptionbooleanaddCompletor(Completor completor) Add the specifiedCompletorto the list of handlers for tab-completion.voidaddTriggeredAction(char c, ActionListener listener) Adding a triggered Action allows to give another course of action if a character passed the preprocessing.final booleanIssue a backspace.final voidbeep()Issue an audible keyboard bell, ifgetBellEnabled()return true.booleanClear the screen by issuing the ANSI "clear screen" code.static voiddebug.final booleandelete()Issue a delete.final voiddrawLine()Output put the prompt + the current bufferfinal voidFlush the console output stream.intbooleanReturns an unmodifiable list of all the completors.The default prompt that will be issued.Returns the echo character.getInput()Returns the stream used for console input.intQuery the terminal to find the current width;intQuery the terminal to find the current width;booleanWhether or not to add new commands to the history buffer.booleanWhether 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.final intmoveCursor(int num) Move the cursor where characters.booleanpaste()Paste the contents of the clipboard into the console buffervoidprintColumns(Collection stuff) Output the specifiedCollectionin proper columns.final voidOutput a platform-dependant newline.voidprintSearchStatus(String searchTerm, String match) final voidprintString(String str) Output the specified string to the output stream (but not the buffer).final voidWrite out the specified string to the buffer and the output stream.final intreadCharacter(char[] allowed) readLine()Read the next line and return the contents of the buffer.Read the next line with the specified character mask.Read a line from the inInputStream, and return the line (without any trailing newlines).final intRead a character from the console.final voidClear the line and redraw it.booleanremoveCompletor(Completor completor) Remove the specifiedCompletorfrom the list of handlers for tab-completion.final booleanvoidvoidsetAutoprintThreshhold(int autoprintThreshhold) voidsetBellEnabled(boolean bellEnabled) voidsetCompletionHandler(CompletionHandler completionHandler) final 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 Details
-
CR
-
KEYMAP_NAMES
Map that contains the operation name to keymay operation mapping.
-
-
Constructor Details
-
ConsoleReader
Create a new reader usingFileDescriptor.infor input andSystem.outfor output.FileDescriptor.inis used because it has a better chance of being unbuffered.- Throws:
IOException
-
ConsoleReader
Create a new reader using the specifiedInputStreamfor input and the specific writer for output, using the default keybindings resource.- Throws:
IOException
-
ConsoleReader
- Throws:
IOException
-
ConsoleReader
public ConsoleReader(InputStream in, Writer out, InputStream bindings, Terminal term) throws IOException Create a new reader.- Parameters:
in- the inputout- the outputbindings- the key bindings to useterm- the terminal to use- Throws:
IOException
-
-
Method Details
-
addTriggeredAction
Adding a triggered Action allows to give another course 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-
-
getTerminal
-
setDebug
Set the stream for debugging. Development use only. -
setInput
Set the stream to be used for console input. -
getInput
Returns the stream used for console input. -
readLine
Read the next line and return the contents of the buffer.- Throws:
IOException
-
readLine
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
-
setBellEnabled
- Parameters:
bellEnabled- if true, enable audible keyboard bells if an alert is required.
-
getBellEnabled
- Returns:
- true is audible keyboard bell is enabled.
-
getTermwidth
Query the terminal to find the current width;- Returns:
- the width of the current terminal.
- See Also:
-
getTermheight
Query the terminal to find the current width;- Returns:
- the height of the current terminal.
- See Also:
-
setAutoprintThreshhold
- Parameters:
autoprintThreshhold- the number of candidates to print without issuing a warning.
-
getAutoprintThreshhold
- Returns:
- the number of candidates to print without issing a warning.
-
readLine
- Throws:
IOException
-
setDefaultPrompt
The default prompt that will be issued. -
getDefaultPrompt
The default prompt that will be issued. -
readLine
Read a line from the inInputStream, 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
-
paste
Paste the contents of the clipboard into the console buffer- Returns:
- true if clipboard contents pasted
- Throws:
IOException
-
killLine
Kill the buffer ahead of the current cursor position.- Returns:
- true if successful
- Throws:
IOException
-
clearScreen
Clear the screen by issuing the ANSI "clear screen" code.- Throws:
IOException
-
getCursorBuffer
-
printColumns
Output the specifiedCollectionin proper columns.- Parameters:
stuff- the stuff to print- Throws:
IOException
-
addCompletor
Add the specifiedCompletorto the list of handlers for tab-completion.- Parameters:
completor- theCompletorto add- Returns:
- true if it was successfully added
-
removeCompletor
Remove the specifiedCompletorfrom the list of handlers for tab-completion.- Parameters:
completor- theCompletorto remove- Returns:
- true if it was successfully removed
-
getCompletors
Returns an unmodifiable list of all the completors. -
setCursorPosition
Move the cursor position to the specified absolute index.- Throws:
IOException
-
redrawLine
Clear the line and redraw it.- Throws:
IOException
-
drawLine
Output put the prompt + the current buffer- Throws:
IOException
-
printNewline
Output a platform-dependant newline.- Throws:
IOException
-
putString
Write out the specified string to the buffer and the output stream.- Throws:
IOException
-
printString
Output the specified string to the output stream (but not the buffer).- Throws:
IOException
-
beep
Issue an audible keyboard bell, ifgetBellEnabled()return true.- Throws:
IOException
-
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.- Throws:
IOException
-
backspace
Issue a backspace.- Returns:
- true if successful
- Throws:
IOException
-
moveCursor
Move the cursor where characters.- Parameters:
num- if less than 0, move abs(num) to the left, otherwise move num to the right.- Returns:
- the number of spaces we moved
- Throws:
IOException
-
debug
debug.- Parameters:
str- the message to issue.
-
readVirtualKey
Read a character from the console.- Returns:
- the character, or -1 if an EOF is received.
- Throws:
IOException
-
readCharacter
- Throws:
IOException
-
replace
-
delete
Issue a delete.- Returns:
- true if successful
- Throws:
IOException
-
setHistory
-
getHistory
-
setCompletionHandler
-
getCompletionHandler
-
setEchoCharacter
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 tonew Character(0)
will cause nothing to be echoed.- Parameters:
echoCharacter- the character to echo to the console in place of the typed character.
-
getEchoCharacter
Returns the echo character. -
setUseHistory
Whether or not to add new commands to the history buffer. -
getUseHistory
Whether or not to add new commands to the history buffer. -
setUsePagination
Whether to use pagination when the number of rows of candidates exceeds the height of the temrinal. -
getUsePagination
Whether to use pagination when the number of rows of candidates exceeds the height of the temrinal. -
printSearchStatus
- Throws:
IOException
-
restoreLine
- Throws:
IOException
-