Class Buffer
java.lang.Object
org.aesh.readline.Buffer
Buffer to keep track of text and cursor position in the console.
Is using ANSI-codes to clear text and move cursor in the terminal.
- Author:
- Ståle W. Pedersen
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionasString()Returns the entire buffer content as a string, including multi-line content.voidclear()Clears the buffer content without resetting multi-line state.intcursor()Returns the current cursor position in the buffer.voidDeletes characters from the buffer relative to the cursor position.voidDeletes characters from the buffer relative to the cursor position.voiddisablePrompt(boolean disable) Need to disable prompt in calculations involving search.voidforceSetDeltaChangedAtEndOfBuffer(boolean delta) Forces the delta changed at end of buffer flag to a specific value.intget(int pos) Returns the character at the specified position in the buffer.Returns the cursor locator for this buffer.int[]Returns the current line content, with masking applied if enabled.voidInserts text at the current cursor position.voidInserts a single character at the current cursor position.voidInserts a string at the current cursor position and updates the cursor accordingly.voidSome completion occured, do not try to compute character index location.booleanChecks if the buffer is in masking mode (e.g., for password input).booleanChecks if the buffer is in multi-line mode.booleanChecks if the prompt is disabled for calculations.intlength()Returns the length of the buffer content.voidMove the cursor left if the param is negative, right if its positive.voidMove the cursor left if the param is negative, right if its positive.static int[]moveNumberOfColumns(int column, char direction) Creates an ANSI escape sequence to move the cursor a specified number of columns.intReturns the cursor position including the multi-line buffer offset.int[]Returns the complete buffer content including multi-line content.prompt()Returns the current prompt.voidReplaces the character at the current cursor position with the specified character.voidReplaces the entire current buffer with the given line as code points.voidReplace the entire current buffer with the given line.voidreset()Resets the buffer to its initial empty state.voidsetIsPromptDisplayed(boolean isPromptDisplayed) Sets whether the prompt has been displayed.voidsetMultiLine(boolean multi) Sets the multi-line mode for this buffer.voidUpdates the multi-line buffer by appending the current line content.
-
Constructor Details
-
Buffer
Creates a copy of an existing buffer.- Parameters:
buf- the buffer to copy
-
-
Method Details
-
getCursorLocator
Returns the cursor locator for this buffer.- Returns:
- the cursor locator used to track cursor position
-
get
public int get(int pos) Returns the character at the specified position in the buffer.- Parameters:
pos- the position in the buffer- Returns:
- the character code point at the specified position
- Throws:
IndexOutOfBoundsException- if the position is out of bounds
-
cursor
public int cursor()Returns the current cursor position in the buffer.- Returns:
- the current cursor position
-
multiCursor
public int multiCursor()Returns the cursor position including the multi-line buffer offset. If the buffer is in multi-line mode, this returns the cursor position relative to the entire multi-line content.- Returns:
- the cursor position including multi-line offset
-
isMasking
public boolean isMasking()Checks if the buffer is in masking mode (e.g., for password input).- Returns:
- true if input is being masked, false otherwise
-
isMultiLine
public boolean isMultiLine()Checks if the buffer is in multi-line mode.- Returns:
- true if the buffer is in multi-line mode, false otherwise
-
asString
Returns the entire buffer content as a string, including multi-line content.- Returns:
- the buffer content as a string
-
reset
public void reset()Resets the buffer to its initial empty state. This clears all content, resets the cursor to position 0, and clears any multi-line buffer content. -
setIsPromptDisplayed
public void setIsPromptDisplayed(boolean isPromptDisplayed) Sets whether the prompt has been displayed.- Parameters:
isPromptDisplayed- true if the prompt is displayed, false otherwise
-
forceSetDeltaChangedAtEndOfBuffer
public void forceSetDeltaChangedAtEndOfBuffer(boolean delta) Forces the delta changed at end of buffer flag to a specific value. This flag is used to optimize terminal updates.- Parameters:
delta- the value to set for the deltaChangedAtEndOfBuffer flag
-
disablePrompt
public void disablePrompt(boolean disable) Need to disable prompt in calculations involving search.- Parameters:
disable- prompt or not
-
isPromptDisabled
public boolean isPromptDisabled()Checks if the prompt is disabled for calculations.- Returns:
- true if the prompt is disabled, false otherwise
-
prompt
-
length
public int length()Returns the length of the buffer content. If masking with a null mask character, returns 1.- Returns:
- the length of the buffer content
-
setMultiLine
public void setMultiLine(boolean multi) Sets the multi-line mode for this buffer. Multi-line mode is not enabled if the buffer is in masking mode.- Parameters:
multi- true to enable multi-line mode, false to disable
-
invalidateCursorLocation
public void invalidateCursorLocation()Some completion occured, do not try to compute character index location. This could be revisited to implement a strategy. -
updateMultiLineBuffer
public void updateMultiLineBuffer()Updates the multi-line buffer by appending the current line content. If the line ends with a backslash, the backslash is removed. Otherwise, a newline is appended to the buffer. -
insert
Inserts text at the current cursor position.- Parameters:
out- the output consumer for terminal updatesdata- the text to insert as an array of code pointswidth- the terminal width
-
insert
Inserts a single character at the current cursor position.- Parameters:
out- the output consumer for terminal updatesdata- the character code point to insertwidth- the terminal width
-
move
Move the cursor left if the param is negative, right if its positive.- Parameters:
out- streammove- where to movetermWidth- terminal width
-
move
Move the cursor left if the param is negative, right if its positive. If viMode is true, the cursor will not move beyond the current buffer size- Parameters:
out- streammove- where to movetermWidth- terminal widthviMode- edit mode (vi or emacs)
-
moveNumberOfColumns
public static int[] moveNumberOfColumns(int column, char direction) Creates an ANSI escape sequence to move the cursor a specified number of columns.- Parameters:
column- the number of columns to movedirection- the direction character ('C' for right, 'D' for left, 'A' for up, 'B' for down)- Returns:
- an int array containing the ANSI escape sequence
-
getLineMasked
public int[] getLineMasked()Returns the current line content, with masking applied if enabled. If masking is enabled, each character is replaced with the mask character. If the mask character is null (0), an empty array is returned.- Returns:
- the line content with masking applied, or the original line if not masking
-
clear
public void clear()Clears the buffer content without resetting multi-line state. This resets the cursor position and size to 0 and marks the prompt as not displayed. -
replace
-
replace
Replaces the entire current buffer with the given line as code points. The new line will be pushed to the consumer and the cursor will be moved to the end of the new buffer line.- Parameters:
out- the output consumer for terminal updatesline- the new buffer content as an array of code pointswidth- the terminal width
-
multiLine
public int[] multiLine()Returns the complete buffer content including multi-line content. If in multi-line mode, this combines the multi-line buffer with the current line.- Returns:
- the complete buffer content as an array of code points
-
delete
Deletes characters from the buffer relative to the cursor position. Deletes backward if delta is negative, forward if delta is positive.- Parameters:
out- the output consumer for terminal updatesdelta- the number of characters to delete (negative for backward, positive for forward)width- the terminal width
-
delete
Deletes characters from the buffer relative to the cursor position. Deletes backward if delta is negative, forward if delta is positive.- Parameters:
out- the output consumer for terminal updatesdelta- the number of characters to delete (negative for backward, positive for forward)width- the terminal widthviMode- true if vi editing mode is enabled, false for emacs mode
-
insert
-
replace
Replaces the character at the current cursor position with the specified character.- Parameters:
out- the output consumer for terminal updatesrChar- the replacement character
-