Raxol.Terminal.Graphics.GraphicsServer (Raxol v2.0.1)
View SourceProvides unified graphics capabilities for the terminal emulator with automatic protocol detection and fallback support.
This module handles:
- Automatic graphics protocol detection (Kitty, iTerm2, Sixel)
- Protocol-agnostic image display interface
- Graceful fallback between supported protocols
- Graphics state management and context switching
- Performance optimization and caching
Supported Protocols
- Kitty Graphics Protocol - High-performance, supports animations and transparency
- iTerm2 Inline Images - Good compatibility, limited animation support
- Sixel Graphics - Wide terminal support, basic image display
- Fallback Mode - ASCII art conversion for unsupported terminals
Usage
# Automatic protocol detection and image display
{:ok, _} = GraphicsServer.display_image(image_data, %{
width: 300,
height: 200,
format: :png
})
# Query available protocols
graphics_info = GraphicsServer.get_graphics_info()
Summary
Functions
Returns a specification to start this module under a supervisor.
Cleans up resources.
Clears the graphics context.
Closes a graphics context.
Converts an image from one format to another with display optimization.
Creates an animation from multiple image frames.
Creates a new graphics context with the given configuration.
Displays an image using the best available graphics protocol.
Processes and displays multiple images with optimized format conversion.
Gets the active graphics context ID.
Gets the list of all graphics contexts.
Gets information about available graphics protocols and capabilities.
Gets the state of a specific graphics context.
Gets comprehensive information about a processed image.
Callback implementation for Raxol.Core.Behaviours.BaseManager.handle_manager_cast/2.
Callback implementation for Raxol.Core.Behaviours.BaseManager.handle_manager_info/2.
Manages the image cache for performance optimization.
Creates optimized variants for different terminal capabilities.
Renders graphics data to the specified context.
Sets the active graphics context.
Sets the preferred graphics protocol for future operations.
Updates the graphics manager configuration.
Updates the configuration of a specific graphics context.
Updates properties of an existing graphics element for animation purposes.
Types
@type display_options() :: %{ optional(:x) => non_neg_integer(), optional(:y) => non_neg_integer(), optional(:width) => non_neg_integer(), optional(:height) => non_neg_integer(), optional(:scale) => float(), optional(:preserve_aspect_ratio) => boolean(), optional(:protocol) => graphics_protocol(), optional(:quality) => 1..100, optional(:dither) => :none | :riemersma | :floyd_steinberg | :ordered, optional(:color_optimization) => boolean(), optional(:cache_enabled) => boolean(), optional(:processing_options) => map() }
@type graphics_config() :: %{ optional(:width) => non_neg_integer(), optional(:height) => non_neg_integer(), optional(:format) => image_format(), optional(:compression) => :none | :zlib | :lz4, optional(:quality) => 0..100, optional(:protocol) => graphics_protocol(), optional(:fallback_enabled) => boolean() }
@type graphics_id() :: non_neg_integer()
@type graphics_protocol() :: :kitty | :iterm2 | :sixel | :fallback
@type graphics_state() :: :active | :inactive | :hidden
@type image_format() :: :rgb | :rgba | :png | :jpeg | :webp | :gif
Functions
Returns a specification to start this module under a supervisor.
See Supervisor.
@spec cleanup() :: :ok
Cleans up resources.
@spec clear_graphics(graphics_id()) :: :ok | {:error, term()}
Clears the graphics context.
@spec close_graphics(graphics_id()) :: :ok | {:error, term()}
Closes a graphics context.
@spec convert_and_display(binary(), image_format(), display_options()) :: {:ok, graphics_id()} | {:error, term()}
Converts an image from one format to another with display optimization.
Parameters
image_data- Source image datatarget_format- Target format (:png, :jpeg, :webp, :gif)options- Conversion and display options
Returns
{:ok, graphics_id}- Successfully converted and displayed{:error, reason}- Conversion failed
@spec create_animation([binary()], map()) :: {:ok, graphics_id()} | {:error, term()}
Creates an animation from multiple image frames.
Parameters
frames- List of image data for animation framesoptions- Animation options (frame_delay, loop_count, protocol)
Returns
{:ok, graphics_id}- Successfully created animation{:error, reason}- Error with reason
@spec create_graphics(map()) :: {:ok, graphics_id()} | {:error, term()}
Creates a new graphics context with the given configuration.
@spec display_image(binary() | String.t(), display_options()) :: {:ok, graphics_id()} | {:error, term()}
Displays an image using the best available graphics protocol.
Automatically detects the optimal protocol and falls back gracefully if the preferred protocol is not supported.
Parameters
image_data- Binary image data or file pathoptions- Display options including size, position, format
Returns
{:ok, graphics_id}- Successfully displayed image{:error, reason}- Error with reason
Examples
# Display PNG image with automatic protocol detection
{:ok, id} = GraphicsServer.display_image(png_data, %{
width: 300,
height: 200,
format: :png
})
# Display with specific protocol preference
{:ok, id} = GraphicsServer.display_image(image_data, %{
width: 400,
height: 300,
protocol: :kitty,
fallback_enabled: true
})
@spec display_images( [binary() | {binary(), display_options()}], display_options() ) :: {:ok, [graphics_id()]} | {:error, term()}
Processes and displays multiple images with optimized format conversion.
Automatically detects image formats, applies appropriate processing, and selects the best display protocol for each image.
Parameters
images- List of image data or {data, options} tuplesshared_options- Options applied to all images
Returns
{:ok, [graphics_id]}- Successfully processed and displayed images{:error, reason}- Processing failed
Examples
# Process multiple images with shared settings
{:ok, ids} = GraphicsServer.display_images([
png_data,
{jpeg_data, %{quality: 95}},
svg_data
], %{
width: 300,
height: 200,
optimize_for_terminal: true
})
@spec get_active_graphics() :: {:ok, graphics_id()} | {:error, :no_active_graphics}
Gets the active graphics context ID.
@spec get_graphics() :: [graphics_id()]
Gets the list of all graphics contexts.
@spec get_graphics_info() :: map()
Gets information about available graphics protocols and capabilities.
Returns
A map containing:
:supported_protocols- List of supported protocols:preferred_protocol- Recommended protocol for best performance:terminal_info- Detected terminal information:capabilities- Protocol-specific capabilities
Examples
iex> GraphicsServer.get_graphics_info()
%{
supported_protocols: [:kitty, :sixel],
preferred_protocol: :kitty,
terminal_info: %{type: :kitty, version: "0.26.5"},
capabilities: %{
kitty: %{max_image_size: 100_000_000, supports_animation: true},
sixel: %{max_image_size: 1_000_000, supports_animation: false}
}
}
@spec get_graphics_state(graphics_id()) :: {:ok, map()} | {:error, term()}
Gets the state of a specific graphics context.
@spec get_image_info(graphics_id()) :: {:ok, map()} | {:error, term()}
Gets comprehensive information about a processed image.
Parameters
graphics_id- ID of previously processed image
Returns
{:ok, image_info}- Image metadata and processing information{:error, reason}- Image not found or error
Callback implementation for Raxol.Core.Behaviours.BaseManager.handle_manager_cast/2.
Callback implementation for Raxol.Core.Behaviours.BaseManager.handle_manager_info/2.
Manages the image cache for performance optimization.
Parameters
action- Cache action (:get_stats, :clear, :configure)params- Optional parameters for the action
Returns
- Cache operation result
@spec optimize_and_display(binary(), display_options()) :: {:ok, graphics_id()} | {:error, term()}
Creates optimized variants for different terminal capabilities.
Generates multiple versions of an image optimized for different terminal types and automatically selects the best one to display.
Parameters
image_data- Source image dataoptions- Processing and display options
Returns
{:ok, graphics_id}- Successfully optimized and displayed{:error, reason}- Optimization failed
@spec render_graphics(graphics_id(), binary()) :: :ok | {:error, term()}
Renders graphics data to the specified context.
@spec set_active_graphics(graphics_id()) :: :ok | {:error, term()}
Sets the active graphics context.
@spec set_preferred_protocol(graphics_protocol() | :auto, boolean()) :: :ok | {:error, term()}
Sets the preferred graphics protocol for future operations.
Parameters
protocol- Preferred protocol (:kitty, :iterm2, :sixel, :auto)fallback_enabled- Whether to enable fallback to other protocols
Returns
:ok- Protocol preference updated{:error, reason}- Error with reason
@spec update_config(map()) :: :ok
Updates the graphics manager configuration.
@spec update_graphics_config(graphics_id(), graphics_config()) :: :ok | {:error, term()}
Updates the configuration of a specific graphics context.
@spec update_graphics_properties(graphics_id(), map()) :: :ok | {:error, term()}
Updates properties of an existing graphics element for animation purposes.
Parameters
graphics_id- ID of the graphics element to updateproperties- Map of properties to update (opacity, position, scale, etc.)
Returns
:ok- Properties updated successfully{:error, reason}- Update failed