@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,191 @@
|
|
|
1
|
+
import { isCatalogState } from '../protocol/catalog.js';
|
|
2
|
+
import { TerminalError } from '../protocol/core.js';
|
|
3
|
+
import { createCatalogClient, isRetiredCatalogRead, isStaleCatalogRead } from './client.js';
|
|
4
|
+
import { createPagedReader } from './paged.js';
|
|
5
|
+
/** Most stories kept loaded; selection pursuit may exceed it so a selected story is never lost. */
|
|
6
|
+
const MAX_LOADED_STORIES = 2000;
|
|
7
|
+
/** Maps a failed catalog read to the reader's reaction. */
|
|
8
|
+
function classify(error) {
|
|
9
|
+
if (isStaleCatalogRead(error))
|
|
10
|
+
return 'stale';
|
|
11
|
+
if (isRetiredCatalogRead(error))
|
|
12
|
+
return 'ignore';
|
|
13
|
+
return { problem: error instanceof TerminalError && error.code === 'unavailable' ? 'Catalog is not ready' : error instanceof Error ? error.message : String(error) };
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Session-lifetime, revision-safe cache of the catalog browser. Pages are read on demand
|
|
17
|
+
* from the child's canonical catalog; a catalog revision event or a stale answer restarts
|
|
18
|
+
* the read, so rows of two revisions are never combined. Everything is dropped as soon as
|
|
19
|
+
* the runtime generation changes: nothing is actionable until the successor's catalog
|
|
20
|
+
* answers, while the selection itself (kept by ID in the view) survives unresolved.
|
|
21
|
+
* @param options Bridge, progress mirror and repaint hook.
|
|
22
|
+
*/
|
|
23
|
+
export function createStoriesState(options) {
|
|
24
|
+
const client = options.client ?? createCatalogClient(options.bridge);
|
|
25
|
+
const listeners = new Set();
|
|
26
|
+
let generation;
|
|
27
|
+
let query = '';
|
|
28
|
+
let catalog;
|
|
29
|
+
let workbench;
|
|
30
|
+
let openStoryId;
|
|
31
|
+
let wantedStory = () => undefined;
|
|
32
|
+
let wantedVariant = () => undefined;
|
|
33
|
+
let selectStory = () => { };
|
|
34
|
+
/** A search was applied and its first page has not landed yet. */
|
|
35
|
+
let searchPending = false;
|
|
36
|
+
let disposed = false;
|
|
37
|
+
/** Notifies subscribers and the repaint hook; faulty listeners never break ingestion. */
|
|
38
|
+
function notify() {
|
|
39
|
+
options.changed?.();
|
|
40
|
+
for (const listener of [...listeners]) {
|
|
41
|
+
try {
|
|
42
|
+
listener();
|
|
43
|
+
}
|
|
44
|
+
catch { }
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
const stories = createPagedReader({
|
|
48
|
+
read: ({ cursor, expectedRevision }) => client.list({ ...(query ? { query } : {}), ...(cursor ? { cursor, expectedRevision } : {}) }),
|
|
49
|
+
select: page => ({ items: page.stories, revision: page.revision, total: page.total, next: page.next }),
|
|
50
|
+
classify,
|
|
51
|
+
idOf: story => story.id,
|
|
52
|
+
wanted: () => wantedStory(),
|
|
53
|
+
accepted(page) {
|
|
54
|
+
catalog = page.state;
|
|
55
|
+
workbench = page.workbench;
|
|
56
|
+
// A selection the new results do not contain is replaced by the first match before the reader
|
|
57
|
+
// goes looking for it across pages (`select` runs before the pursuit of the wanted row).
|
|
58
|
+
if (searchPending) {
|
|
59
|
+
searchPending = false;
|
|
60
|
+
const current = wantedStory();
|
|
61
|
+
if (query && !page.stories.some(story => story.id === current))
|
|
62
|
+
selectStory(page.stories[0]?.id);
|
|
63
|
+
}
|
|
64
|
+
// The variants shown must belong to the revision of the stories around them.
|
|
65
|
+
if (openStoryId && variants.snapshot().revision !== page.revision)
|
|
66
|
+
void variants.restart();
|
|
67
|
+
},
|
|
68
|
+
changed: notify,
|
|
69
|
+
maxItems: MAX_LOADED_STORIES,
|
|
70
|
+
});
|
|
71
|
+
const variants = createPagedReader({
|
|
72
|
+
read: ({ cursor, expectedRevision }) => client.variants({ storyId: openStoryId, ...(query ? { query } : {}), ...(cursor ? { cursor, expectedRevision } : {}) }),
|
|
73
|
+
select: page => ({ items: page.variants, revision: page.revision, total: page.total, next: page.next }),
|
|
74
|
+
classify,
|
|
75
|
+
idOf: variant => variant.id,
|
|
76
|
+
wanted: () => wantedVariant(),
|
|
77
|
+
accepted(page) {
|
|
78
|
+
// A variants page of a newer revision than the stories list: refresh the list too.
|
|
79
|
+
const listed = stories.snapshot().revision;
|
|
80
|
+
if (listed !== undefined && listed !== page.revision)
|
|
81
|
+
void stories.restart();
|
|
82
|
+
},
|
|
83
|
+
changed: notify,
|
|
84
|
+
});
|
|
85
|
+
/** Drops every row: the runtime that produced them is gone. */
|
|
86
|
+
function reset() {
|
|
87
|
+
catalog = undefined;
|
|
88
|
+
workbench = undefined;
|
|
89
|
+
stories.reset();
|
|
90
|
+
variants.reset();
|
|
91
|
+
}
|
|
92
|
+
/** Follows the runtime: a new or restarted generation clears, a ready one is read. */
|
|
93
|
+
function onProgress() {
|
|
94
|
+
const { generation: current, status } = options.progress.snapshot();
|
|
95
|
+
const ready = status === 'ready' && current !== undefined;
|
|
96
|
+
if (generation !== current || !ready) {
|
|
97
|
+
const changedGeneration = generation !== current;
|
|
98
|
+
generation = current;
|
|
99
|
+
if (changedGeneration || stories.snapshot().items.length || catalog)
|
|
100
|
+
reset();
|
|
101
|
+
}
|
|
102
|
+
if (ready && stories.snapshot().status === 'idle') {
|
|
103
|
+
void stories.restart();
|
|
104
|
+
if (openStoryId)
|
|
105
|
+
void variants.restart();
|
|
106
|
+
}
|
|
107
|
+
notify();
|
|
108
|
+
}
|
|
109
|
+
const offProgress = options.progress.subscribe(onProgress);
|
|
110
|
+
const offEvents = options.bridge.subscribe(({ event, data, generation: from }) => {
|
|
111
|
+
if (event !== 'catalog.state' || from !== generation || !isCatalogState(data))
|
|
112
|
+
return;
|
|
113
|
+
const known = catalog?.revision;
|
|
114
|
+
catalog = data;
|
|
115
|
+
// A new revision restarts the reads; an `updating` flip only repaints.
|
|
116
|
+
if (data.revision !== undefined && known !== data.revision && stories.snapshot().status !== 'idle') {
|
|
117
|
+
void stories.restart();
|
|
118
|
+
if (openStoryId)
|
|
119
|
+
void variants.restart();
|
|
120
|
+
}
|
|
121
|
+
notify();
|
|
122
|
+
});
|
|
123
|
+
onProgress();
|
|
124
|
+
return {
|
|
125
|
+
/** Current catalog view for the stories list. */
|
|
126
|
+
view() {
|
|
127
|
+
const snapshot = stories.snapshot();
|
|
128
|
+
const { status } = options.progress.snapshot();
|
|
129
|
+
return { status: status === 'ready' ? snapshot.status : 'waiting', problem: snapshot.problem, query, revision: snapshot.revision, catalog, workbench, generation, stories: snapshot.items, total: snapshot.total, hasMore: snapshot.hasMore };
|
|
130
|
+
},
|
|
131
|
+
/** Current view of the opened story's variants. */
|
|
132
|
+
variants() {
|
|
133
|
+
const snapshot = variants.snapshot();
|
|
134
|
+
return { storyId: openStoryId, status: openStoryId ? snapshot.status : 'idle', problem: snapshot.problem, revision: snapshot.revision, variants: snapshot.items, total: snapshot.total, hasMore: snapshot.hasMore };
|
|
135
|
+
},
|
|
136
|
+
/**
|
|
137
|
+
* Registers the selection providers; reads keep loading pages until the selected row is present.
|
|
138
|
+
* @param story Selected story ID.
|
|
139
|
+
* @param variant Selected variant ID.
|
|
140
|
+
* @param select Selects a story ID; called with the first match when a search's results land.
|
|
141
|
+
*/
|
|
142
|
+
keepVisible(story, variant, select) {
|
|
143
|
+
wantedStory = story;
|
|
144
|
+
wantedVariant = variant;
|
|
145
|
+
if (select)
|
|
146
|
+
selectStory = select;
|
|
147
|
+
},
|
|
148
|
+
/** Applies a search; the child filters its complete catalog, so matches beyond loaded pages are found. */
|
|
149
|
+
search(text) {
|
|
150
|
+
if (text === query)
|
|
151
|
+
return;
|
|
152
|
+
query = text;
|
|
153
|
+
searchPending = true;
|
|
154
|
+
void stories.restart();
|
|
155
|
+
if (openStoryId)
|
|
156
|
+
void variants.restart();
|
|
157
|
+
},
|
|
158
|
+
/** Opens (or, with `undefined`, closes) the variants of a story. */
|
|
159
|
+
openStory(storyId) {
|
|
160
|
+
openStoryId = storyId;
|
|
161
|
+
variants.reset();
|
|
162
|
+
if (storyId !== undefined && stories.snapshot().status !== 'idle')
|
|
163
|
+
void variants.restart();
|
|
164
|
+
notify();
|
|
165
|
+
},
|
|
166
|
+
/** Loads the next page of stories. */
|
|
167
|
+
moreStories: () => stories.more(),
|
|
168
|
+
/** Loads the next page of the opened story's variants. */
|
|
169
|
+
moreVariants: () => variants.more(),
|
|
170
|
+
/** Reads again from the first page (manual refresh). */
|
|
171
|
+
refresh() {
|
|
172
|
+
void stories.restart();
|
|
173
|
+
if (openStoryId)
|
|
174
|
+
void variants.restart();
|
|
175
|
+
},
|
|
176
|
+
/** Observes changes. Returns an unsubscribe function. */
|
|
177
|
+
subscribe(listener) {
|
|
178
|
+
listeners.add(listener);
|
|
179
|
+
return () => void listeners.delete(listener);
|
|
180
|
+
},
|
|
181
|
+
/** Stops following the runtime. */
|
|
182
|
+
dispose() {
|
|
183
|
+
if (disposed)
|
|
184
|
+
return;
|
|
185
|
+
disposed = true;
|
|
186
|
+
offProgress();
|
|
187
|
+
offEvents();
|
|
188
|
+
listeners.clear();
|
|
189
|
+
},
|
|
190
|
+
};
|
|
191
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { CatalogStoryDto, CatalogVariantDto, CatalogWorkbenchDto } from '../protocol/catalog.js';
|
|
2
|
+
/** Result of resolving what a selection can do. */
|
|
3
|
+
export type TargetUrl = {
|
|
4
|
+
url: string;
|
|
5
|
+
} | {
|
|
6
|
+
problem: string;
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* Builds the workbench URL of a story or variant against the public address actually bound
|
|
10
|
+
* (`workbenchUrl` carries origin and base of the running proxy) with the child's router mode.
|
|
11
|
+
* A story without variants, or a docs-only one, opens its story route; no variant ID is invented.
|
|
12
|
+
* The route helpers (and the workbench protocol behind them) load on first use, never before the port binds.
|
|
13
|
+
* @param options Public workbench URL, router facts and the exact selection.
|
|
14
|
+
* @param options.publicUrl Bound public workbench URL, when known.
|
|
15
|
+
* @param options.workbench Base and router mode reported by the runtime.
|
|
16
|
+
* @param options.story Selected story.
|
|
17
|
+
* @param options.variant Selected variant, when the variants list is shown.
|
|
18
|
+
*/
|
|
19
|
+
export declare function resolveTargetUrl(options: {
|
|
20
|
+
publicUrl: string | undefined;
|
|
21
|
+
workbench: CatalogWorkbenchDto | undefined;
|
|
22
|
+
story: CatalogStoryDto;
|
|
23
|
+
variant?: CatalogVariantDto;
|
|
24
|
+
}): Promise<TargetUrl>;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Builds the workbench URL of a story or variant against the public address actually bound
|
|
3
|
+
* (`workbenchUrl` carries origin and base of the running proxy) with the child's router mode.
|
|
4
|
+
* A story without variants, or a docs-only one, opens its story route; no variant ID is invented.
|
|
5
|
+
* The route helpers (and the workbench protocol behind them) load on first use, never before the port binds.
|
|
6
|
+
* @param options Public workbench URL, router facts and the exact selection.
|
|
7
|
+
* @param options.publicUrl Bound public workbench URL, when known.
|
|
8
|
+
* @param options.workbench Base and router mode reported by the runtime.
|
|
9
|
+
* @param options.story Selected story.
|
|
10
|
+
* @param options.variant Selected variant, when the variants list is shown.
|
|
11
|
+
*/
|
|
12
|
+
export async function resolveTargetUrl(options) {
|
|
13
|
+
if (options.story.ambiguous)
|
|
14
|
+
return { problem: 'Story ID is shared by several stories' };
|
|
15
|
+
if (options.variant?.ambiguous)
|
|
16
|
+
return { problem: 'Variant ID is shared by several variants' };
|
|
17
|
+
if (!options.publicUrl)
|
|
18
|
+
return { problem: 'Workbench address is not known yet' };
|
|
19
|
+
if (!options.workbench)
|
|
20
|
+
return { problem: 'Workbench base is unusable' };
|
|
21
|
+
try {
|
|
22
|
+
const { origin } = new URL(options.publicUrl);
|
|
23
|
+
const { createStoryUrl } = await import('../../workbench-url.js');
|
|
24
|
+
return { url: createStoryUrl({ origin, ...options.workbench, storyId: options.story.id, variantId: options.variant?.id }) };
|
|
25
|
+
}
|
|
26
|
+
catch (error) {
|
|
27
|
+
return { problem: error instanceof Error ? error.message : String(error) };
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/** Shared terminal palette: every colored spot goes through semantic helpers so one color decision governs all output. */
|
|
2
|
+
/** Color capability: 0 none, 1 basic 16 colors, 2 256 colors, 3 truecolor. */
|
|
3
|
+
export type ColorLevel = 0 | 1 | 2 | 3;
|
|
4
|
+
/** Environment and stream facts the color decision reads. */
|
|
5
|
+
export type ColorEnvironment = Readonly<Record<string, string | undefined>>;
|
|
6
|
+
/**
|
|
7
|
+
* Decides the color level. `NO_COLOR`, `FORCE_COLOR=0`, `TERM=dumb` and a non-TTY stream
|
|
8
|
+
* disable color; `FORCE_COLOR` 1/2/3 forces a level even without a TTY.
|
|
9
|
+
* @param environment Process environment.
|
|
10
|
+
* @param isTTY Whether the output stream is a terminal.
|
|
11
|
+
*/
|
|
12
|
+
export declare function detectColorLevel(environment?: ColorEnvironment, isTTY?: boolean): ColorLevel;
|
|
13
|
+
/** Semantic text helpers bound to one color level. */
|
|
14
|
+
export interface Style {
|
|
15
|
+
level: ColorLevel;
|
|
16
|
+
/** Brand accent. */
|
|
17
|
+
accent: (text: string) => string;
|
|
18
|
+
/** Positive state. */
|
|
19
|
+
success: (text: string) => string;
|
|
20
|
+
/** Attention state. */
|
|
21
|
+
warn: (text: string) => string;
|
|
22
|
+
/** Failure state. */
|
|
23
|
+
error: (text: string) => string;
|
|
24
|
+
/** De-emphasized text (attribute, kept without color). */
|
|
25
|
+
dim: (text: string) => string;
|
|
26
|
+
/** Emphasized text (attribute, kept without color). */
|
|
27
|
+
bold: (text: string) => string;
|
|
28
|
+
/** Shortcut key: bold accent. */
|
|
29
|
+
key: (text: string) => string;
|
|
30
|
+
/** Title bar: brand background, inverse video without color. */
|
|
31
|
+
title: (text: string) => string;
|
|
32
|
+
/** Hint text `key label · key label`: keys highlighted, the rest dimmed. */
|
|
33
|
+
hints: (text: string) => string;
|
|
34
|
+
/** Appends a reset to a styled line so nothing bleeds into the next one. */
|
|
35
|
+
seal: (line: string) => string;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Builds the palette for a color level. Hues need color; bold, dim and inverse are
|
|
39
|
+
* attributes and stay available so selection and hierarchy survive `NO_COLOR`.
|
|
40
|
+
* @param level Color level; 0 emits no hue.
|
|
41
|
+
* @param attributes Whether bold, dim and inverse are emitted at level 0; `false` makes level 0 fully plain.
|
|
42
|
+
*/
|
|
43
|
+
export declare function createStyle(level: ColorLevel, attributes?: boolean): Style;
|
|
44
|
+
/** Palette for the current process environment, evaluated per call so tests can vary it. */
|
|
45
|
+
export declare function defaultStyle(): Style;
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/** Shared terminal palette: every colored spot goes through semantic helpers so one color decision governs all output. */
|
|
2
|
+
/** Histoire brand green. */
|
|
3
|
+
const BRAND = { rgb: [66, 184, 131], ansi256: 79, basic: 32 };
|
|
4
|
+
/** Foreground hues of the semantic helpers. */
|
|
5
|
+
const HUES = {
|
|
6
|
+
accent: BRAND,
|
|
7
|
+
success: BRAND,
|
|
8
|
+
warn: { rgb: [245, 190, 60], ansi256: 215, basic: 33 },
|
|
9
|
+
error: { rgb: [240, 90, 90], ansi256: 203, basic: 31 },
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Decides the color level. `NO_COLOR`, `FORCE_COLOR=0`, `TERM=dumb` and a non-TTY stream
|
|
13
|
+
* disable color; `FORCE_COLOR` 1/2/3 forces a level even without a TTY.
|
|
14
|
+
* @param environment Process environment.
|
|
15
|
+
* @param isTTY Whether the output stream is a terminal.
|
|
16
|
+
*/
|
|
17
|
+
export function detectColorLevel(environment = process.env, isTTY = process.stdout.isTTY === true) {
|
|
18
|
+
const forced = environment.FORCE_COLOR;
|
|
19
|
+
if (forced === '0' || forced === 'false')
|
|
20
|
+
return 0;
|
|
21
|
+
if (forced !== undefined && forced !== '' && environment.NO_COLOR === undefined) {
|
|
22
|
+
const level = forced === 'true' ? 1 : Number(forced);
|
|
23
|
+
if (level >= 1)
|
|
24
|
+
return Math.min(3, Math.trunc(level));
|
|
25
|
+
}
|
|
26
|
+
if (environment.NO_COLOR)
|
|
27
|
+
return 0;
|
|
28
|
+
if (!isTTY || environment.TERM === 'dumb')
|
|
29
|
+
return 0;
|
|
30
|
+
if (environment.COLORTERM === 'truecolor' || environment.COLORTERM === '24bit')
|
|
31
|
+
return 3;
|
|
32
|
+
return environment.TERM?.includes('256color') ? 2 : 1;
|
|
33
|
+
}
|
|
34
|
+
/** Foreground escape of a hue at a level. */
|
|
35
|
+
function foreground(hue, level) {
|
|
36
|
+
if (level === 3)
|
|
37
|
+
return `38;2;${hue.rgb.join(';')}`;
|
|
38
|
+
return level === 2 ? `38;5;${hue.ansi256}` : String(hue.basic);
|
|
39
|
+
}
|
|
40
|
+
/** Leading shortcut keys of a hint segment (`Up/Down scroll`, `y copy`, `Esc or q back`). */
|
|
41
|
+
const HINT_KEY = /^(?:Up\/Down(?: Home\/End)?|Home\/End|Enter(?: or Esc)?|Esc(?: or q)?|Ctrl\+\w|[A-Za-z/?])(?= )/;
|
|
42
|
+
/**
|
|
43
|
+
* Builds the palette for a color level. Hues need color; bold, dim and inverse are
|
|
44
|
+
* attributes and stay available so selection and hierarchy survive `NO_COLOR`.
|
|
45
|
+
* @param level Color level; 0 emits no hue.
|
|
46
|
+
* @param attributes Whether bold, dim and inverse are emitted at level 0; `false` makes level 0 fully plain.
|
|
47
|
+
*/
|
|
48
|
+
export function createStyle(level, attributes = true) {
|
|
49
|
+
const live = level > 0 || attributes;
|
|
50
|
+
const wrap = (open, close) => live ? (text) => `\u001B[${open}m${text}\u001B[${close}m` : (text) => text;
|
|
51
|
+
const hue = (hueName) => level ? wrap(foreground(HUES[hueName], level), '39') : (text) => text;
|
|
52
|
+
const dim = wrap('2', '22');
|
|
53
|
+
const bold = wrap('1', '22');
|
|
54
|
+
const accent = hue('accent');
|
|
55
|
+
const key = (text) => bold(accent(text));
|
|
56
|
+
const background = level === 3 ? `48;2;${BRAND.rgb.join(';')}` : level === 2 ? `48;5;${BRAND.ansi256}` : `4${BRAND.basic - 30}`;
|
|
57
|
+
const title = level ? (text) => `\u001B[${background};30;1m${text}\u001B[0m` : wrap('7', '27');
|
|
58
|
+
const hints = !live
|
|
59
|
+
? (text) => text
|
|
60
|
+
: (text) => text.split(' · ').map((part) => {
|
|
61
|
+
const match = HINT_KEY.exec(part);
|
|
62
|
+
return match ? `${key(match[0])}${dim(part.slice(match[0].length))}` : dim(part);
|
|
63
|
+
}).join(dim(' · '));
|
|
64
|
+
return {
|
|
65
|
+
level,
|
|
66
|
+
accent,
|
|
67
|
+
success: hue('success'),
|
|
68
|
+
warn: hue('warn'),
|
|
69
|
+
error: hue('error'),
|
|
70
|
+
dim,
|
|
71
|
+
bold,
|
|
72
|
+
key,
|
|
73
|
+
title,
|
|
74
|
+
hints,
|
|
75
|
+
// eslint-disable-next-line no-control-regex -- detects any styling in an already built line
|
|
76
|
+
seal: line => /\u001B\[/.test(line) && !line.endsWith('\u001B[0m') ? `${line}\u001B[0m` : line,
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
/** Palette for the current process environment, evaluated per call so tests can vary it. */
|
|
80
|
+
export function defaultStyle() {
|
|
81
|
+
return createStyle(detectColorLevel());
|
|
82
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/** Terminal activation requested through `HISTOIRE_TUI`. */
|
|
2
|
+
export type TuiEnvironment = 'plain' | 'auto' | 'force';
|
|
3
|
+
/** Rendering mode of the dev command. */
|
|
4
|
+
export type TerminalMode = 'tui' | 'plain';
|
|
5
|
+
/** Inputs of the activation policy; every field is injectable for tests. */
|
|
6
|
+
export interface SupportInputs {
|
|
7
|
+
/** Explicit `--tui` (true) or `--no-tui` (false). */
|
|
8
|
+
flag?: boolean;
|
|
9
|
+
/** Raw `HISTOIRE_TUI` value. */
|
|
10
|
+
env?: string;
|
|
11
|
+
stdin: {
|
|
12
|
+
isTTY?: boolean;
|
|
13
|
+
setRawMode?: unknown;
|
|
14
|
+
};
|
|
15
|
+
stdout: {
|
|
16
|
+
isTTY?: boolean;
|
|
17
|
+
columns?: number;
|
|
18
|
+
rows?: number;
|
|
19
|
+
};
|
|
20
|
+
environment: NodeJS.ProcessEnv;
|
|
21
|
+
execArgv: readonly string[];
|
|
22
|
+
/** Result of `inspector.url()`; defined while an inspector listens. */
|
|
23
|
+
inspectorUrl?: string;
|
|
24
|
+
}
|
|
25
|
+
/** Policy outcome with a short reason when plain was chosen. */
|
|
26
|
+
export interface SupportDecision {
|
|
27
|
+
mode: TerminalMode;
|
|
28
|
+
/** Why plain was chosen; undefined for `tui`. */
|
|
29
|
+
reason?: string;
|
|
30
|
+
/** The terminal was usable but only too small: a resize may recover the TUI. */
|
|
31
|
+
recoverable?: boolean;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Parses `HISTOIRE_TUI`; unset or empty is `auto`.
|
|
35
|
+
* @param value Raw environment value.
|
|
36
|
+
* @throws When the value is not `plain`, `auto` or `1`.
|
|
37
|
+
*/
|
|
38
|
+
export declare function parseTuiEnvironment(value: string | undefined): TuiEnvironment;
|
|
39
|
+
/**
|
|
40
|
+
* Whether Node was started, or inherits options, that enable an inspector.
|
|
41
|
+
* @param execArgv Node options of this process.
|
|
42
|
+
* @param environment Inherited environment (`NODE_OPTIONS`).
|
|
43
|
+
* @param inspectorUrl Active inspector URL, if any.
|
|
44
|
+
*/
|
|
45
|
+
export declare function isInspectorAttached(execArgv: readonly string[], environment: NodeJS.ProcessEnv, inspectorUrl?: string): boolean;
|
|
46
|
+
/**
|
|
47
|
+
* Decides between the interactive panel and plain output (see contracts, Activation and flags).
|
|
48
|
+
* Explicit disable always wins; explicit enable only overrides CI and inspector checks, never
|
|
49
|
+
* missing terminal capabilities or dimensions.
|
|
50
|
+
* @param inputs Flag, environment and stream facts.
|
|
51
|
+
* @throws When `HISTOIRE_TUI` is invalid.
|
|
52
|
+
*/
|
|
53
|
+
export declare function decideTerminalMode(inputs: SupportInputs): SupportDecision;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/** Smallest terminal width the main panel needs. */
|
|
2
|
+
const MIN_COLUMNS = 40;
|
|
3
|
+
/** Smallest terminal height the main panel needs. */
|
|
4
|
+
const MIN_ROWS = 10;
|
|
5
|
+
/**
|
|
6
|
+
* Parses `HISTOIRE_TUI`; unset or empty is `auto`.
|
|
7
|
+
* @param value Raw environment value.
|
|
8
|
+
* @throws When the value is not `plain`, `auto` or `1`.
|
|
9
|
+
*/
|
|
10
|
+
export function parseTuiEnvironment(value) {
|
|
11
|
+
if (value === undefined || value === '' || value === 'auto')
|
|
12
|
+
return 'auto';
|
|
13
|
+
if (value === 'plain')
|
|
14
|
+
return 'plain';
|
|
15
|
+
if (value === '1')
|
|
16
|
+
return 'force';
|
|
17
|
+
throw new Error(`Invalid HISTOIRE_TUI value "${value}". Use plain, auto or 1`);
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Whether Node was started, or inherits options, that enable an inspector.
|
|
21
|
+
* @param execArgv Node options of this process.
|
|
22
|
+
* @param environment Inherited environment (`NODE_OPTIONS`).
|
|
23
|
+
* @param inspectorUrl Active inspector URL, if any.
|
|
24
|
+
*/
|
|
25
|
+
export function isInspectorAttached(execArgv, environment, inspectorUrl) {
|
|
26
|
+
if (inspectorUrl)
|
|
27
|
+
return true;
|
|
28
|
+
const flag = /^--inspect(?:-brk|-wait)?(?:=|$)/;
|
|
29
|
+
return execArgv.some(argument => flag.test(argument)) || (environment.NODE_OPTIONS ?? '').split(/\s+/).some(argument => flag.test(argument));
|
|
30
|
+
}
|
|
31
|
+
/** Whether the environment declares a CI run. */
|
|
32
|
+
function isContinuousIntegration(environment) {
|
|
33
|
+
const value = environment.CI;
|
|
34
|
+
return value !== undefined && value !== '' && value !== '0' && value !== 'false';
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Decides between the interactive panel and plain output (see contracts, Activation and flags).
|
|
38
|
+
* Explicit disable always wins; explicit enable only overrides CI and inspector checks, never
|
|
39
|
+
* missing terminal capabilities or dimensions.
|
|
40
|
+
* @param inputs Flag, environment and stream facts.
|
|
41
|
+
* @throws When `HISTOIRE_TUI` is invalid.
|
|
42
|
+
*/
|
|
43
|
+
export function decideTerminalMode(inputs) {
|
|
44
|
+
const env = parseTuiEnvironment(inputs.env);
|
|
45
|
+
if (inputs.flag === false)
|
|
46
|
+
return { mode: 'plain', reason: '--no-tui' };
|
|
47
|
+
if (inputs.flag === undefined && env === 'plain')
|
|
48
|
+
return { mode: 'plain', reason: 'HISTOIRE_TUI=plain' };
|
|
49
|
+
if (!inputs.stdin.isTTY || !inputs.stdout.isTTY)
|
|
50
|
+
return { mode: 'plain', reason: 'not an interactive terminal' };
|
|
51
|
+
if (typeof inputs.stdin.setRawMode !== 'function')
|
|
52
|
+
return { mode: 'plain', reason: 'raw input unavailable' };
|
|
53
|
+
const term = inputs.environment.TERM;
|
|
54
|
+
if (term === 'dumb' || (process.platform !== 'win32' && !term))
|
|
55
|
+
return { mode: 'plain', reason: 'dumb terminal' };
|
|
56
|
+
if ((inputs.stdout.columns ?? 0) < MIN_COLUMNS || (inputs.stdout.rows ?? 0) < MIN_ROWS)
|
|
57
|
+
return { mode: 'plain', reason: `terminal smaller than ${MIN_COLUMNS}x${MIN_ROWS}`, recoverable: true };
|
|
58
|
+
const forced = inputs.flag === true || env === 'force';
|
|
59
|
+
if (!forced) {
|
|
60
|
+
if (isContinuousIntegration(inputs.environment))
|
|
61
|
+
return { mode: 'plain', reason: 'CI' };
|
|
62
|
+
if (isInspectorAttached(inputs.execArgv, inputs.environment, inputs.inspectorUrl))
|
|
63
|
+
return { mode: 'plain', reason: 'Node inspector attached' };
|
|
64
|
+
}
|
|
65
|
+
return { mode: 'tui' };
|
|
66
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { Writable } from 'node:stream';
|
|
2
|
+
/** Output stream the surface paints on. */
|
|
3
|
+
export type SurfaceStream = Pick<Writable, 'write'> & {
|
|
4
|
+
columns?: number;
|
|
5
|
+
rows?: number;
|
|
6
|
+
};
|
|
7
|
+
/** Source of the pinned panel and of an optional full-screen view. */
|
|
8
|
+
export interface SurfaceContent {
|
|
9
|
+
/** Panel lines (already fitted); drawn under the log area on the normal screen. */
|
|
10
|
+
panel: () => string[];
|
|
11
|
+
/** Full-screen lines while a view is open; undefined shows the normal screen. */
|
|
12
|
+
screen: () => string[] | undefined;
|
|
13
|
+
}
|
|
14
|
+
/** Screen painter: logs scroll above a pinned panel; views use the alternate screen. */
|
|
15
|
+
export interface Surface {
|
|
16
|
+
/** Hides the cursor and draws the panel. Idempotent. */
|
|
17
|
+
attach: () => void;
|
|
18
|
+
/** Removes the panel, leaves the alternate screen, shows the cursor, flushes held text. Idempotent. */
|
|
19
|
+
detach: () => void;
|
|
20
|
+
/** Inserts log output above the panel; held on the alternate screen and shown on return. */
|
|
21
|
+
write: (stream: 'stdout' | 'stderr', data: string | Uint8Array) => void;
|
|
22
|
+
/** Redraws the active screen (state or size changed). */
|
|
23
|
+
repaint: () => void;
|
|
24
|
+
/** Clears the visible normal screen. Retained histories are untouched. */
|
|
25
|
+
clear: () => void;
|
|
26
|
+
/** Whether the surface is attached. */
|
|
27
|
+
readonly attached: boolean;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Creates the painter. Every terminal write is guarded: a failing stream reports once
|
|
31
|
+
* through `onFailure` and the surface goes inert, so a render failure never stops the server.
|
|
32
|
+
* @param options Stream, content providers and failure callback.
|
|
33
|
+
* @param options.stdout Terminal output stream.
|
|
34
|
+
* @param options.content Panel and screen providers.
|
|
35
|
+
* @param options.onFailure Called once when painting fails.
|
|
36
|
+
*/
|
|
37
|
+
export declare function createSurface(options: {
|
|
38
|
+
stdout: SurfaceStream;
|
|
39
|
+
content: SurfaceContent;
|
|
40
|
+
onFailure: (error: unknown) => void;
|
|
41
|
+
}): Surface;
|