Behaviour specification for terminal drivers.
This behaviour defines the contract that terminal drivers must implement to provide terminal I/O functionality. Different implementations can provide native (NIF-based), pure Elixir, or web-based terminal support.
Implementations
Raxol.Terminal.Driver- Main driver with automatic backend selectionRaxol.Terminal.IOTerminal- Pure Elixir implementation
Example Implementation
defmodule MyTerminalDriver do
@behaviour Raxol.Terminal.Driver.Behaviour
@impl true
def init(opts) do
# Initialize terminal
{:ok, %{}}
end
@impl true
def shutdown(state) do
# Cleanup
:ok
end
# ... implement other callbacks
end
Summary
Types
Cell attributes
Color specification
Terminal dimensions
Terminal event
Driver state - implementation specific
Callbacks
Clear the entire screen.
Clear from cursor to end of line.
Flush any buffered output to the terminal.
Get the current cursor position.
Get the current terminal dimensions.
Initialize the terminal driver.
Move the cursor to the specified position.
Poll for input events.
Reset all styling to defaults.
Set text attributes for subsequent output.
Set the background color for subsequent output.
Enable or disable bracketed paste mode.
Show or hide the cursor.
Set the foreground color for subsequent output.
Enable or disable mouse input.
Set the terminal title.
Shut down the terminal driver.
Write a string at the current cursor position.
Write a string at the specified position with optional attributes.
Types
@type attributes() :: %{ optional(:fg) => color(), optional(:bg) => color(), optional(:bold) => boolean(), optional(:italic) => boolean(), optional(:underline) => boolean(), optional(:reverse) => boolean(), optional(:blink) => boolean() }
Cell attributes
@type color() :: atom() | non_neg_integer() | {r :: 0..255, g :: 0..255, b :: 0..255}
Color specification
@type dimensions() :: {width :: pos_integer(), height :: pos_integer()}
Terminal dimensions
@type event() :: {:key, key_data :: map()} | {:mouse, mouse_data :: map()} | {:resize, width :: pos_integer(), height :: pos_integer()} | {:paste, content :: String.t()}
Terminal event
@type state() :: term()
Driver state - implementation specific
Callbacks
Clear the entire screen.
Clear from cursor to end of line.
Flush any buffered output to the terminal.
@callback get_cursor_position(state()) :: {:ok, {x :: non_neg_integer(), y :: non_neg_integer()}} | {:error, term()}
Get the current cursor position.
@callback get_size(state()) :: {:ok, dimensions()} | {:error, term()}
Get the current terminal dimensions.
Returns the width and height in character cells.
Initialize the terminal driver.
Called when the driver process starts. Should set up the terminal for raw input mode and prepare for rendering.
Options
Implementation-specific options may include:
:width- Initial width (for testing):height- Initial height (for testing):output_device- Output device (default: :stdio)
Returns
{:ok, state}- Initialization successful{:error, reason}- Initialization failed
@callback move_cursor(state(), x :: non_neg_integer(), y :: non_neg_integer()) :: {:ok, state()} | {:error, term()}
Move the cursor to the specified position.
Coordinates are 0-indexed, with (0, 0) at the top-left corner.
@callback poll_event(state(), timeout :: non_neg_integer()) :: {:ok, event(), state()} | {:timeout, state()} | {:error, term()}
Poll for input events.
Returns the next available event or :timeout if no event is available
within the specified timeout (in milliseconds).
Reset all styling to defaults.
@callback set_attributes(state(), attributes()) :: {:ok, state()} | {:error, term()}
Set text attributes for subsequent output.
Set the background color for subsequent output.
Enable or disable bracketed paste mode.
This is an optional callback - implementations may return {:ok, state}
without taking action if not supported.
Show or hide the cursor.
Set the foreground color for subsequent output.
Enable or disable mouse input.
This is an optional callback - implementations may return {:ok, state}
without taking action if not supported.
Set the terminal title.
This is an optional callback - implementations may return {:ok, state}
without taking action if not supported.
@callback shutdown(state()) :: :ok
Shut down the terminal driver.
Called when the driver process terminates. Should restore the terminal to its original state.
Write a string at the current cursor position.
@callback write_at( state(), x :: non_neg_integer(), y :: non_neg_integer(), content :: String.t(), attrs :: attributes() ) :: {:ok, state()} | {:error, term()}
Write a string at the specified position with optional attributes.