diff options
| author | user <user@clank> | 2026-08-12 22:24:55 +0200 |
|---|---|---|
| committer | user <user@clank> | 2026-08-12 22:24:55 +0200 |
| commit | 2cd247828d863cdc779c7178c988152835096ca2 (patch) | |
| tree | 867b63f58057e81ac8ab4d242e73b733b8372c9e /cli/src/core/layout.ts | |
| parent | docs(cli): record the fork gap the design turned up (diff) | |
| download | sl0ppi-2cd247828d863cdc779c7178c988152835096ca2.tar.gz sl0ppi-2cd247828d863cdc779c7178c988152835096ca2.tar.xz sl0ppi-2cd247828d863cdc779c7178c988152835096ca2.zip | |
cli: up, ls, open, close
Slice 2 of docs/cli-design.md. The commands read the world (discovery +
list-tabs), compute a plan, then apply it -- never mutating while walking, which
is how `up` stays idempotent.
sl0ppij.ts process wrapper; every piece of state comes back out of
list-tabs/list-panes, so there is nothing to desync
layout.ts composes the tab node around a project's fragment
root.ts root resolution, session naming, the root registry
workspace.ts up / ls / open / close
index.ts dispatch + the fail-open wrapper
Four things this turned up that reading the code would not have:
- `attach --create-background` fails with "There is no active session!" when
the caller is itself inside a session -- which sl0ppi almost always is,
being run from the user's daily-driver terminal. Session creation now runs
with the SL0PPIJ*/ZELLIJ* markers stripped: we are creating a new detached
session, not nesting.
- falling open by passing our own argv to sl0ppij just produces "unrecognized
subcommand: up" and still leaves the user without a terminal. Our verbs and
flags are stripped first, so what is handed over is something sl0ppij
actually understands.
- attaching panics in the client when stdin is not a tty, so `up` from a
script or CI step died. It now reports the session is ready and exits.
- a start_suspended pane reports `is_held`, not `exited`. Conflating them made
`ls` claim every agent was running when none had started. Plugin panes (the
status bar, the link handler) also inflated pane counts and are now excluded
as chrome.
Index conventions were verified against a live session rather than inferred,
because guessing closes the wrong tab: list-tabs `position` and
`close-tab --tab-id` are 0-based, `go-to-tab` is 1-based, and closing renumbers
every tab after it, so batched closes run in descending position order.
Verified end to end against a real session (three projects, one shipping a
custom fragment): up creates the session with all tabs and their purposes; up
again reports "up to date" with tabs unchanged; ls reports panes and
agent:suspended correctly; open creates-and-focuses and resolves unique
prefixes; close removes a tab; deleting a project auto-closes its tab; and with
the root itself missing, nothing is closed at all.
64 tests green.
Diffstat (limited to 'cli/src/core/layout.ts')
| -rw-r--r-- | cli/src/core/layout.ts | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/cli/src/core/layout.ts b/cli/src/core/layout.ts new file mode 100644 index 0000000..a1bf0bb --- /dev/null +++ b/cli/src/core/layout.ts @@ -0,0 +1,68 @@ +/** + * 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`; +} |
