Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions docs/changes/0038-add-embeddable-grid-components.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Expose the grid as two components a plugin renders inside its own render session: `Grid`, the printed lines as an element laid out for a given width, and `SelectGrid`, the driven grid over the dialogs' embeddable `Select`. `print` and `select` stay as they are.

**Spec:** [Grid](../specs/grid/)
**Status:** draft
**Status:** complete
**Depends On:** 0029, 0030, 0036

## Motivation
Expand Down Expand Up @@ -78,13 +78,13 @@ Skipping or weakening any of these rules to land the PR MUST be treated as a bug
- [x] `test/plugin-consumer.test.ts`: the consumer fixture types `Grid` through the published contract; `test/plugin-boundary.test.ts` keeps passing
- [x] [`docs/manual/plugins.md`](../manual/plugins.md#use-the-bundled-grid-capability): document `Grid`
- [x] [Grid](../specs/grid/) spec: mark [Embeddable Grid](../specs/grid/index.md#embeddable-grid) implemented and add a changelog row
- [ ] Add the embeddable `SelectGrid` component (after [Change 0036](./0036-add-embeddable-dialog-components.md))
- [ ] `plugins/grid/contract.ts`: `SelectGridProps<T, A>` importing `EmbeddedProps` type-only from `@fx/tx/dialogs`, and the `SelectGrid` member
- [ ] `plugins/grid/index.ts` and `plugins/grid/select.ts`: the component over the dialogs' `Select`, mapping the request on mount and the answer on submit
- [ ] `test/grid-embedded.test.ts`: every scenario in [Grid: Embeddable Selection](../specs/grid/index.md#embeddable-selection) — acting on a row, cancelling, and the absent dialogs provider — plus `isActive`, `height`, and a row offering no actions
- [ ] `test/plugin-consumer.test.ts`: the consumer fixture types `SelectGrid`, resolving the grid contract through the dialogs contract in the packed tarball
- [ ] [`docs/manual/plugins.md`](../manual/plugins.md#use-the-bundled-grid-capability): document `SelectGrid`
- [ ] [Grid](../specs/grid/) spec: mark [Embeddable Selection](../specs/grid/index.md#embeddable-selection) implemented and add a changelog row; set this change `complete` in `docs/index.yml` and `docs/index.md`
- [x] Add the embeddable `SelectGrid` component (after [Change 0036](./0036-add-embeddable-dialog-components.md)) (PR #89)
- [x] `plugins/grid/contract.ts`: `SelectGridProps<T, A>` importing `EmbeddedProps` type-only from `@fx/tx/dialogs`, and the `SelectGrid` member
- [x] `plugins/grid/index.ts` and `plugins/grid/select.ts`: the component over the dialogs' `Select`, mapping the request on mount and the answer on submit
- [x] `test/grid-embedded.test.ts`: every scenario in [Grid: Embeddable Selection](../specs/grid/index.md#embeddable-selection) — acting on a row, cancelling, and the absent dialogs provider — plus `isActive`, `height`, and a row offering no actions
- [x] `test/plugin-consumer.test.ts`: the consumer fixture types `SelectGrid`, resolving the grid contract through the dialogs contract in the packed tarball
- [x] [`docs/manual/plugins.md`](../manual/plugins.md#use-the-bundled-grid-capability): document `SelectGrid`
- [x] [Grid](../specs/grid/) spec: mark [Embeddable Selection](../specs/grid/index.md#embeddable-selection) implemented and add a changelog row; set this change `complete` in `docs/index.yml` and `docs/index.md`

## References

Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@
| 0035 | [Production React and Cached Text Widths](changes/0035-production-react-and-cached-widths.md) | [Architecture](specs/architecture/) | complete | 0033 |
| 0036 | [Add Embeddable Dialog Components](changes/0036-add-embeddable-dialog-components.md) | [Dialogs](specs/dialogs/) | complete | 0017, 0023, 0031, 0033 |
| 0037 | [Add Multi-Select](changes/0037-add-multi-select.md) | [Dialogs](specs/dialogs/) | draft | 0027, 0036 |
| 0038 | [Add Embeddable Grid Components](changes/0038-add-embeddable-grid-components.md) | [Grid](specs/grid/) | draft | 0029, 0030, 0036 |
| 0038 | [Add Embeddable Grid Components](changes/0038-add-embeddable-grid-components.md) | [Grid](specs/grid/) | complete | 0029, 0030, 0036 |
2 changes: 1 addition & 1 deletion docs/index.yml
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ changes:
path: changes/0038-add-embeddable-grid-components.md
description: Expose the grid as a Grid component drawing the printed lines for a given width and a SelectGrid component driving rows over the dialogs' embeddable Select, leaving print and select unchanged.
spec: grid
status: draft
status: complete
depends_on:
- "0029"
- "0030"
Expand Down
42 changes: 41 additions & 1 deletion docs/manual/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -646,10 +646,11 @@ import type {
GridSelectRow,
OutputStream,
Row,
SelectGridProps,
} from "@fx/tx/grid";
```

`Grid` is what the key carries: `print(request)` writing a `GridRequest` — a `stream`, an optional `layout` of `"table"` or `"flow"`, optional `headers`, the `rows`, an optional `empty` message, and an optional `summary` — and `select(request)` driving a `GridSelectRequest` of a `message`, optional `headers`, and `GridSelectRow`s, each carrying its `cells`, the `value` identifying it, and the `GridAction`s it offers, resolving to a `GridSelection` of the chosen `value` and `action` or to `undefined`. It also carries `Grid`, the printed grid as a component taking `GridProps`, described below. A `Row` is a list of `Cell`s or bare strings, and an `OutputStream` is anything that can be written to and may report its `columns` and `isTTY`.
`Grid` is what the key carries: `print(request)` writing a `GridRequest` — a `stream`, an optional `layout` of `"table"` or `"flow"`, optional `headers`, the `rows`, an optional `empty` message, and an optional `summary` — and `select(request)` driving a `GridSelectRequest` of a `message`, optional `headers`, and `GridSelectRow`s, each carrying its `cells`, the `value` identifying it, and the `GridAction`s it offers, resolving to a `GridSelection` of the chosen `value` and `action` or to `undefined`. It also carries `Grid`, the printed grid as a component taking `GridProps`, and `SelectGrid`, the driven grid as a component taking `SelectGridProps`, both described below. A `Row` is a list of `Cell`s or bare strings, and an `OutputStream` is anything that can be written to and may report its `columns` and `isTTY`.

A consumer reads `registrations<Grid>("@fx/tx/grid")` inside its command action, after initialization has committed every provider. The contract is types only, and it declares a cell's `variable` as [Theming](#use-the-bundled-theme-capability)'s own `ThemeVariable` by importing `@fx/tx/theme` rather than restating that vocabulary, so a variable renamed there is renamed in every cell that can name one. The grid resolves a theme for the stream it is printing to through the theme capability above, so a `tx` composed without exactly one theme provider fails the call with an error naming the count rather than choosing an appearance of its own. It uses the injected React and Ink instances and never reaches for the process's own streams.

Expand Down Expand Up @@ -766,6 +767,45 @@ const plugin: Plugin = ({ command, context, dependencies, registrations }) => {
};
```

`SelectGrid` is `select` as a component, for the same plugin. It takes `SelectGridProps<T, A>`: the `GridSelectRequest` you would pass to `select` as its `request`, and the `width`, `height`, `isActive`, `onSubmit`, and `onCancel` every embeddable component that takes keys shares — the `EmbeddedProps` of [Embed a dialog in your own interface](#embed-a-dialog-in-your-own-interface), which the grid contract imports from `@fx/tx/dialogs` rather than restating, so they mean exactly what they mean for `dialogs.Select`. It draws its request as that embedded `Select`, given your `width`, `height`, and `isActive`, so every rule above about selecting — the padded rows, the dropped `variable` and `align`, a row's actions as the column it opens, a row with no actions taken on its own — holds for it, and so does every rule that section states for an embedded `Select`: it is one-shot, it reads your callbacks on every call, it presents the request it was mounted with, and it answers keys only while it is active. `onSubmit` receives exactly the `GridSelection` `select` would resolve with, its `action` key absent where the row offered none, and `onCancel` is called where `select` would resolve `undefined`.

It reads the dialogs capability when it mounts, while your command runs, and fails that render — which your session's error handling sees like any other render failure — with an error naming the count where a `tx` is composed without exactly one dialogs provider, rather than falling back to anything. It writes to no stream and needs no interactive one, so the handover guarantee above is `select`'s alone: a process you start after `onSubmit` inherits a terminal your session holds, and handing it over is yours. Every read of the capability yields the same `SelectGrid`. Cast it where you hand it to `createElement`, exactly as `Grid`:

```ts
import type { Grid, GridSelection, SelectGridProps } from "@fx/tx/grid";
import type { ReactNode } from "react";

// Inside your command action, with `grid`, `react`, and `ink` in hand.
const ServicePicker = grid.SelectGrid as (
props: SelectGridProps<Service, "connect" | "logs">,
) => ReactNode;

function Picker({ onPicked }: {
readonly onPicked: (chosen?: GridSelection<Service, "connect" | "logs">) => void;
}) {
const { columns, rows } = ink.useWindowSize();
return react.createElement(ServicePicker, {
request: {
message: "Pick a service",
headers: ["SERVICE", "STATE"],
rows: services.map((service) => ({
cells: [service.name, service.state],
value: service,
actions: [
{ label: "connect", value: "connect" as const },
{ label: "open logs", value: "logs" as const },
],
})),
},
width: columns,
height: rows - 1,
isActive: true,
onSubmit: (chosen) => onPicked(chosen),
onCancel: () => onPicked(undefined),
});
}
```

Sorting, grouping, filtering, and paginating the supplied rows are out of scope, as are machine-readable output, borders and box drawing, spanning and wrapped cells, caller-specified widths, multi-row selection, in-place cell editing, column reordering, and value formatting of every kind — a duration, a timestamp, and a byte count are the consumer's to render into a string before it hands one over.

## One namespace per plugin
Expand Down
5 changes: 3 additions & 2 deletions docs/specs/grid/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ The grid capability lays text out in aligned cells and either prints it once or

A grid is not only a table. A table — a header row over columns of equal meaning — is one layout; a flow, where a list of short items fills as many columns as the terminal affords, is another. Both are the same cells measured the same way, so both belong to one capability.

[Change 0030](../../changes/0030-publish-bundled-capability-contracts.md) publishes this contract at `@fx/tx/grid` and renames the capability's registry key to that same specifier, so a consumer imports the contract rather than restating it, under [Plugin System: Published Capability Contracts](../plugin-system/index.md#published-capability-contracts). That change has landed, so every requirement below describes current behavior except those the next paragraph names.
[Change 0030](../../changes/0030-publish-bundled-capability-contracts.md) publishes this contract at `@fx/tx/grid` and renames the capability's registry key to that same specifier, so a consumer imports the contract rather than restating it, under [Plugin System: Published Capability Contracts](../plugin-system/index.md#published-capability-contracts). That change has landed.

[Change 0038](../../changes/0038-add-embeddable-grid-components.md) specifies [Embeddable Grid](#embeddable-grid) and [Embeddable Selection](#embeddable-selection) — a printed grid and a driven one rendered inside a plugin's own render session — together with the members they add to [Grid Capability](#grid-capability). [Embeddable Grid](#embeddable-grid) and the `Grid` member it adds are implemented. [Embeddable Selection](#embeddable-selection) and the `SelectGrid` member it adds are planned and not yet implemented: today the capability exposes `print`, `select`, and `Grid`.
[Change 0038](../../changes/0038-add-embeddable-grid-components.md) adds [Embeddable Grid](#embeddable-grid) and [Embeddable Selection](#embeddable-selection) — a printed grid and a driven one rendered inside a plugin's own render session — together with the `Grid` and `SelectGrid` members they add to [Grid Capability](#grid-capability). That change has landed too, so every requirement below describes current behavior: the capability exposes `print`, `select`, `Grid`, and `SelectGrid`.

## Background

Expand Down Expand Up @@ -436,3 +436,4 @@ They share every measurement decision and differ only in what drives them. Split
| 2026-09-06 | Required the grid contract to be published at `@fx/tx/grid` as a types-only import, replacing the local structural copy every consumer maintained | [0030-publish-bundled-capability-contracts](../../changes/0030-publish-bundled-capability-contracts.md) |
| 2026-10-18 | Desired embeddable `Grid` and `SelectGrid` components, sized by their host under the props Dialogs defines for every embeddable component | [0038-add-embeddable-grid-components](../../changes/0038-add-embeddable-grid-components.md) |
| 2026-10-18 | Implemented the embeddable `Grid` component: the lines `print` would write, laid out against the host's `width` rather than a stream's, themed for the injected standard error, and handed to the host's session whole, a cell's own trailing spaces included | [0038-add-embeddable-grid-components](../../changes/0038-add-embeddable-grid-components.md) |
| 2026-10-18 | Implemented the embeddable `SelectGrid` component: the request `select` presents, drawn as the dialogs' embeddable `Select` in the host's `width`, `height`, and focus, mapped once on mount and answered through `onSubmit` with exactly the selection `select` resolves with, reading the dialogs capability when it mounts and failing that render naming the count when there is not exactly one provider | [0038-add-embeddable-grid-components](../../changes/0038-add-embeddable-grid-components.md) |
31 changes: 29 additions & 2 deletions plugins/grid/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* restating it learns about a contract that moved when it builds rather than
* when its command reaches for a member that is no longer there.
*
* It declares types alone — one type-only import, no statements, nothing that
* It declares types alone — type-only imports, no statements, nothing that
* survives compilation — which is what lets it be published under a `types`
* condition with no runtime condition beside it. The capability's value comes
* from the registry and must keep coming from there: nothing here prints,
Expand All @@ -20,9 +20,13 @@
* one runtime module graph; the published path shares no graph at all, since
* it is erased. It is the mechanism `@fx/tx/plugin` already uses between the
* same two directories, and it means a variable renamed in one place is
* renamed everywhere a cell can name one.
* renamed everywhere a cell can name one. The props every embeddable component
* taking keys shares arrive through `@fx/tx/dialogs` for the same reason, so a
* host learns them once and the selecting grid cannot drift from the select it
* is drawn as.
*/

import type { EmbeddedProps } from "@fx/tx/dialogs";
import type { ThemeVariable } from "@fx/tx/theme";

/** One cell of a grid. `variable` and `align` describe a *printed* cell; the
Expand Down Expand Up @@ -136,6 +140,21 @@ export type GridProps = {
readonly width: number;
};

/** What a host hands the embeddable `SelectGrid`: the selecting request it
* would pass to `select`, the props every embeddable component taking keys
* shares, and the rows it may draw in.
*
* `onSubmit` receives exactly the selection `select` resolves with, and
* `onCancel` is called where `select` resolves with nothing. The request is the
* one the component was mounted with: a host presenting different rows mounts
* a new component, keyed differently, rather than passing a new request. */
export type SelectGridProps<T, A> = EmbeddedProps<GridSelection<T, A>> & {
readonly request: GridSelectRequest<T, A>;
/** The terminal rows the grid may draw in, as the select it is drawn as
* reads them. */
readonly height: number;
};

/** The value registered under `@fx/tx/grid`: cells printed once, or driven. A
* grid is one or the other in a call, never both. */
export type Grid = {
Expand All @@ -151,4 +170,12 @@ export type Grid = {
* React type: a host casts it where it passes it to `createElement`. The
* same value on every read of the capability. */
readonly Grid: (props: GridProps) => unknown;
/** The driven grid as a component a host renders inside a session it owns:
* the request `select` would present, drawn as the dialogs' embeddable
* `Select` in the host's `width` and `height` and answering keys only while
* `isActive`. It writes to no stream and needs no interactive one. It reads
* the dialogs capability when it mounts, and fails that render when it finds
* other than exactly one provider. Typed as returning `unknown` for the
* reason `Grid` is; the same value on every read of the capability. */
readonly SelectGrid: <T, A>(props: SelectGridProps<T, A>) => unknown;
};
12 changes: 11 additions & 1 deletion plugins/grid/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {
} from "./contract.ts";
import { requireDialogsCapability } from "./dialogs.ts";
import { embeddedGrid, printGrid } from "./render.ts";
import { selection, selectRequest } from "./select.ts";
import { embeddedSelectGrid, selection, selectRequest } from "./select.ts";
import { requireThemeCapability } from "./theme.ts";

/**
Expand Down Expand Up @@ -56,6 +56,15 @@ const definition: PluginDefinition = Object.freeze({
requireThemeCapability(registrations).theme(context.stderr),
);

// Created once for the same reason. The dialogs capability is read when
// it mounts rather than here, for the reasons `select` reads it while
// the command runs: a plugin reading during its own initialization sees
// only what committed before it, and the provider is composed ahead of
// its consumers.
const SelectGrid = embeddedSelectGrid(react, () =>
requireDialogsCapability(registrations),
);

// Frozen, like every value a capability hands out: a consumer holds the
// same object every other consumer in the process holds.
const grid: Grid = Object.freeze({
Expand Down Expand Up @@ -108,6 +117,7 @@ const definition: PluginDefinition = Object.freeze({
},

Grid,
SelectGrid,
});

register<Grid>(gridKey, grid);
Expand Down
Loading
Loading