Something went wrong. Try again.
Monorepo for the nibling.run platform
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879// The slopbox terminal: the only module that touches the terminal emulator. build.mjs// bundles it to dist/demo/terminal.js, with xterm's stylesheet inlined as text. Swapping the// emulator (ghostty-web speaks the same API) means rewriting this file and nothing else.//// xterm writes runtime CSS (theme colours, cell metrics) into <style> elements it// creates, so the page needs style-src 'unsafe-inline'; scripts/serve.mjs grants it to// the slopbox page only.import { Terminal } from "@xterm/xterm";import { FitAddon } from "@xterm/addon-fit";import css from "@xterm/xterm/css/xterm.css";
const FONT = 'ui-monospace, "SF Mono", Menlo, Consolas, monospace';
function injectCss() { if (document.getElementById("terminal-css")) return; const style = document.createElement("style"); style.id = "terminal-css"; style.textContent = css; document.head.append(style);}
/** * @typedef {import("@xterm/xterm").ITheme} Theme * @param {HTMLElement} el * @param {{ theme: { light: Theme, dark: Theme } }} options the palette for each * colour scheme; the terminal follows prefers-color-scheme while it lives. */export function create(el, { theme }) { injectCss(); const dark = matchMedia("(prefers-color-scheme: dark)"); const term = new Terminal({ fontFamily: FONT, fontSize: 13, lineHeight: 1.25, cursorBlink: true, scrollback: 5000, theme: dark.matches ? theme.dark : theme.light, }); const fitter = new FitAddon(); term.loadAddon(fitter); term.open(el); const onScheme = () => { term.options.theme = dark.matches ? theme.dark : theme.light; }; dark.addEventListener("change", onScheme);
return { /** @param {string | Uint8Array} data */ write(data) { term.write(data); }, /** Keystrokes and pastes, as the strings a pty master expects. @param {(data: string) => void} cb */ onData(cb) { term.onData(cb); }, /** @param {(size: { cols: number, rows: number }) => void} cb */ onResize(cb) { term.onResize(({ cols, rows }) => cb({ cols, rows })); }, /** Fit the grid to the element and return the size it settled on. */ fit() { fitter.fit(); return { cols: term.cols, rows: term.rows }; }, focus() { term.focus(); }, /** Test hook: the active buffer as text, one line per row, trailing blanks trimmed. */ readBuffer() { const buf = term.buffer.active; const lines = []; for (let i = 0; i < buf.length; i++) lines.push(buf.getLine(i)?.translateToString(true) ?? ""); return lines.join("\n").trimEnd(); }, dispose() { dark.removeEventListener("change", onScheme); term.dispose(); }, };}