@histoire/cli 0.0.0-stage → 1.0.0-beta.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +8 -2
- package/dist/cli.d.ts +9 -0
- package/dist/cli.js +88 -0
- package/dist/commands/dev.d.ts +44 -0
- package/dist/commands/dev.js +117 -0
- package/dist/commands/host.d.ts +66 -0
- package/dist/commands/host.js +1 -0
- package/dist/commands/screenshot-setup-args.d.ts +4 -0
- package/dist/commands/screenshot-setup-args.js +35 -0
- package/dist/commands/signal-guard.d.ts +29 -0
- package/dist/commands/signal-guard.js +52 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1 -0
- package/dist/terminal/actions.d.ts +51 -0
- package/dist/terminal/actions.js +60 -0
- package/dist/terminal/banner.d.ts +19 -0
- package/dist/terminal/banner.js +48 -0
- package/dist/terminal/bridge.d.ts +58 -0
- package/dist/terminal/bridge.js +103 -0
- package/dist/terminal/clipboard.d.ts +46 -0
- package/dist/terminal/clipboard.js +85 -0
- package/dist/terminal/diagnostics/attach.d.ts +9 -0
- package/dist/terminal/diagnostics/attach.js +46 -0
- package/dist/terminal/diagnostics/client.d.ts +12 -0
- package/dist/terminal/diagnostics/client.js +19 -0
- package/dist/terminal/diagnostics/lines.d.ts +17 -0
- package/dist/terminal/diagnostics/lines.js +61 -0
- package/dist/terminal/dispatch.d.ts +28 -0
- package/dist/terminal/dispatch.js +52 -0
- package/dist/terminal/feedback.d.ts +14 -0
- package/dist/terminal/feedback.js +27 -0
- package/dist/terminal/history/bounded.d.ts +37 -0
- package/dist/terminal/history/bounded.js +54 -0
- package/dist/terminal/host-extras.d.ts +14 -0
- package/dist/terminal/host-extras.js +26 -0
- package/dist/terminal/info/client.d.ts +20 -0
- package/dist/terminal/info/client.js +25 -0
- package/dist/terminal/info/generation-client.d.ts +33 -0
- package/dist/terminal/info/generation-client.js +32 -0
- package/dist/terminal/info/lines.d.ts +19 -0
- package/dist/terminal/info/lines.js +33 -0
- package/dist/terminal/info/network.d.ts +17 -0
- package/dist/terminal/info/network.js +48 -0
- package/dist/terminal/info/qr.d.ts +7 -0
- package/dist/terminal/info/qr.js +14 -0
- package/dist/terminal/keys.d.ts +44 -0
- package/dist/terminal/keys.js +60 -0
- package/dist/terminal/logs/browser-options.d.ts +9 -0
- package/dist/terminal/logs/browser-options.js +19 -0
- package/dist/terminal/navigation/browser.d.ts +69 -0
- package/dist/terminal/navigation/browser.js +193 -0
- package/dist/terminal/navigation/list.d.ts +53 -0
- package/dist/terminal/navigation/list.js +104 -0
- package/dist/terminal/navigation/search.d.ts +22 -0
- package/dist/terminal/navigation/search.js +44 -0
- package/dist/terminal/output/decoder.d.ts +24 -0
- package/dist/terminal/output/decoder.js +63 -0
- package/dist/terminal/output/history.d.ts +43 -0
- package/dist/terminal/output/history.js +21 -0
- package/dist/terminal/output/pipeline.d.ts +33 -0
- package/dist/terminal/output/pipeline.js +59 -0
- package/dist/terminal/output/sanitize.d.ts +32 -0
- package/dist/terminal/output/sanitize.js +142 -0
- package/dist/terminal/panel-model.d.ts +20 -0
- package/dist/terminal/panel-model.js +38 -0
- package/dist/terminal/panel.d.ts +30 -0
- package/dist/terminal/panel.js +53 -0
- package/dist/terminal/plain.d.ts +29 -0
- package/dist/terminal/plain.js +68 -0
- package/dist/terminal/progress/format.d.ts +28 -0
- package/dist/terminal/progress/format.js +77 -0
- package/dist/terminal/progress/state.d.ts +71 -0
- package/dist/terminal/progress/state.js +150 -0
- package/dist/terminal/protocol/catalog.d.ts +143 -0
- package/dist/terminal/protocol/catalog.js +61 -0
- package/dist/terminal/protocol/core.d.ts +117 -0
- package/dist/terminal/protocol/core.js +98 -0
- package/dist/terminal/protocol/diagnostics.d.ts +79 -0
- package/dist/terminal/protocol/diagnostics.js +31 -0
- package/dist/terminal/protocol/index.d.ts +22 -0
- package/dist/terminal/protocol/index.js +1 -0
- package/dist/terminal/protocol/progress.d.ts +67 -0
- package/dist/terminal/protocol/progress.js +52 -0
- package/dist/terminal/protocol/requests.d.ts +30 -0
- package/dist/terminal/protocol/requests.js +13 -0
- package/dist/terminal/protocol/session.d.ts +55 -0
- package/dist/terminal/protocol/session.js +28 -0
- package/dist/terminal/protocol/tests.d.ts +212 -0
- package/dist/terminal/protocol/tests.js +87 -0
- package/dist/terminal/protocol/watch.d.ts +60 -0
- package/dist/terminal/protocol/watch.js +40 -0
- package/dist/terminal/requests/browser-options.d.ts +8 -0
- package/dist/terminal/requests/browser-options.js +24 -0
- package/dist/terminal/requests/history.d.ts +30 -0
- package/dist/terminal/requests/history.js +53 -0
- package/dist/terminal/retained.d.ts +27 -0
- package/dist/terminal/retained.js +21 -0
- package/dist/terminal/session-types.d.ts +76 -0
- package/dist/terminal/session-types.js +1 -0
- package/dist/terminal/session.d.ts +8 -0
- package/dist/terminal/session.js +282 -0
- package/dist/terminal/shutdown.d.ts +12 -0
- package/dist/terminal/shutdown.js +30 -0
- package/dist/terminal/stories/client.d.ts +26 -0
- package/dist/terminal/stories/client.js +28 -0
- package/dist/terminal/stories/handoff.d.ts +39 -0
- package/dist/terminal/stories/handoff.js +40 -0
- package/dist/terminal/stories/paged.d.ts +70 -0
- package/dist/terminal/stories/paged.js +137 -0
- package/dist/terminal/stories/render.d.ts +39 -0
- package/dist/terminal/stories/render.js +94 -0
- package/dist/terminal/stories/state.d.ts +93 -0
- package/dist/terminal/stories/state.js +191 -0
- package/dist/terminal/stories/target.d.ts +24 -0
- package/dist/terminal/stories/target.js +29 -0
- package/dist/terminal/style.d.ts +45 -0
- package/dist/terminal/style.js +82 -0
- package/dist/terminal/support.d.ts +53 -0
- package/dist/terminal/support.js +66 -0
- package/dist/terminal/surface.d.ts +41 -0
- package/dist/terminal/surface.js +200 -0
- package/dist/terminal/tests/client.d.ts +21 -0
- package/dist/terminal/tests/client.js +12 -0
- package/dist/terminal/tests/console.d.ts +95 -0
- package/dist/terminal/tests/console.js +202 -0
- package/dist/terminal/tests/format.d.ts +18 -0
- package/dist/terminal/tests/format.js +55 -0
- package/dist/terminal/tests/header.d.ts +16 -0
- package/dist/terminal/tests/header.js +90 -0
- package/dist/terminal/tests/reporter.d.ts +17 -0
- package/dist/terminal/tests/reporter.js +99 -0
- package/dist/terminal/tests/results.d.ts +44 -0
- package/dist/terminal/tests/results.js +104 -0
- package/dist/terminal/tests/scope.d.ts +25 -0
- package/dist/terminal/tests/scope.js +25 -0
- package/dist/terminal/tests/session.d.ts +11 -0
- package/dist/terminal/tests/session.js +24 -0
- package/dist/terminal/tests/state.d.ts +57 -0
- package/dist/terminal/tests/state.js +134 -0
- package/dist/terminal/views/clear-history.d.ts +6 -0
- package/dist/terminal/views/clear-history.js +10 -0
- package/dist/terminal/views/error.d.ts +8 -0
- package/dist/terminal/views/error.js +24 -0
- package/dist/terminal/views/frame.d.ts +20 -0
- package/dist/terminal/views/frame.js +23 -0
- package/dist/terminal/views/help.d.ts +3 -0
- package/dist/terminal/views/help.js +32 -0
- package/dist/terminal/views/index.d.ts +6 -0
- package/dist/terminal/views/index.js +13 -0
- package/dist/terminal/views/logs.d.ts +3 -0
- package/dist/terminal/views/logs.js +7 -0
- package/dist/terminal/views/registry.d.ts +114 -0
- package/dist/terminal/views/registry.js +40 -0
- package/dist/terminal/views/requests.d.ts +8 -0
- package/dist/terminal/views/requests.js +15 -0
- package/dist/terminal/views/session.d.ts +9 -0
- package/dist/terminal/views/session.js +133 -0
- package/dist/terminal/views/stories.d.ts +9 -0
- package/dist/terminal/views/stories.js +172 -0
- package/dist/terminal/views/tests.d.ts +9 -0
- package/dist/terminal/views/tests.js +105 -0
- package/dist/terminal/watch/client.d.ts +12 -0
- package/dist/terminal/watch/client.js +7 -0
- package/dist/terminal/watch/controller.d.ts +30 -0
- package/dist/terminal/watch/controller.js +157 -0
- package/dist/terminal/watch/format.d.ts +15 -0
- package/dist/terminal/watch/format.js +37 -0
- package/dist/terminal/watch/session.d.ts +17 -0
- package/dist/terminal/watch/session.js +12 -0
- package/dist/terminal/width.d.ts +24 -0
- package/dist/terminal/width.js +171 -0
- package/dist/workbench-url.d.ts +30 -0
- package/dist/workbench-url.js +55 -0
- package/package.json +54 -3
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { CATALOG_STALE_MESSAGE } from '../protocol/catalog.js';
|
|
2
|
+
import { TerminalError } from '../protocol/core.js';
|
|
3
|
+
/**
|
|
4
|
+
* Creates the typed client. Pure sugar over `bridge.send`; holds no state.
|
|
5
|
+
* @param bridge Parent bridge (or any object with a compatible `send`).
|
|
6
|
+
*/
|
|
7
|
+
export function createCatalogClient(bridge) {
|
|
8
|
+
return {
|
|
9
|
+
list: args => bridge.send('catalog.list', args),
|
|
10
|
+
variants: args => bridge.send('catalog.variants', args),
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Whether a failed read says its expected revision is no longer current. The read must then
|
|
15
|
+
* restart from the first page; pages of two revisions are never combined.
|
|
16
|
+
* @param error Rejection of a catalog read.
|
|
17
|
+
*/
|
|
18
|
+
export function isStaleCatalogRead(error) {
|
|
19
|
+
return error instanceof TerminalError && error.code === 'failed' && error.message === CATALOG_STALE_MESSAGE;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Whether a failed read belongs to a generation that no longer exists. Nothing is shown for
|
|
23
|
+
* it: the progress mirror already moved on and the successor generation is read afresh.
|
|
24
|
+
* @param error Rejection of a catalog read.
|
|
25
|
+
*/
|
|
26
|
+
export function isRetiredCatalogRead(error) {
|
|
27
|
+
return error instanceof TerminalError && (error.code === 'retired' || error.code === 'closed');
|
|
28
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { TestTarget } from '../protocol/tests.js';
|
|
2
|
+
import type { TerminalViewHost } from '../views/registry.js';
|
|
3
|
+
/**
|
|
4
|
+
* Exact test target the stories screen hands to the tests screen (`t`), which then runs it.
|
|
5
|
+
* Handing over never retargets watching, and the child re-validates the IDs against
|
|
6
|
+
* `revision` when a run is admitted.
|
|
7
|
+
*/
|
|
8
|
+
export interface TestTargetSelection {
|
|
9
|
+
/** Exact story or variant scope (never `all`). */
|
|
10
|
+
target: Extract<TestTarget, {
|
|
11
|
+
scope: 'story' | 'variant';
|
|
12
|
+
}>;
|
|
13
|
+
/** Display label of the target (sanitize before rendering). */
|
|
14
|
+
label: string;
|
|
15
|
+
/** Display label of the owning story when `target` is a variant (the story scope's label). */
|
|
16
|
+
storyLabel?: string;
|
|
17
|
+
/** Catalog revision the target was selected in; pass as `expectedRevision` to `tests.run`. */
|
|
18
|
+
revision: string;
|
|
19
|
+
/** Runtime generation the target was selected in; a different current generation makes it unresolved. */
|
|
20
|
+
generation: string;
|
|
21
|
+
}
|
|
22
|
+
/** Session-lifetime holder of the latest handed-over target. */
|
|
23
|
+
export interface TestTargetHandoff {
|
|
24
|
+
/** Replaces the handed-over target and notifies subscribers. */
|
|
25
|
+
set: (selection: TestTargetSelection) => void;
|
|
26
|
+
/** Latest handed-over target, until cleared or replaced. */
|
|
27
|
+
current: () => TestTargetSelection | undefined;
|
|
28
|
+
/** Forgets the target. */
|
|
29
|
+
clear: () => void;
|
|
30
|
+
/** Observes changes. Returns an unsubscribe function. */
|
|
31
|
+
subscribe: (listener: () => void) => () => void;
|
|
32
|
+
}
|
|
33
|
+
/** Creates an empty handoff. */
|
|
34
|
+
export declare function createTestTargetHandoff(): TestTargetHandoff;
|
|
35
|
+
/**
|
|
36
|
+
* The session's handoff. The stories screen writes it; the tests screen (slice 08) reads it.
|
|
37
|
+
* @param host View host; only its session-lifetime bridge identifies the session.
|
|
38
|
+
*/
|
|
39
|
+
export declare function getTestTargetHandoff(host: Pick<TerminalViewHost, 'bridge'>): TestTargetHandoff;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/** Creates an empty handoff. */
|
|
2
|
+
export function createTestTargetHandoff() {
|
|
3
|
+
let selection;
|
|
4
|
+
const listeners = new Set();
|
|
5
|
+
const notify = () => {
|
|
6
|
+
for (const listener of [...listeners]) {
|
|
7
|
+
try {
|
|
8
|
+
listener();
|
|
9
|
+
}
|
|
10
|
+
catch { }
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
return {
|
|
14
|
+
set(next) {
|
|
15
|
+
selection = next;
|
|
16
|
+
notify();
|
|
17
|
+
},
|
|
18
|
+
current: () => selection,
|
|
19
|
+
clear() {
|
|
20
|
+
selection = undefined;
|
|
21
|
+
notify();
|
|
22
|
+
},
|
|
23
|
+
subscribe(listener) {
|
|
24
|
+
listeners.add(listener);
|
|
25
|
+
return () => void listeners.delete(listener);
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
/** One handoff per session, shared by views without importing each other. Keyed by the session's bridge. */
|
|
30
|
+
const handoffs = new WeakMap();
|
|
31
|
+
/**
|
|
32
|
+
* The session's handoff. The stories screen writes it; the tests screen (slice 08) reads it.
|
|
33
|
+
* @param host View host; only its session-lifetime bridge identifies the session.
|
|
34
|
+
*/
|
|
35
|
+
export function getTestTargetHandoff(host) {
|
|
36
|
+
let handoff = handoffs.get(host.bridge);
|
|
37
|
+
if (!handoff)
|
|
38
|
+
handoffs.set(host.bridge, handoff = createTestTargetHandoff());
|
|
39
|
+
return handoff;
|
|
40
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/** One page as the reader needs it, independent of the operation behind it. */
|
|
2
|
+
export interface PagedPage<T> {
|
|
3
|
+
/** Rows of the page. */
|
|
4
|
+
items: T[];
|
|
5
|
+
/** Revision every row belongs to. */
|
|
6
|
+
revision: string;
|
|
7
|
+
/** Rows matching across all pages. */
|
|
8
|
+
total: number;
|
|
9
|
+
/** Cursor of the next page. */
|
|
10
|
+
next?: string;
|
|
11
|
+
}
|
|
12
|
+
/** What the reader should do with a failed read. */
|
|
13
|
+
export type PagedFailure = 'stale' | 'ignore' | {
|
|
14
|
+
problem: string;
|
|
15
|
+
};
|
|
16
|
+
/** Observable state of a paged reader. */
|
|
17
|
+
export interface PagedSnapshot<T> {
|
|
18
|
+
/** `idle` before the first read, `loading` until the first page of a read landed. */
|
|
19
|
+
status: 'idle' | 'loading' | 'ready' | 'failed';
|
|
20
|
+
/** Why the latest read failed; rows already loaded stay visible. */
|
|
21
|
+
problem?: string;
|
|
22
|
+
/** Revision of the rows shown. */
|
|
23
|
+
revision?: string;
|
|
24
|
+
/** Rows loaded so far, all of one revision. */
|
|
25
|
+
items: readonly T[];
|
|
26
|
+
/** Rows matching across all pages. */
|
|
27
|
+
total: number;
|
|
28
|
+
/** More pages exist. */
|
|
29
|
+
hasMore: boolean;
|
|
30
|
+
}
|
|
31
|
+
/** Inputs of a paged reader. */
|
|
32
|
+
export interface PagedReaderOptions<T, P> {
|
|
33
|
+
/** Reads one page; continuation pages carry the cursor and the revision the read started at. */
|
|
34
|
+
read: (request: {
|
|
35
|
+
cursor?: string;
|
|
36
|
+
expectedRevision?: string;
|
|
37
|
+
}) => Promise<P>;
|
|
38
|
+
/** Extracts the reader's view of a page. */
|
|
39
|
+
select: (page: P) => PagedPage<T>;
|
|
40
|
+
/** Classifies a failed read. */
|
|
41
|
+
classify: (error: unknown) => PagedFailure;
|
|
42
|
+
/** Stable ID of a row, used to keep loading until the wanted row is present. */
|
|
43
|
+
idOf: (item: T) => string;
|
|
44
|
+
/** ID the owner wants visible (the selection); pages load until it appears or none remain. */
|
|
45
|
+
wanted?: () => string | undefined;
|
|
46
|
+
/** Called for every accepted page (first or continuation). */
|
|
47
|
+
accepted?: (page: P) => void;
|
|
48
|
+
/** Called after any change of the snapshot. */
|
|
49
|
+
changed: () => void;
|
|
50
|
+
/** Most rows kept; further pages load only when the wanted row is still missing. */
|
|
51
|
+
maxItems?: number;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Reads a revision-pinned list page by page. The first page decides the revision;
|
|
55
|
+
* continuation pages must match it, and a stale answer restarts the read from the first
|
|
56
|
+
* page instead of appending. Rows of the previous read stay visible until the new first
|
|
57
|
+
* page replaces them wholesale, so the list never mixes revisions.
|
|
58
|
+
* @param options Page source and callbacks.
|
|
59
|
+
*/
|
|
60
|
+
export declare function createPagedReader<T, P>(options: PagedReaderOptions<T, P>): {
|
|
61
|
+
snapshot(): PagedSnapshot<T>;
|
|
62
|
+
/** Drops everything: the owner's generation changed, so no row is actionable any more. */
|
|
63
|
+
reset(): void;
|
|
64
|
+
/** Starts a read from the first page; concurrent calls coalesce into one follow-up read. */
|
|
65
|
+
restart(): Promise<void>;
|
|
66
|
+
/** Loads the next page of the current read. */
|
|
67
|
+
more(force?: boolean): Promise<undefined>;
|
|
68
|
+
};
|
|
69
|
+
/** Reader created by {@link createPagedReader}. */
|
|
70
|
+
export type PagedReader<T> = ReturnType<typeof createPagedReader<T, unknown>>;
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/** Pages a read can load while chasing a wanted row. */
|
|
2
|
+
const MAX_PURSUIT_PAGES = 200;
|
|
3
|
+
/**
|
|
4
|
+
* Reads a revision-pinned list page by page. The first page decides the revision;
|
|
5
|
+
* continuation pages must match it, and a stale answer restarts the read from the first
|
|
6
|
+
* page instead of appending. Rows of the previous read stay visible until the new first
|
|
7
|
+
* page replaces them wholesale, so the list never mixes revisions.
|
|
8
|
+
* @param options Page source and callbacks.
|
|
9
|
+
*/
|
|
10
|
+
export function createPagedReader(options) {
|
|
11
|
+
let items = [];
|
|
12
|
+
let revision;
|
|
13
|
+
let next;
|
|
14
|
+
let total = 0;
|
|
15
|
+
let status = 'idle';
|
|
16
|
+
let problem;
|
|
17
|
+
/** Increments on every restart or reset; answers of an older read are dropped. */
|
|
18
|
+
let epoch = 0;
|
|
19
|
+
/** Reads still awaiting an answer, including dropped ones: a successor waits for them instead of racing. */
|
|
20
|
+
let firstInFlight = 0;
|
|
21
|
+
let moreInFlight = 0;
|
|
22
|
+
let again = false;
|
|
23
|
+
/** Pages loaded while chasing the wanted row in the current read. */
|
|
24
|
+
let pursued = 0;
|
|
25
|
+
/**
|
|
26
|
+
* Records a failure unless it should be ignored; stale answers restart the read. A failed first
|
|
27
|
+
* page leaves nothing the catalog confirms (the story may be gone), so earlier rows are dropped;
|
|
28
|
+
* a failed continuation keeps the rows already loaded for the same revision.
|
|
29
|
+
*/
|
|
30
|
+
function fail(error, startedAt, first) {
|
|
31
|
+
if (startedAt !== epoch)
|
|
32
|
+
return;
|
|
33
|
+
const outcome = options.classify(error);
|
|
34
|
+
if (outcome === 'ignore')
|
|
35
|
+
return;
|
|
36
|
+
if (outcome === 'stale')
|
|
37
|
+
return void self.restart();
|
|
38
|
+
problem = outcome.problem;
|
|
39
|
+
if (first) {
|
|
40
|
+
items = [];
|
|
41
|
+
revision = next = undefined;
|
|
42
|
+
total = 0;
|
|
43
|
+
status = 'failed';
|
|
44
|
+
}
|
|
45
|
+
options.changed();
|
|
46
|
+
}
|
|
47
|
+
/** Loads further pages while the wanted row is not present yet. */
|
|
48
|
+
function pursue() {
|
|
49
|
+
const wanted = options.wanted?.();
|
|
50
|
+
if (wanted !== undefined && next && !items.some(item => options.idOf(item) === wanted) && ++pursued < MAX_PURSUIT_PAGES)
|
|
51
|
+
void self.more(true);
|
|
52
|
+
}
|
|
53
|
+
const self = {
|
|
54
|
+
snapshot() {
|
|
55
|
+
return { status, problem, revision, items, total, hasMore: next !== undefined };
|
|
56
|
+
},
|
|
57
|
+
/** Drops everything: the owner's generation changed, so no row is actionable any more. */
|
|
58
|
+
reset() {
|
|
59
|
+
epoch++;
|
|
60
|
+
items = [];
|
|
61
|
+
revision = next = problem = undefined;
|
|
62
|
+
total = 0;
|
|
63
|
+
status = 'idle';
|
|
64
|
+
again = false;
|
|
65
|
+
options.changed();
|
|
66
|
+
},
|
|
67
|
+
/** Starts a read from the first page; concurrent calls coalesce into one follow-up read. */
|
|
68
|
+
async restart() {
|
|
69
|
+
if (firstInFlight > 0) {
|
|
70
|
+
again = true;
|
|
71
|
+
epoch++;
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
const startedAt = ++epoch;
|
|
75
|
+
firstInFlight++;
|
|
76
|
+
again = false;
|
|
77
|
+
if (status !== 'ready')
|
|
78
|
+
status = 'loading';
|
|
79
|
+
problem = undefined;
|
|
80
|
+
options.changed();
|
|
81
|
+
try {
|
|
82
|
+
const page = await options.read({});
|
|
83
|
+
firstInFlight--;
|
|
84
|
+
if (startedAt === epoch) {
|
|
85
|
+
const view = options.select(page);
|
|
86
|
+
items = view.items;
|
|
87
|
+
revision = view.revision;
|
|
88
|
+
next = view.next;
|
|
89
|
+
total = view.total;
|
|
90
|
+
status = 'ready';
|
|
91
|
+
pursued = 0;
|
|
92
|
+
options.accepted?.(page);
|
|
93
|
+
options.changed();
|
|
94
|
+
pursue();
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
catch (error) {
|
|
98
|
+
firstInFlight--;
|
|
99
|
+
fail(error, startedAt, true);
|
|
100
|
+
}
|
|
101
|
+
if (again) {
|
|
102
|
+
again = false;
|
|
103
|
+
void self.restart();
|
|
104
|
+
}
|
|
105
|
+
},
|
|
106
|
+
/** Loads the next page of the current read. */
|
|
107
|
+
async more(force = false) {
|
|
108
|
+
if (!next || moreInFlight > 0 || firstInFlight > 0 || status !== 'ready')
|
|
109
|
+
return;
|
|
110
|
+
if (!force && items.length >= (options.maxItems ?? Number.POSITIVE_INFINITY))
|
|
111
|
+
return;
|
|
112
|
+
const startedAt = epoch;
|
|
113
|
+
moreInFlight++;
|
|
114
|
+
try {
|
|
115
|
+
const page = await options.read({ cursor: next, expectedRevision: revision });
|
|
116
|
+
moreInFlight--;
|
|
117
|
+
if (startedAt !== epoch)
|
|
118
|
+
return;
|
|
119
|
+
const view = options.select(page);
|
|
120
|
+
// A continuation of another revision would mix two catalogs: restart instead.
|
|
121
|
+
if (view.revision !== revision)
|
|
122
|
+
return void self.restart();
|
|
123
|
+
items = [...items, ...view.items];
|
|
124
|
+
next = view.next;
|
|
125
|
+
total = view.total;
|
|
126
|
+
options.accepted?.(page);
|
|
127
|
+
options.changed();
|
|
128
|
+
pursue();
|
|
129
|
+
}
|
|
130
|
+
catch (error) {
|
|
131
|
+
moreInFlight--;
|
|
132
|
+
fail(error, startedAt, false);
|
|
133
|
+
}
|
|
134
|
+
},
|
|
135
|
+
};
|
|
136
|
+
return self;
|
|
137
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { CatalogStateDto, CatalogStoryDto, CatalogVariantDto } from '../protocol/catalog.js';
|
|
2
|
+
import type { StoriesView, VariantsView } from './state.js';
|
|
3
|
+
/**
|
|
4
|
+
* Panel segment: whole-catalog counts, hidden until a catalog of the current generation answered.
|
|
5
|
+
* @param catalog Latest catalog state.
|
|
6
|
+
*/
|
|
7
|
+
export declare function catalogSegment(catalog: CatalogStateDto | undefined): string | undefined;
|
|
8
|
+
/**
|
|
9
|
+
* One story row: title, file label and flags.
|
|
10
|
+
* @param story Story DTO.
|
|
11
|
+
* @param selected Whether the cursor is on it.
|
|
12
|
+
* @param columns Screen width.
|
|
13
|
+
*/
|
|
14
|
+
export declare function storyRow(story: CatalogStoryDto, selected: boolean, columns: number): string;
|
|
15
|
+
/**
|
|
16
|
+
* One variant row.
|
|
17
|
+
* @param variant Variant DTO.
|
|
18
|
+
* @param selected Whether the cursor is on it.
|
|
19
|
+
* @param columns Screen width.
|
|
20
|
+
*/
|
|
21
|
+
export declare function variantRow(variant: CatalogVariantDto, selected: boolean, columns: number): string;
|
|
22
|
+
/**
|
|
23
|
+
* Status line above the stories list.
|
|
24
|
+
* @param view Stories view.
|
|
25
|
+
*/
|
|
26
|
+
export declare function storiesStatus(view: StoriesView): string;
|
|
27
|
+
/**
|
|
28
|
+
* Status line above the variants list.
|
|
29
|
+
* @param story Opened story as listed, when it still exists.
|
|
30
|
+
* @param variants Variants view.
|
|
31
|
+
* @param catalogUpdating The catalog is being collected again.
|
|
32
|
+
*/
|
|
33
|
+
export declare function variantsStatus(story: CatalogStoryDto | undefined, variants: VariantsView, catalogUpdating: boolean): string;
|
|
34
|
+
/**
|
|
35
|
+
* Text shown instead of rows.
|
|
36
|
+
* @param view Stories view.
|
|
37
|
+
* @param hasSelection A selection by ID exists that the catalog does not confirm.
|
|
38
|
+
*/
|
|
39
|
+
export declare function emptyStoriesText(view: StoriesView, hasSelection: boolean): string;
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { sanitizeText } from '../output/sanitize.js';
|
|
2
|
+
import { fitWidth } from '../width.js';
|
|
3
|
+
/** `1 story`, `2 stories`. */
|
|
4
|
+
function count(value, singular, plural) {
|
|
5
|
+
return `${value} ${value === 1 ? singular : plural}`;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Panel segment: whole-catalog counts, hidden until a catalog of the current generation answered.
|
|
9
|
+
* @param catalog Latest catalog state.
|
|
10
|
+
*/
|
|
11
|
+
export function catalogSegment(catalog) {
|
|
12
|
+
if (!catalog?.revision)
|
|
13
|
+
return undefined;
|
|
14
|
+
return `${count(catalog.stories, 'story', 'stories')}, ${count(catalog.variants, 'variant', 'variants')}${catalog.updating ? ', updating' : ''}`;
|
|
15
|
+
}
|
|
16
|
+
/** Display text safe for a terminal row (authored titles may contain control sequences). */
|
|
17
|
+
function show(text) {
|
|
18
|
+
return sanitizeText(text).replace(/\s+/g, ' ');
|
|
19
|
+
}
|
|
20
|
+
/** Marks the selected row with inverse video. */
|
|
21
|
+
function mark(line, selected) {
|
|
22
|
+
return selected ? `\u001B[7m${line}\u001B[27m` : line;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* One story row: title, file label and flags.
|
|
26
|
+
* @param story Story DTO.
|
|
27
|
+
* @param selected Whether the cursor is on it.
|
|
28
|
+
* @param columns Screen width.
|
|
29
|
+
*/
|
|
30
|
+
export function storyRow(story, selected, columns) {
|
|
31
|
+
const flags = [
|
|
32
|
+
story.ambiguous ? 'ID shared' : story.docsOnly ? 'docs' : count(story.variantCount, 'variant', 'variants'),
|
|
33
|
+
...(story.matchedVariants ? [`${count(story.matchedVariants, 'variant', 'variants')} match`] : []),
|
|
34
|
+
];
|
|
35
|
+
return mark(fitWidth(`${selected ? '›' : ' '} ${show(story.title)} \u001B[2m${show(story.filePath)} · ${flags.join(' · ')}\u001B[22m`, columns), selected);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* One variant row.
|
|
39
|
+
* @param variant Variant DTO.
|
|
40
|
+
* @param selected Whether the cursor is on it.
|
|
41
|
+
* @param columns Screen width.
|
|
42
|
+
*/
|
|
43
|
+
export function variantRow(variant, selected, columns) {
|
|
44
|
+
const sameTitle = variant.title === variant.id;
|
|
45
|
+
const flags = [...(variant.ambiguous ? ['ID shared'] : []), ...(variant.matched ? ['match'] : [])];
|
|
46
|
+
const detail = [...(sameTitle ? [] : [show(variant.id)]), ...flags];
|
|
47
|
+
return mark(fitWidth(`${selected ? '›' : ' '} ${show(variant.title)}${detail.length ? ` \u001B[2m${detail.join(' · ')}\u001B[22m` : ''}`, columns), selected);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Status line above the stories list.
|
|
51
|
+
* @param view Stories view.
|
|
52
|
+
*/
|
|
53
|
+
export function storiesStatus(view) {
|
|
54
|
+
const parts = [`${view.stories.length} of ${count(view.total, 'story', 'stories')}`];
|
|
55
|
+
if (view.query)
|
|
56
|
+
parts.push(`filter "${show(view.query)}"`);
|
|
57
|
+
else if (view.catalog && view.catalog.stories !== view.total)
|
|
58
|
+
parts.push(`${view.catalog.stories} in catalog`);
|
|
59
|
+
if (view.hasMore)
|
|
60
|
+
parts.push('more load as you scroll');
|
|
61
|
+
if (view.catalog?.updating)
|
|
62
|
+
parts.push('updating');
|
|
63
|
+
if (view.catalog?.diagnostics)
|
|
64
|
+
parts.push(count(view.catalog.diagnostics, 'issue', 'issues'));
|
|
65
|
+
return `\u001B[2m${parts.join(' · ')}\u001B[22m`;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Status line above the variants list.
|
|
69
|
+
* @param story Opened story as listed, when it still exists.
|
|
70
|
+
* @param variants Variants view.
|
|
71
|
+
* @param catalogUpdating The catalog is being collected again.
|
|
72
|
+
*/
|
|
73
|
+
export function variantsStatus(story, variants, catalogUpdating) {
|
|
74
|
+
const parts = [story ? show(story.title) : 'Story is not in the catalog', `${variants.variants.length} of ${count(variants.total, 'variant', 'variants')}`];
|
|
75
|
+
if (variants.hasMore)
|
|
76
|
+
parts.push('more load as you scroll');
|
|
77
|
+
if (catalogUpdating)
|
|
78
|
+
parts.push('updating');
|
|
79
|
+
return `\u001B[2m${parts.join(' · ')}\u001B[22m`;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Text shown instead of rows.
|
|
83
|
+
* @param view Stories view.
|
|
84
|
+
* @param hasSelection A selection by ID exists that the catalog does not confirm.
|
|
85
|
+
*/
|
|
86
|
+
export function emptyStoriesText(view, hasSelection) {
|
|
87
|
+
if (view.status === 'waiting')
|
|
88
|
+
return hasSelection ? 'Waiting for the runtime; the selection is kept until the catalog confirms it' : 'Waiting for the runtime';
|
|
89
|
+
if (view.status === 'idle' || view.status === 'loading')
|
|
90
|
+
return 'Loading stories';
|
|
91
|
+
if (view.status === 'failed')
|
|
92
|
+
return `Catalog unavailable: ${view.problem ?? 'unknown reason'}`;
|
|
93
|
+
return view.query ? `No stories match "${show(view.query)}"` : 'No stories';
|
|
94
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import type { TerminalBridge } from '../bridge.js';
|
|
2
|
+
import type { ProgressState } from '../progress/state.js';
|
|
3
|
+
import type { CatalogStateDto, CatalogStoryDto, CatalogVariantDto, CatalogWorkbenchDto } from '../protocol/catalog.js';
|
|
4
|
+
import type { CatalogClient } from './client.js';
|
|
5
|
+
import type { PagedSnapshot } from './paged.js';
|
|
6
|
+
/** Inputs of the catalog state. */
|
|
7
|
+
export interface StoriesStateOptions {
|
|
8
|
+
/** Command bridge; its events carry the catalog revisions. */
|
|
9
|
+
bridge: Pick<TerminalBridge, 'send' | 'subscribe'>;
|
|
10
|
+
/** Generation and readiness mirror. */
|
|
11
|
+
progress: Pick<ProgressState, 'snapshot' | 'subscribe'>;
|
|
12
|
+
/** Repaint hook, called after any change. */
|
|
13
|
+
changed?: () => void;
|
|
14
|
+
/** Replaces the client (tests only). */
|
|
15
|
+
client?: CatalogClient;
|
|
16
|
+
}
|
|
17
|
+
/** Everything the stories screen and the panel know about the catalog of the current generation. */
|
|
18
|
+
export interface StoriesView {
|
|
19
|
+
/** `waiting` while no runtime of the current generation is ready. */
|
|
20
|
+
status: 'waiting' | PagedSnapshot<CatalogStoryDto>['status'];
|
|
21
|
+
/** Why the latest read failed. */
|
|
22
|
+
problem?: string;
|
|
23
|
+
/** Applied search text. */
|
|
24
|
+
query: string;
|
|
25
|
+
/** Revision of the stories shown. */
|
|
26
|
+
revision?: string;
|
|
27
|
+
/** Lifecycle and counts of the whole catalog. */
|
|
28
|
+
catalog?: CatalogStateDto;
|
|
29
|
+
/** Routing facts for building workbench URLs. */
|
|
30
|
+
workbench?: CatalogWorkbenchDto;
|
|
31
|
+
/** Generation the rows belong to. */
|
|
32
|
+
generation?: string;
|
|
33
|
+
/** Loaded stories, all of one revision. */
|
|
34
|
+
stories: readonly CatalogStoryDto[];
|
|
35
|
+
/** Stories matching the query across the whole catalog. */
|
|
36
|
+
total: number;
|
|
37
|
+
/** More pages exist. */
|
|
38
|
+
hasMore: boolean;
|
|
39
|
+
}
|
|
40
|
+
/** Variants of the opened story. */
|
|
41
|
+
export interface VariantsView {
|
|
42
|
+
/** Story whose variants are read; undefined while the stories list is shown. */
|
|
43
|
+
storyId?: string;
|
|
44
|
+
/** `idle` when no story is open. */
|
|
45
|
+
status: PagedSnapshot<CatalogVariantDto>['status'];
|
|
46
|
+
/** Why the latest read failed (e.g. the story vanished). */
|
|
47
|
+
problem?: string;
|
|
48
|
+
/** Revision of the variants shown. */
|
|
49
|
+
revision?: string;
|
|
50
|
+
/** Loaded variants, all of one revision. */
|
|
51
|
+
variants: readonly CatalogVariantDto[];
|
|
52
|
+
/** Variants of the story. */
|
|
53
|
+
total: number;
|
|
54
|
+
/** More pages exist. */
|
|
55
|
+
hasMore: boolean;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Session-lifetime, revision-safe cache of the catalog browser. Pages are read on demand
|
|
59
|
+
* from the child's canonical catalog; a catalog revision event or a stale answer restarts
|
|
60
|
+
* the read, so rows of two revisions are never combined. Everything is dropped as soon as
|
|
61
|
+
* the runtime generation changes: nothing is actionable until the successor's catalog
|
|
62
|
+
* answers, while the selection itself (kept by ID in the view) survives unresolved.
|
|
63
|
+
* @param options Bridge, progress mirror and repaint hook.
|
|
64
|
+
*/
|
|
65
|
+
export declare function createStoriesState(options: StoriesStateOptions): {
|
|
66
|
+
/** Current catalog view for the stories list. */
|
|
67
|
+
view(): StoriesView;
|
|
68
|
+
/** Current view of the opened story's variants. */
|
|
69
|
+
variants(): VariantsView;
|
|
70
|
+
/**
|
|
71
|
+
* Registers the selection providers; reads keep loading pages until the selected row is present.
|
|
72
|
+
* @param story Selected story ID.
|
|
73
|
+
* @param variant Selected variant ID.
|
|
74
|
+
* @param select Selects a story ID; called with the first match when a search's results land.
|
|
75
|
+
*/
|
|
76
|
+
keepVisible(story: () => string | undefined, variant: () => string | undefined, select?: (id: string | undefined) => void): void;
|
|
77
|
+
/** Applies a search; the child filters its complete catalog, so matches beyond loaded pages are found. */
|
|
78
|
+
search(text: string): void;
|
|
79
|
+
/** Opens (or, with `undefined`, closes) the variants of a story. */
|
|
80
|
+
openStory(storyId: string | undefined): void;
|
|
81
|
+
/** Loads the next page of stories. */
|
|
82
|
+
moreStories: () => Promise<undefined>;
|
|
83
|
+
/** Loads the next page of the opened story's variants. */
|
|
84
|
+
moreVariants: () => Promise<undefined>;
|
|
85
|
+
/** Reads again from the first page (manual refresh). */
|
|
86
|
+
refresh(): void;
|
|
87
|
+
/** Observes changes. Returns an unsubscribe function. */
|
|
88
|
+
subscribe(listener: () => void): () => undefined;
|
|
89
|
+
/** Stops following the runtime. */
|
|
90
|
+
dispose(): void;
|
|
91
|
+
};
|
|
92
|
+
/** Catalog state created by {@link createStoriesState}. */
|
|
93
|
+
export type StoriesState = ReturnType<typeof createStoriesState>;
|