Skip to main content

charmed_wasm/
lib.rs

1//! # charmed-wasm
2//!
3//! Terminal UI components for the web, compiled to WebAssembly.
4//!
5//! This crate provides WebAssembly bindings for the `charmed_rust` library,
6//! allowing you to use lipgloss styling in web applications.
7//!
8//! ## Role in `charmed_rust`
9//!
10//! charmed-wasm is the web-facing bridge for the ecosystem:
11//! - **lipgloss** is re-exported with WASM-friendly APIs.
12//! - It enables sharing the same styling model between terminal and web UI.
13//!
14//! ## Quick Start (JavaScript)
15//!
16//! ```javascript
17//! import init, { newStyle, joinVertical, Position } from 'charmed-wasm';
18//!
19//! async function main() {
20//!     await init();
21//!
22//!     const style = newStyle()
23//!         .foreground("#ff00ff")
24//!         .background("#1a1a1a")
25//!         .bold()
26//!         .padding(1, 2, 1, 2);
27//!
28//!     const rendered = style.render("Hello, World!");
29//!     document.body.innerHTML = `<pre>${rendered}</pre>`;
30//! }
31//!
32//! main();
33//! ```
34//!
35//! ## Available APIs
36//!
37//! ### Styling (from lipgloss)
38//!
39//! - `newStyle()` - Create a new style builder
40//! - `JsStyle` - Chainable style configuration
41//! - `JsColor` - Color utilities
42//!
43//! ### Layout
44//!
45//! - `joinHorizontal(position, items)` - Join strings horizontally
46//! - `joinVertical(position, items)` - Join strings vertically
47//! - `place(width, height, hPos, vPos, content)` - Place content in a container
48//!
49//! ### Utilities
50//!
51//! - `stringWidth(s)` - Get visible width of a string
52//! - `stringHeight(s)` - Get height (line count) of a string
53//! - `borderPresets()` - Get available border preset names
54
55#![forbid(unsafe_code)]
56
57// Use wee_alloc for smaller binaries (optional)
58#[cfg(feature = "wee_alloc")]
59#[global_allocator]
60static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
61
62use wasm_bindgen::prelude::*;
63
64// Re-export everything from lipgloss wasm module
65pub use lipgloss::wasm::*;
66
67/// Module version information.
68#[must_use]
69#[wasm_bindgen(js_name = "version")]
70pub fn version() -> String {
71    env!("CARGO_PKG_VERSION").to_string()
72}
73
74/// Check if the module is properly initialized.
75#[must_use]
76#[wasm_bindgen(js_name = "isReady")]
77#[allow(clippy::missing_const_for_fn)] // wasm_bindgen doesn't support const fn
78pub fn is_ready() -> bool {
79    true
80}
81
82#[cfg(test)]
83mod tests {
84    use super::*;
85
86    #[test]
87    fn test_version() {
88        let v = version();
89        assert!(!v.is_empty());
90    }
91}