/** * KDL composition. * * A project ships a *fragment* (pane nodes only); sl0ppi wraps it in the tab * node that carries identity and cwd. Pure string composition, so there is no * KDL parser to keep in step with the fork's dialect, and sl0ppi keeps sole * control of the purpose. * * ALWAYS emit multi-line KDL. On a single line, KDL requires `;` terminators * between nodes: `tab x { pane }` is a parse error, which is a genuinely * confusing way to lose an afternoon. */ import { projectPurpose } from "./purpose.ts"; /** Quote a value for a KDL string property. */ export function kdlString(value: string): string { return `"${value.replace(/\\/g, "\\\\").replace(/"/g, '\\"')}"`; } /** * What a project gets when it has a `.sl0ppi/` marker but no `layout.kdl`. * * One suspended agent: every project wants a main agent (agreed), and * `start_suspended` is what stops `up` on a twelve-project root from launching * twelve of them. The pane exists, focused-and-keypress runs it. */ export const DEFAULT_FRAGMENT = `pane purpose="agent:main" start_suspended=true command="pi"`; export interface TabSpec { /** Tab name, shown in the tab bar. Cosmetic; identity is the purpose. */ name: string; /** Project path relative to the root, which the purpose encodes. */ relPath: string; /** Absolute path used as the tab's cwd. */ cwd: string; /** Pane nodes, verbatim from `.sl0ppi/layout.kdl`. */ fragment?: string; } function indent(text: string, spaces: number): string { const pad = " ".repeat(spaces); return text .split("\n") .map((line) => (line.trim() === "" ? "" : pad + line)) .join("\n"); } /** A single `tab { ... }` node, indented for embedding in a `layout` block. */ export function tabKdl(spec: TabSpec, indentBy = 4): string { const fragment = (spec.fragment ?? DEFAULT_FRAGMENT).trimEnd(); const head = `tab name=${kdlString(spec.name)} ` + `purpose=${kdlString(projectPurpose(spec.relPath))} ` + `cwd=${kdlString(spec.cwd)} {`; return indent(`${head}\n${indent(fragment, 4)}\n}`, indentBy); } /** A whole-session layout: what `up` passes to `--layout` when creating. */ export function sessionLayoutKdl(specs: TabSpec[]): string { const tabs = specs.map((spec) => tabKdl(spec)).join("\n"); return `layout {\n${tabs}\n}\n`; } /** A one-tab layout: what `up` passes to `new-tab --layout-string`. */ export function singleTabLayoutKdl(spec: TabSpec): string { return `layout {\n${tabKdl(spec)}\n}\n`; }