Files
uutils.github.io/static/js/wasm-example.js
T
Sylvestre Ledru 5c22a5b3d6 Add interactive WASM playground for uutils coreutils
A browser-based terminal that runs real Rust coreutils compiled to
WebAssembly (wasm32-wasip1) using @bjorn3/browser_wasi_shim.

Features:
- xterm.js terminal with command history and line editing
- Pipe support (cmd1 | cmd2 | cmd3), file redirects (>)
- Persistent virtual filesystem across commands
- cd/pwd builtins with virtual working directory
- Locale support (locale builtin + selector)
- UTF-8/multibyte correct output (raw ConsoleStdout, fixed args_sizes_get)
- Click-to-run example buttons
- Inline wasm_example shortcode for blog posts
2026-04-03 21:47:38 +02:00

51 lines
1.4 KiB
JavaScript

/**
* Inline "Run" button handler for uutils code examples.
* Lightweight alternative to the full terminal playground.
*/
(function () {
let runtimeLoaded = false;
async function ensureRuntime() {
if (runtimeLoaded) return;
// Load the terminal runtime which provides uutilsExecute
const script = document.createElement("script");
script.src = "/js/wasm-terminal.js";
await new Promise((resolve, reject) => {
script.onload = resolve;
script.onerror = reject;
document.head.appendChild(script);
});
runtimeLoaded = true;
}
async function runExample(button) {
const container = button.closest(".wasm-example");
if (!container) return;
const command = container.dataset.command;
const outputEl = container.querySelector(".wasm-example-output");
const preEl = outputEl.querySelector("pre");
// Show loading state
button.disabled = true;
button.textContent = "Running...";
outputEl.style.display = "block";
preEl.textContent = "Loading...";
try {
await ensureRuntime();
const output = await window.uutilsExecute(command);
preEl.textContent = output || "(no output)";
} catch (e) {
preEl.textContent = "Error: " + e.message;
}
button.disabled = false;
button.textContent = "Run";
}
// Expose globally
window.runWasmExample = runExample;
})();