ghostty-web-umd-bindgen 0.1.0

wasm-bindgen bindings for the ghostty-web UMD global GhosttyWeb
Documentation
//! `bon` builders for dictionary option types.
//!
//! These construct the JS option bags bound in [`crate`] (`ITheme`,
//! [`ITerminalOptions`], [`RendererOptions`], [`GhosttyTerminalConfig`],
//! [`KeyEvent`]).

use bon::builder;
use js_sys::Array;
use js_sys::Number;

use crate::Ghostty;
use crate::GhosttyTerminalConfig;
use crate::ITerminalOptions;
use crate::ITheme;
use crate::Key;
use crate::KeyAction;
use crate::KeyEvent;
use crate::Mods;
use crate::RendererOptions;

/// Build an [`ITheme`] color theme object.
///
/// # Example
///
/// ```ignore
/// use ghostty_web_umd_bindgen::builders::theme;
///
/// let t = theme()
///     .foreground("#c0caf5".into())
///     .background("#1a1b26".into())
///     .call();
/// ```
#[builder]
pub fn theme(
    foreground: Option<String>,
    background: Option<String>,
    cursor: Option<String>,
    cursor_accent: Option<String>,
    selection_background: Option<String>,
    selection_foreground: Option<String>,
    black: Option<String>,
    red: Option<String>,
    green: Option<String>,
    yellow: Option<String>,
    blue: Option<String>,
    magenta: Option<String>,
    cyan: Option<String>,
    white: Option<String>,
    bright_black: Option<String>,
    bright_red: Option<String>,
    bright_green: Option<String>,
    bright_yellow: Option<String>,
    bright_blue: Option<String>,
    bright_magenta: Option<String>,
    bright_cyan: Option<String>,
    bright_white: Option<String>,
) -> ITheme
{
    let o = ITheme::new();
    if let Some(v) = foreground
    {
        o.set_foreground(&v);
    }
    if let Some(v) = background
    {
        o.set_background(&v);
    }
    if let Some(v) = cursor
    {
        o.set_cursor(&v);
    }
    if let Some(v) = cursor_accent
    {
        o.set_cursor_accent(&v);
    }
    if let Some(v) = selection_background
    {
        o.set_selection_background(&v);
    }
    if let Some(v) = selection_foreground
    {
        o.set_selection_foreground(&v);
    }
    if let Some(v) = black
    {
        o.set_black(&v);
    }
    if let Some(v) = red
    {
        o.set_red(&v);
    }
    if let Some(v) = green
    {
        o.set_green(&v);
    }
    if let Some(v) = yellow
    {
        o.set_yellow(&v);
    }
    if let Some(v) = blue
    {
        o.set_blue(&v);
    }
    if let Some(v) = magenta
    {
        o.set_magenta(&v);
    }
    if let Some(v) = cyan
    {
        o.set_cyan(&v);
    }
    if let Some(v) = white
    {
        o.set_white(&v);
    }
    if let Some(v) = bright_black
    {
        o.set_bright_black(&v);
    }
    if let Some(v) = bright_red
    {
        o.set_bright_red(&v);
    }
    if let Some(v) = bright_green
    {
        o.set_bright_green(&v);
    }
    if let Some(v) = bright_yellow
    {
        o.set_bright_yellow(&v);
    }
    if let Some(v) = bright_blue
    {
        o.set_bright_blue(&v);
    }
    if let Some(v) = bright_magenta
    {
        o.set_bright_magenta(&v);
    }
    if let Some(v) = bright_cyan
    {
        o.set_bright_cyan(&v);
    }
    if let Some(v) = bright_white
    {
        o.set_bright_white(&v);
    }
    return o;
}

/// Build [`ITerminalOptions`] for [`crate::Terminal::new`].
///
/// # Example
///
/// ```ignore
/// use ghostty_web_umd_bindgen::builders::terminal_options;
///
/// let opts = terminal_options()
///     .cols(80)
///     .rows(24)
///     .font_size(14.0)
///     .cursor_style("block".into())
///     .call();
/// ```
#[builder]
pub fn terminal_options(
    cols: Option<u32>,
    rows: Option<u32>,
    cursor_blink: Option<bool>,
    /// One of `"block"`, `"underline"`, or `"bar"`.
    cursor_style: Option<String>,
    theme: Option<ITheme>,
    scrollback: Option<u32>,
    font_size: Option<f64>,
    font_family: Option<String>,
    allow_transparency: Option<bool>,
    convert_eol: Option<bool>,
    disable_stdin: Option<bool>,
    smooth_scroll_duration: Option<f64>,
    /// Optional pre-loaded [`Ghostty`] instance (for test isolation).
    ghostty: Option<Ghostty>,
) -> ITerminalOptions
{
    let o = ITerminalOptions::new();
    if let Some(v) = cols
    {
        o.set_cols(v);
    }
    if let Some(v) = rows
    {
        o.set_rows(v);
    }
    if let Some(v) = cursor_blink
    {
        o.set_cursor_blink(v);
    }
    if let Some(v) = cursor_style
    {
        o.set_cursor_style(&v);
    }
    if let Some(v) = theme
    {
        o.set_theme(&v);
    }
    if let Some(v) = scrollback
    {
        o.set_scrollback(v);
    }
    if let Some(v) = font_size
    {
        o.set_font_size(v);
    }
    if let Some(v) = font_family
    {
        o.set_font_family(&v);
    }
    if let Some(v) = allow_transparency
    {
        o.set_allow_transparency(v);
    }
    if let Some(v) = convert_eol
    {
        o.set_convert_eol(v);
    }
    if let Some(v) = disable_stdin
    {
        o.set_disable_stdin(v);
    }
    if let Some(v) = smooth_scroll_duration
    {
        o.set_smooth_scroll_duration(v);
    }
    if let Some(v) = ghostty
    {
        o.set_ghostty(&v);
    }
    return o;
}

/// Build [`RendererOptions`] for [`crate::CanvasRenderer::new`].
///
/// # Example
///
/// ```ignore
/// use ghostty_web_umd_bindgen::builders::renderer_options;
///
/// let opts = renderer_options()
///     .font_size(14.0)
///     .cursor_blink(true)
///     .call();
/// ```
#[builder]
pub fn renderer_options(
    font_size: Option<f64>,
    font_family: Option<String>,
    /// One of `"block"`, `"underline"`, or `"bar"`.
    cursor_style: Option<String>,
    cursor_blink: Option<bool>,
    theme: Option<ITheme>,
    device_pixel_ratio: Option<f64>,
) -> RendererOptions
{
    let o = RendererOptions::new();
    if let Some(v) = font_size
    {
        o.set_font_size(v);
    }
    if let Some(v) = font_family
    {
        o.set_font_family(&v);
    }
    if let Some(v) = cursor_style
    {
        o.set_cursor_style(&v);
    }
    if let Some(v) = cursor_blink
    {
        o.set_cursor_blink(v);
    }
    if let Some(v) = theme
    {
        o.set_theme(&v);
    }
    if let Some(v) = device_pixel_ratio
    {
        o.set_device_pixel_ratio(v);
    }
    return o;
}

/// Build [`GhosttyTerminalConfig`] (passed to
/// `ghostty_terminal_new_with_config`).
///
/// All color values use `0xRRGGBB` format. A value of `0` means "use default".
///
/// # Example
///
/// ```ignore
/// use ghostty_web_umd_bindgen::builders::terminal_config;
///
/// let cfg = terminal_config()
///     .scrollback_limit(10_000)
///     .fg_color(0xc0caf5)
///     .bg_color(0x1a1b26)
///     .call();
/// ```
#[builder]
pub fn terminal_config(
    scrollback_limit: Option<u32>,
    /// Foreground color as `0xRRGGBB` (`0` = use default).
    fg_color: Option<u32>,
    /// Background color as `0xRRGGBB` (`0` = use default).
    bg_color: Option<u32>,
    /// Cursor color as `0xRRGGBB` (`0` = use default).
    cursor_color: Option<u32>,
    /// 16-color palette entries as `0xRRGGBB` values.
    palette: Option<Array<Number>>,
) -> GhosttyTerminalConfig
{
    let o = GhosttyTerminalConfig::new();
    if let Some(v) = scrollback_limit
    {
        o.set_scrollback_limit(v);
    }
    if let Some(v) = fg_color
    {
        o.set_fg_color(v);
    }
    if let Some(v) = bg_color
    {
        o.set_bg_color(v);
    }
    if let Some(v) = cursor_color
    {
        o.set_cursor_color(v);
    }
    if let Some(v) = palette
    {
        o.set_palette(&v);
    }
    return o;
}

/// Build a [`KeyEvent`] for [`crate::KeyEncoder::encode`].
///
/// # Example
///
/// ```ignore
/// use ghostty_web_umd_bindgen::builders::{key_event, Key, KeyAction, Mods};
///
/// let event = key_event()
///     .action(KeyAction::Press)
///     .key(Key::A)
///     .mods(Mods::CTRL)
///     .call();
/// ```
#[builder]
pub fn key_event(
    action: KeyAction,
    key: Key,
    mods: Mods,
    consumed_mods: Option<Mods>,
    composing: Option<bool>,
    utf8: Option<String>,
    unshifted_codepoint: Option<u32>,
) -> KeyEvent
{
    let o = KeyEvent::new();
    o.set_action(action as u32);
    o.set_key(key as u32);
    o.set_mods(mods.bits());
    if let Some(v) = consumed_mods
    {
        o.set_consumed_mods(v.bits());
    }
    if let Some(v) = composing
    {
        o.set_composing(v);
    }
    if let Some(v) = utf8
    {
        o.set_utf8(&v);
    }
    if let Some(v) = unshifted_codepoint
    {
        o.set_unshifted_codepoint(v);
    }
    return o;
}