Class Buffer

java.lang.Object
org.aesh.readline.Buffer

public final class Buffer extends Object
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
    Constructor
    Description
    Creates a copy of an existing buffer.
  • Method Summary

    Modifier and Type
    Method
    Description
    Returns the entire buffer content as a string, including multi-line content.
    void
    Clears the buffer content without resetting multi-line state.
    int
    Returns the current cursor position in the buffer.
    void
    delete(Consumer<int[]> out, int delta, int width)
    Deletes characters from the buffer relative to the cursor position.
    void
    delete(Consumer<int[]> out, int delta, int width, boolean viMode)
    Deletes characters from the buffer relative to the cursor position.
    void
    disablePrompt(boolean disable)
    Need to disable prompt in calculations involving search.
    void
    Forces the delta changed at end of buffer flag to a specific value.
    int
    get(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.
    void
    insert(Consumer<int[]> out, int[] data, int width)
    Inserts text at the current cursor position.
    void
    insert(Consumer<int[]> out, int data, int width)
    Inserts a single character at the current cursor position.
    void
    insert(Consumer<int[]> out, String str, int width)
    Inserts a string at the current cursor position and updates the cursor accordingly.
    void
    Some completion occured, do not try to compute character index location.
    boolean
    Checks if the buffer is in masking mode (e.g., for password input).
    boolean
    Checks if the buffer is in multi-line mode.
    boolean
    Checks if the prompt is disabled for calculations.
    int
    Returns the length of the buffer content.
    void
    move(Consumer<int[]> out, int move, int termWidth)
    Move the cursor left if the param is negative, right if its positive.
    void
    move(Consumer<int[]> out, int move, int termWidth, boolean viMode)
    Move 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.
    int
    Returns the cursor position including the multi-line buffer offset.
    int[]
    Returns the complete buffer content including multi-line content.
    Returns the current prompt.
    void
    replace(Consumer<int[]> out, char rChar)
    Replaces the character at the current cursor position with the specified character.
    void
    replace(Consumer<int[]> out, int[] line, int width)
    Replaces the entire current buffer with the given line as code points.
    void
    replace(Consumer<int[]> out, String line, int width)
    Replace the entire current buffer with the given line.
    void
    Resets the buffer to its initial empty state.
    void
    setIsPromptDisplayed(boolean isPromptDisplayed)
    Sets whether the prompt has been displayed.
    void
    setMultiLine(boolean multi)
    Sets the multi-line mode for this buffer.
    void
    Updates the multi-line buffer by appending the current line content.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Buffer

      public Buffer(Buffer buf)
      Creates a copy of an existing buffer.
      Parameters:
      buf - the buffer to copy
  • Method Details

    • getCursorLocator

      public CursorLocator 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

      public String 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

      public Prompt prompt()
      Returns the current prompt.
      Returns:
      the prompt associated with this buffer
    • 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

      public void insert(Consumer<int[]> out, int[] data, int width)
      Inserts text at the current cursor position.
      Parameters:
      out - the output consumer for terminal updates
      data - the text to insert as an array of code points
      width - the terminal width
    • insert

      public void insert(Consumer<int[]> out, int data, int width)
      Inserts a single character at the current cursor position.
      Parameters:
      out - the output consumer for terminal updates
      data - the character code point to insert
      width - the terminal width
    • move

      public void move(Consumer<int[]> out, int move, int termWidth)
      Move the cursor left if the param is negative, right if its positive.
      Parameters:
      out - stream
      move - where to move
      termWidth - terminal width
    • move

      public void move(Consumer<int[]> out, int move, int termWidth, boolean viMode)
      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 - stream
      move - where to move
      termWidth - terminal width
      viMode - 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 move
      direction - 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

      public void replace(Consumer<int[]> out, String line, int width)
      Replace the entire current buffer with the given line. The new line will be pushed to the consumer Cursor will be moved to the end of the new buffer line
      Parameters:
      out - stream
      line - new buffer line
      width - term width
    • replace

      public void replace(Consumer<int[]> out, int[] line, int width)
      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 updates
      line - the new buffer content as an array of code points
      width - 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

      public void delete(Consumer<int[]> out, int delta, int width)
      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 updates
      delta - the number of characters to delete (negative for backward, positive for forward)
      width - the terminal width
    • delete

      public void delete(Consumer<int[]> out, int delta, int width, boolean viMode)
      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 updates
      delta - the number of characters to delete (negative for backward, positive for forward)
      width - the terminal width
      viMode - true if vi editing mode is enabled, false for emacs mode
    • insert

      public void insert(Consumer<int[]> out, String str, int width)
      Inserts a string at the current cursor position and updates the cursor accordingly.
      Parameters:
      out - the output consumer for terminal updates
      str - the string to insert
      width - the terminal width
    • replace

      public void replace(Consumer<int[]> out, char rChar)
      Replaces the character at the current cursor position with the specified character.
      Parameters:
      out - the output consumer for terminal updates
      rChar - the replacement character