clipboard

    clipboard
    subprocess
    cross-platform
    Download zip
    Author
    Version
    0.1.1
    License
    MIT
    Last updated
    6 months ago
    Downloads
    73

    #clipboard.mbt

    A cross-platform clipboard library for MoonBit, built on subprocess-based native backends.

    #Features

    • Read & write clipboard text with a small async API.
    • Watch for clipboard changes via a polling background task.
    • Cross-platform: macOS (pbcopy / pbpaste), Linux X11 (xclip), Linux Wayland (wl-copy / wl-paste), and Windows (PowerShell). The OS is detected automatically.
    • In-memory backend for tests and headless hosts.

    #Install

    moon add mst-mkt/clipboard

    #Usage

    async test "read and write" {
    let cb = @clipboard.Clipboard::new_in_memory()
    cb.set_text("hello")
    inspect(cb.get_text(), content="hello")
    }

    Use @clipboard.Clipboard::new() instead of new_in_memory() to bind to the real OS clipboard.

    All clipboard operations are async (the transport spawns subprocesses), so call them from an async context — async fn, async test, or inside @async.with_task_group.

    #Watching for changes

    @async.with_task_group(group => {
    let cb = @clipboard.Clipboard::new()
    let handle = cb.watch(group, value => println("clipboard changed: \{value}"))
    // ... later
    handle.stop()
    })

    watch seeds a baseline on the first read, then invokes the callback only on subsequent transitions. stop is idempotent and cancels the watcher immediately.

    #API

    FunctionDescription
    Clipboard::new()Bind to the host OS's native backend (detects the OS, may spawn uname).
    Clipboard::new_in_memory()In-memory clipboard; never raises.
    get_text()Read clipboard text.
    set_text(text)Write clipboard text.
    watch(group, on_change)Poll for changes; returns a Handle.
    Handle::stop()Stop a watcher.

    #Errors

    Operations raise ClipboardError:

    • Empty — no text content available.
    • NotUtf8(len) — bytes are not valid UTF-8 (only the length is reported; raw bytes are never retained).
    • ToolNotFound(name) — the required CLI tool isn't installed.
    • Timeout(name) — the tool didn't respond in time.
    • Unsupported(msg) — the host OS isn't supported (e.g. headless Linux with no DISPLAY/WAYLAND_DISPLAY).
    • Backend(msg) — other platform-specific failure (carries the tool's stderr).
    • Cancelled — the operation was cancelled.

    #Notes

    • No internal locking. Concurrent calls against the same Clipboard may interleave subprocess invocations. Serialize with your own @async.semaphore if needed.
    • Linux persistence. On X11/Wayland, written content is held by a background xclip/wl-copy child and does not outlive your program. Use a dedicated clipboard manager (clipmenud, cliphist, …) if you need persistence. macOS and Windows persist automatically.

    #License

    Backend

    pub(open) trait Backend {
    async fn get_text(Self) -> String raise ClipboardError
    async fn set_text(Self, String) -> Unit raise ClipboardError
    }

    Pluggable backend trait. Most callers will not implement this directly; it exists so that InMemoryBackend, the subprocess backend, and any future native backends share a common interface that Clipboard dispatches to.

    ClipboardError

    pub(all) suberror ClipboardError {
    Empty
    NotUtf8(Int)
    ToolNotFound(String)
    Timeout(String)
    Unsupported(String)
    Backend(String)
    Cancelled
    } derive(Eq, ToJson,
    Debug
    )

    Error type returned by clipboard operations.

    Clipboard

    pub struct Clipboard {
    // private fields
    }

    Clipboard::get_text

    async fn Clipboard::get_text(self : Clipboard) -> String raise ClipboardError

    Clipboard::new

    async fn Clipboard::new() -> Clipboard raise ClipboardError

    Clipboard::new_in_memory

    fn Clipboard::new_in_memory() -> Clipboard

    Clipboard::set_text

    async fn Clipboard::set_text(self : Clipboard, text : String) -> Unit raise ClipboardError

    Clipboard::watch

    async fn Clipboard::watch(self : Clipboard, group :
    TaskGroup
    [Unit], on_change : (String) -> Unit) -> Handle

    Handle

    pub struct Handle {
    // private fields
    }

    Handle::stop

    fn Handle::stop(self : Handle) -> Unit