Raxol.Terminal.Emulator (Raxol v2.0.1)

View Source

Enterprise-grade terminal emulator with VT100/ANSI support and high-performance parsing.

Provides full terminal emulation with true color, mouse tracking, alternate screen, and modern features. Uses modular architecture with separate coordinators for buffer, mode, input, and output operations.

Usage

# Create standard emulator
emulator = Raxol.Terminal.Emulator.new(80, 24)

# Process input with colors
{emulator, output} = Raxol.Terminal.Emulator.process_input(
  emulator, 
  "\e[1;31mRed Bold\e[0m Normal text"
)

Performance Modes

  • new/2 - Full features (2.8MB, ~95ms startup)
  • new_lite/3 - Most features (1.2MB, ~30ms startup)
  • new_minimal/2 - Basic only (8.8KB, <10ms startup)

Summary

Functions

Applies color changes (legacy compatibility).

Returns cursor blinking state.

Cleans up emulator resources.

Clears the specified line.

Clears the entire screen.

Clears the screen and moves cursor to home position.

Clears the scrollback buffer.

Clears the current text selection.

Returns true if the cursor is blinking.

Returns true if the cursor is visible.

Deletes the character at the cursor position.

Deletes the specified number of characters.

Ends the current text selection.

Erases the specified number of characters.

Erases display content based on mode (0=to end, 1=from start, 2=entire).

Erases from cursor position to end of screen.

Erases from start of screen to cursor position.

Erases content within the display.

Erases content within the current line.

Erases line content based on mode.

Gets the configuration structure.

Gets the current cursor position as {x, y}.

Gets cursor position as a structured object.

Gets the current cursor style (:block, :line, :underscore).

Gets cursor visibility state.

Gets the terminal height in rows.

Gets the mode manager.

Gets cursor visibility from mode manager.

Gets output from the emulator.

Gets output buffer (legacy compatibility).

Gets the active screen buffer.

Gets the current scroll region as {top, bottom}.

Gets the scrollback buffer contents.

Gets the currently selected text.

Gets the terminal width in columns.

Handles ESC = sequence (DECKPAM - Enable application keypad mode).

Handles ESC > sequence (DECKPNM - Disable application keypad mode).

Returns true if text is currently selected.

Inserts a character at the cursor position.

Inserts the specified number of blank characters.

Performs automatic scrolling if needed.

Moves the cursor to the specified position.

Moves cursor back (stub implementation).

Moves cursor down (stub implementation).

Moves cursor forward (stub implementation).

Moves cursor to specified position.

Moves cursor to specified position with options.

Moves cursor up (stub implementation).

Creates a new terminal emulator.

Creates a new terminal emulator with specified dimensions.

Creates a new terminal emulator with options.

Creates a new terminal emulator with session ID and client options.

Processes input and returns updated emulator with output.

Renders the emulator screen.

Resets the terminal emulator to its initial state.

Resets a terminal mode.

Resizes the terminal to the specified dimensions.

Restores the previously saved terminal state.

Saves the current terminal state.

Scrolls the display down by the specified number of lines.

Scrolls the display up by the specified number of lines.

Sets a terminal attribute.

Sets cursor blinking state.

Sets the cursor position to the specified coordinates.

Sets the cursor style to :block, :line, or :underscore.

Sets terminal dimensions after validation.

Sets a terminal mode.

Starts a linked terminal emulator process.

Starts text selection at the specified coordinates.

Switches to the alternate screen buffer.

Switches to the normal screen buffer.

Updates the active buffer with new content.

Updates auto wrap mode state.

Updates blink state (legacy compatibility).

Updates insert mode state.

Updates the selection endpoint to the specified coordinates.

Validates terminal dimensions.

Returns cursor visibility state.

Writes text to the terminal at the cursor position.

Writes data to the output buffer.

Types

t()

@type t() :: %Raxol.Terminal.Emulator{
  active: any(),
  active_buffer: any(),
  active_buffer_type: atom(),
  alternate: any(),
  alternate_screen_buffer: any(),
  bracketed_paste_active: boolean(),
  bracketed_paste_buffer: String.t(),
  buffer: any(),
  capabilities_manager: any(),
  charset_state: map(),
  client_options: map(),
  clipboard_manager: any(),
  color_manager: any(),
  color_palette: map(),
  command: any(),
  command_history: list(),
  config: any(),
  current_command_buffer: String.t(),
  current_hyperlink: any(),
  cursor: any(),
  cursor_blink_rate: non_neg_integer(),
  cursor_manager: any(),
  cursor_position_reported: boolean(),
  cursor_style: atom(),
  damage_tracker: any(),
  device_status_manager: any(),
  device_status_reported: boolean(),
  event: any(),
  font_manager: any(),
  graphics_manager: any(),
  height: non_neg_integer(),
  history_buffer: term(),
  hyperlink_manager: any(),
  icon_name: String.t() | nil,
  input_manager: any(),
  last_col_exceeded: boolean(),
  last_key_event: any(),
  main_screen_buffer: any(),
  max_command_history: non_neg_integer(),
  memory_limit: non_neg_integer(),
  metrics_manager: any(),
  mode_manager: any(),
  mode_manager_pid: any(),
  mode_state: map(),
  mouse_manager: any(),
  notification_manager: any(),
  output_buffer: list(),
  output_manager: any(),
  parser_state: any(),
  plugin_manager: any(),
  registry: any(),
  renderer: any(),
  saved_cursor: any(),
  screen_buffer_manager: any(),
  scroll_manager: any(),
  scroll_region: tuple(),
  scrollback_buffer: list(),
  scrollback_limit: non_neg_integer(),
  scrollback_manager: any(),
  selection_manager: any(),
  session_id: String.t(),
  session_manager: any(),
  sixel_state: any(),
  state: any(),
  state_manager: any(),
  state_stack: list(),
  style: any(),
  style_manager: any(),
  supervisor: any(),
  sync_manager: any(),
  tab_manager: any(),
  tab_stops: list(),
  terminal_state_manager: any(),
  theme_manager: any(),
  validation_service: any(),
  width: non_neg_integer(),
  window_manager: any(),
  window_registry: any(),
  window_state: map(),
  window_title: String.t() | nil
}

Functions

apply_color_changes(emulator)

Applies color changes (legacy compatibility).

blinking?(emulator)

Returns cursor blinking state.

cleanup(emulator)

@spec cleanup(t()) :: :ok

Cleans up emulator resources.

clear_line(emulator, line)

Clears the specified line.

clear_screen(emulator)

Clears the entire screen.

clear_screen_and_home(emulator)

Clears the screen and moves cursor to home position.

clear_scrollback(emulator)

Clears the scrollback buffer.

clear_selection(emulator)

Clears the current text selection.

cursor_blinking?(emulator)

Returns true if the cursor is blinking.

cursor_visible?(emulator)

Returns true if the cursor is visible.

delete_char(emulator)

Deletes the character at the cursor position.

delete_chars(emulator, count)

Deletes the specified number of characters.

end_selection(emulator)

Ends the current text selection.

erase_chars(emulator, count)

Erases the specified number of characters.

erase_display(emulator, mode)

Erases display content based on mode (0=to end, 1=from start, 2=entire).

erase_from_cursor_to_end(emulator)

Erases from cursor position to end of screen.

erase_from_start_to_cursor(emulator)

Erases from start of screen to cursor position.

erase_in_display(emulator, mode)

Erases content within the display.

erase_in_line(emulator, mode)

Erases content within the current line.

erase_line(emulator, mode)

Erases line content based on mode.

get_config_struct(emulator)

Gets the configuration structure.

get_cursor_position(emulator)

Gets the current cursor position as {x, y}.

get_cursor_position_struct(emulator)

Gets cursor position as a structured object.

get_cursor_style(emulator)

Gets the current cursor style (:block, :line, :underscore).

get_cursor_visible(emulator)

Gets cursor visibility state.

get_height(emulator)

Gets the terminal height in rows.

get_mode_manager(emulator)

Gets the mode manager.

get_mode_manager_cursor_visible(emulator)

Gets cursor visibility from mode manager.

get_output(emulator)

@spec get_output(t()) :: String.t()

Gets output from the emulator.

get_output_buffer(emulator)

Gets output buffer (legacy compatibility).

get_screen_buffer(arg1)

Gets the active screen buffer.

get_scroll_region(emulator)

Gets the current scroll region as {top, bottom}.

get_scrollback(emulator)

Gets the scrollback buffer contents.

get_selection(emulator)

Gets the currently selected text.

get_width(emulator)

Gets the terminal width in columns.

handle_esc_equals(emulator)

Handles ESC = sequence (DECKPAM - Enable application keypad mode).

handle_esc_greater(emulator)

Handles ESC > sequence (DECKPNM - Disable application keypad mode).

has_selection?(emulator)

Returns true if text is currently selected.

insert_char(emulator, char)

Inserts a character at the cursor position.

insert_chars(emulator, count)

Inserts the specified number of blank characters.

maybe_scroll(emulator)

Performs automatic scrolling if needed.

move_cursor(emulator, x, y)

Moves the cursor to the specified position.

move_cursor_back(emulator, count)

Moves cursor back (stub implementation).

move_cursor_down(emulator, count)

Moves cursor down (stub implementation).

move_cursor_forward(emulator, count)

Moves cursor forward (stub implementation).

move_cursor_to(emulator, x, y)

Moves cursor to specified position.

move_cursor_to(emulator, x, y, opts)

Moves cursor to specified position with options.

move_cursor_up(emulator, count)

Moves cursor up (stub implementation).

new()

Creates a new terminal emulator.

Options

  • :use_genservers - Use GenServer processes for state management (default: false for performance)
  • :enable_history - Enable command history tracking (default: true)
  • :scrollback_limit - Number of scrollback lines (default: 1000)
  • :alternate_buffer - Create alternate screen buffer (default: true)
  • :session_id - Session identifier
  • :client_options - Client-specific options

Examples

# Simple creation (no GenServers, optimized for performance)
emulator = Emulator.new(80, 24)

# Full featured with GenServers (for concurrent operations)
emulator = Emulator.new(80, 24, use_genservers: true)

# Minimal for benchmarking (no history, no alternate buffer)
emulator = Emulator.new(80, 24, enable_history: false, alternate_buffer: false)

new(width, height)

Creates a new terminal emulator with specified dimensions.

new(width, height, opts)

Creates a new terminal emulator with options.

new(width, height, session_id, client_options)

Creates a new terminal emulator with session ID and client options.

new_lite(width \\ 80, height \\ 24, opts \\ [])

This function is deprecated. Use new/3 with use_genservers: false option instead.

new_minimal(width \\ 80, height \\ 24)

This function is deprecated. Use new/3 with enable_history: false, alternate_buffer: false options instead.

process_input(emulator, input)

Processes input and returns updated emulator with output.

render_screen(emulator)

@spec render_screen(t()) :: String.t()

Renders the emulator screen.

reset(emulator)

Resets the terminal emulator to its initial state.

reset_mode(emulator, mode)

Resets a terminal mode.

resize(emulator, new_width, new_height)

Resizes the terminal to the specified dimensions.

restore_state(emulator)

Restores the previously saved terminal state.

save_state(emulator)

Saves the current terminal state.

scroll_down(emulator, lines)

Scrolls the display down by the specified number of lines.

scroll_up(emulator, lines)

Scrolls the display up by the specified number of lines.

set_attribute(emulator, attr, value)

Sets a terminal attribute.

set_cursor_blink(emulator, blinking)

Sets cursor blinking state.

set_cursor_position(emulator, x, y)

Sets the cursor position to the specified coordinates.

set_cursor_style(emulator, style)

Sets the cursor style to :block, :line, or :underscore.

set_cursor_visibility(emulator, visible)

Sets cursor visibility.

set_dimensions(emulator, width, height)

Sets terminal dimensions after validation.

set_mode(emulator, mode)

Sets a terminal mode.

start_link(opts \\ [])

Starts a linked terminal emulator process.

This function starts a GenServer-based terminal emulator that can handle terminal operations asynchronously.

Options

  • :width - Terminal width in columns (default: 80)
  • :height - Terminal height in rows (default: 24)
  • :name - Optional process name for registration
  • :session_id - Optional session identifier

Examples

{:ok, pid} = Emulator.start_link(width: 120, height: 40)

start_selection(emulator, x, y)

Starts text selection at the specified coordinates.

switch_to_alternate_screen(emulator)

Switches to the alternate screen buffer.

switch_to_normal_screen(emulator)

Switches to the normal screen buffer.

update_active_buffer(emulator, buffer)

Updates the active buffer with new content.

update_auto_wrap_mode(emulator, enabled)

Updates auto wrap mode state.

update_insert_mode(emulator, enabled)

Updates insert mode state.

update_selection(emulator, x, y)

Updates the selection endpoint to the specified coordinates.

validate_dimensions(width, height)

Validates terminal dimensions.

visible?(emulator)

Returns cursor visibility state.

write_text(emulator, text)

Writes text to the terminal at the cursor position.

write_to_output(emulator, data)

Writes data to the output buffer.