Skip to main content

Crate fusabi_tui_scarab

Crate fusabi_tui_scarab 

Source
Expand description

Scarab shared memory backend for Fusabi TUI.

This crate provides integration between the Fusabi TUI framework and the Scarab terminal emulator’s shared memory protocol. It enables zero-copy rendering of TUI applications directly to Scarab’s terminal buffer.

§Architecture

Scarab uses a split-process architecture with shared memory for IPC:

  • Daemon: Headless server that owns PTY processes
  • Client: Bevy-based GUI that renders via shared memory
  • Plugins: Can run in either process with Fusabi scripting

This crate provides:

  • ScarabRenderer: Renderer implementation that writes to shared memory
  • SharedCell / SharedState: Zero-copy types matching Scarab’s protocol
  • Conversion utilities between Fusabi and Scarab types
  • Plugin trait for building TUI applications

§Examples

§Basic Rendering

use fusabi_tui_scarab::renderer::ScarabRenderer;
use fusabi_tui_render::renderer::Renderer;
use fusabi_tui_core::buffer::Buffer;
use fusabi_tui_core::layout::Rect;
use fusabi_tui_core::style::{Style, Color};

// Connect to Scarab's shared memory
let mut renderer = ScarabRenderer::connect(None)?;

// Create a buffer and draw to it
let size = renderer.size()?;
let mut buffer = Buffer::new(size);

let style = Style::new().fg(Color::Green);
buffer.set_string(0, 0, "Hello, Scarab!", style);

// Render to shared memory
renderer.draw(&buffer)?;
renderer.flush()?;

§Plugin Development

use fusabi_tui_scarab::plugin::{TuiPlugin, PluginContext, RenderContext, InputEvent, Action};
use fusabi_tui_scarab::error::Result;
use fusabi_tui_core::buffer::Buffer;
use fusabi_tui_core::style::{Style, Color};

struct MyPlugin {
    counter: u32,
}

impl TuiPlugin for MyPlugin {
    fn on_init(&mut self, ctx: &PluginContext) -> Result<()> {
        println!("Plugin initialized!");
        Ok(())
    }

    fn on_render(&mut self, ctx: &RenderContext) -> Result<Buffer> {
        let mut buffer = Buffer::new(ctx.size);
        let style = Style::new().fg(Color::Cyan);

        let text = format!("Frame: {}", ctx.frame);
        buffer.set_string(0, 0, &text, style);

        Ok(buffer)
    }

    fn on_input(&mut self, event: InputEvent) -> Result<Action> {
        self.counter += 1;
        Ok(Action::Redraw)
    }
}

§Shared Memory Layout

The shared memory region contains a SharedState structure with:

  • Sequence number for lock-free synchronization
  • Cursor position
  • 200x100 grid of cells (each 16 bytes)

All structures use #[repr(C)] and bytemuck::Pod for zero-copy safety.

§Features

  • plugin: Enables plugin system with JSON serialization (optional)

Re-exports§

pub use error::Result;
pub use error::ScarabError;
pub use renderer::ScarabRenderer;
pub use shared::SharedCell;
pub use shared::SharedState;
pub use shared::GRID_HEIGHT;
pub use shared::GRID_WIDTH;
pub use shared::SHMEM_PATH;

Modules§

convert
Conversion utilities between Fusabi TUI and Scarab types.
error
Error types for Scarab shared memory integration.
plugin
Plugin trait for Scarab TUI plugins.
prelude
Prelude module for convenient imports. Convenient re-exports for common types and traits.
renderer
Scarab shared memory renderer implementation.
shared
Shared memory types matching Scarab’s protocol.