DuckDB Terminal - v0.4.2
    Preparing search index...

    Class InputBuffer

    A text buffer with cursor management for terminal line editing.

    The InputBuffer maintains both the current text content and cursor position, providing methods for common editing operations. Each editing method returns the VT100 escape sequences needed to update the terminal display.

    Features:

    • Character insertion at cursor position
    • Backspace and delete key handling
    • Cursor movement (left, right, home, end)
    • Line clearing operations
    • Word-based operations for auto-completion
    const buffer = new InputBuffer();

    // Type "hello"
    terminal.write(buffer.insert('h'));
    terminal.write(buffer.insert('e'));
    terminal.write(buffer.insert('l'));
    terminal.write(buffer.insert('l'));
    terminal.write(buffer.insert('o'));

    console.log(buffer.getContent()); // 'hello'
    console.log(buffer.getCursorPos()); // 5

    // Delete last character
    terminal.write(buffer.backspace()); // Buffer is now 'hell'
    const buffer = new InputBuffer();
    buffer.setContent('hello world');

    terminal.write(buffer.moveToStart()); // Cursor at position 0
    terminal.write(buffer.moveRight()); // Cursor at position 1
    terminal.write(buffer.moveToEnd()); // Cursor at position 11
    const buffer = new InputBuffer();
    buffer.setContent('SELECT * FROM us');

    const word = buffer.getWordBeforeCursor(); // 'us'
    terminal.write(buffer.replaceWordBeforeCursor('users'));
    console.log(buffer.getContent()); // 'SELECT * FROM users'
    Index

    Constructors

    Properties

    buffer: string = ''
    cursorPos: number = 0
    maxSize: number = MAX_BUFFER_SIZE
    promptLength: number = 0
    terminalWidth: number = 80

    Methods

    • Calculate the row and column position for a given buffer offset. Takes into account the prompt length on the first line.

      Parameters

      • offset: number

        The buffer offset (0-based)

      Returns { col: number; row: number }

      Object with row (0-based) and col (0-based) position

    • Returns the word immediately before the cursor for auto-completion.

      A word is defined as a sequence of word characters (\w) and dots.

      Returns string

      The word before cursor, or empty string if none

    • Generate VT100 escape sequence to move from one position to another.

      Parameters

      • from: { col: number; row: number }

        The starting position

      • to: { col: number; row: number }

        The target position

      Returns string

      VT100 escape sequence string

    • Replaces the word before the cursor with a completion string.

      Parameters

      • completion: string

        The completion string to insert

      Returns string

      VT100 escape sequences to update the terminal display

    • Sets the buffer content and moves cursor to end.

      Used for history navigation when replacing the current line with a previous command.

      Parameters

      • content: string

        The new buffer content

      Returns void

    • Set the prompt length for cursor calculations. This is needed to correctly calculate line wrapping since the prompt takes up space on the first line.

      Parameters

      • length: number

        The visible length of the prompt (excluding ANSI codes)

      Returns void