DuckDB Terminal - v0.2.0
    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

    • Deletes the character before the cursor (backspace operation).

      Returns string

      VT100 escape sequences to update the terminal display, or empty string if at start

    • Clears the entire line and resets buffer (Ctrl+U).

      Returns string

      VT100 escape sequences to clear the terminal line

    • Clears from cursor position to end of line (Ctrl+K).

      Returns string

      VT100 escape sequence to clear the terminal

    • Deletes the character at the cursor position (delete key operation).

      Returns string

      VT100 escape sequences to update the terminal display, or empty string if at end

    • Returns the current buffer content.

      Returns string

      The full text content of the buffer

    • Returns the current cursor position within the buffer.

      Returns number

      The zero-based cursor position

    • Get the current cursor position as row/col.

      Returns { col: number; row: number }

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

    • Get the end position (after the last character).

      Returns { col: number; row: number }

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

    • Returns the maximum buffer size.

      Returns number

      The maximum size in bytes

    • 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

    • Get the current prompt length.

      Returns number

      The prompt length in characters

    • Calculate the number of rows the current content spans.

      Returns number

      The total number of rows (1-based)

    • Get the current terminal width.

      Returns number

      The terminal width in columns

    • 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

    • Inserts text at the cursor position.

      Parameters

      • char: string

        The character(s) to insert

      Returns string

      VT100 escape sequences to update the terminal display

    • Checks if the buffer contains only whitespace.

      Returns boolean

      True if buffer is empty or contains only whitespace

    • Checks if the buffer is empty.

      Returns boolean

      True if buffer has no content

    • 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

    • Moves the cursor one position to the left.

      Returns string

      VT100 escape sequence, or empty string if already at start

    • Moves the cursor one position to the right.

      Returns string

      VT100 escape sequence, or empty string if already at end

    • Moves the cursor to the end of the line (End key / Ctrl+E).

      Returns string

      VT100 escape sequence, or empty string if already at end

    • Moves the cursor to the start of the line (Home key / Ctrl+A).

      Returns string

      VT100 escape sequence, or empty string if already at start

    • 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

    • Sets the maximum buffer size.

      Parameters

      • size: number

        Maximum size in bytes (defaults to MAX_BUFFER_SIZE)

      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

    • Set the terminal width for line wrapping calculations.

      Parameters

      • width: number

        The terminal width in columns

      Returns void