@histoire/cli 0.0.0-stage → 1.0.0-beta.2
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 +115 -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,48 @@
|
|
|
1
|
+
import { createStyle, detectColorLevel } from './style.js';
|
|
2
|
+
import { displayWidth, fitWidth } from './width.js';
|
|
3
|
+
/** Zap art rows, each paired with its gradient position (top to bottom). */
|
|
4
|
+
const ZAP = [
|
|
5
|
+
' ▄███▀',
|
|
6
|
+
' ▄███▀ ',
|
|
7
|
+
' ▀▀███▄▄ ',
|
|
8
|
+
' ▄██▀ ',
|
|
9
|
+
' ▄█▀ ',
|
|
10
|
+
' ▀▀ ',
|
|
11
|
+
];
|
|
12
|
+
/** Gradient stops yellow to amber to green, one per art row, per capability class. */
|
|
13
|
+
const GRADIENT = {
|
|
14
|
+
rgb: [[250, 224, 70], [247, 200, 60], [240, 170, 60], [170, 190, 80], [100, 185, 110], [66, 184, 131]],
|
|
15
|
+
ansi256: [227, 221, 215, 149, 78, 79],
|
|
16
|
+
basic: [93, 93, 33, 92, 32, 32],
|
|
17
|
+
};
|
|
18
|
+
/** Cells the art occupies, plus the gap before the text. */
|
|
19
|
+
const ART_WIDTH = Math.max(...ZAP.map(row => displayWidth(row)));
|
|
20
|
+
const GAP = 2;
|
|
21
|
+
/** Colors one art row for a level. */
|
|
22
|
+
function paint(row, index, level) {
|
|
23
|
+
if (!level)
|
|
24
|
+
return row;
|
|
25
|
+
const code = level === 3 ? `38;2;${GRADIENT.rgb[index].join(';')}` : level === 2 ? `38;5;${GRADIENT.ansi256[index]}` : String(GRADIENT.basic[index]);
|
|
26
|
+
return `\u001B[${code}m${row}\u001B[39m`;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Startup banner: a small zap beside the version and address. Lines are at most `columns`
|
|
30
|
+
* cells. A terminal too narrow for the art gets the version line only; with color off the
|
|
31
|
+
* output has no escape sequences at all.
|
|
32
|
+
* @param options Version, width, color decision and optional address.
|
|
33
|
+
*/
|
|
34
|
+
export function renderBanner(options) {
|
|
35
|
+
const level = typeof options.color === 'number' ? options.color : options.color ? Math.max(1, detectColorLevel(process.env, true)) : 0;
|
|
36
|
+
const style = createStyle(level, false);
|
|
37
|
+
const heading = `${style.bold(style.accent('Histoire'))} ${style.dim(`v${options.version}`)}`;
|
|
38
|
+
const headingWidth = displayWidth(`Histoire v${options.version}`);
|
|
39
|
+
if (options.columns < ART_WIDTH + GAP + headingWidth)
|
|
40
|
+
return [style.seal(fitWidth(heading, options.columns))];
|
|
41
|
+
// Text sits beside the middle art rows.
|
|
42
|
+
const text = new Map(options.url ? [[1, heading], [3, `${style.dim('Local')} ${options.url}`]] : [[2, heading]]);
|
|
43
|
+
const budget = options.columns - ART_WIDTH - GAP;
|
|
44
|
+
return ZAP.map((row, index) => {
|
|
45
|
+
const line = `${paint(row, index, level)}${' '.repeat(GAP)}${fitWidth(text.get(index) ?? '', budget)}`;
|
|
46
|
+
return style.seal(line.trimEnd());
|
|
47
|
+
});
|
|
48
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { RuntimeEvent, RuntimeReply, RuntimeSendResult } from '@histoire/dev-proxy/protocol';
|
|
2
|
+
import type { TerminalEventMap, TerminalOperationMap } from './protocol/core.js';
|
|
3
|
+
import type { TerminalEvents, TerminalOperations } from './protocol/index.js';
|
|
4
|
+
/** Hands one command to the current ready runtime generation (the proxy handle). */
|
|
5
|
+
export interface TerminalTransport {
|
|
6
|
+
/** Must reject nothing; unavailable runtimes answer with `{ ok: false }`. */
|
|
7
|
+
send: (command: {
|
|
8
|
+
id: string;
|
|
9
|
+
operation: string;
|
|
10
|
+
args?: unknown;
|
|
11
|
+
}) => Promise<RuntimeSendResult>;
|
|
12
|
+
}
|
|
13
|
+
/** Per-call options of {@link TerminalBridge.send}. */
|
|
14
|
+
export interface TerminalSendOptions {
|
|
15
|
+
/** Read deadline in milliseconds; defaults to 10 seconds. */
|
|
16
|
+
timeout?: number;
|
|
17
|
+
}
|
|
18
|
+
/** One child event, tagged with the generation that produced it. */
|
|
19
|
+
export interface TerminalBridgeEvent<Events extends TerminalEventMap = TerminalEvents> {
|
|
20
|
+
/** Producing generation. */
|
|
21
|
+
generation: string;
|
|
22
|
+
/** Event name. */
|
|
23
|
+
event: keyof Events & string;
|
|
24
|
+
/** Detached payload; domain code validates it. */
|
|
25
|
+
data: Events[keyof Events & string];
|
|
26
|
+
}
|
|
27
|
+
/** Parent side of the private command channel. */
|
|
28
|
+
export interface TerminalBridge<Operations extends TerminalOperationMap = TerminalOperations, Events extends TerminalEventMap = TerminalEvents> {
|
|
29
|
+
/**
|
|
30
|
+
* Sends one operation and settles exactly once: on reply, timeout, generation
|
|
31
|
+
* retirement, transport failure or bridge close. Timeout never cancels work
|
|
32
|
+
* the child already admitted and is never retried.
|
|
33
|
+
*/
|
|
34
|
+
send: <K extends keyof Operations & string>(operation: K, args: Operations[K]['args'], options?: TerminalSendOptions) => Promise<Operations[K]['result']>;
|
|
35
|
+
/** Subscribes to current-generation events; listener faults are isolated. Returns an unsubscribe function. */
|
|
36
|
+
subscribe: (listener: (event: TerminalBridgeEvent<Events>) => void) => () => void;
|
|
37
|
+
/** Feeds a proxy `onRuntimeMessage` frame into the bridge. */
|
|
38
|
+
receive: (message: RuntimeReply | RuntimeEvent) => void;
|
|
39
|
+
/** Rejects every read of a generation and ignores its later frames (proxy `onRuntimeRetired`). */
|
|
40
|
+
retire: (generation: string) => void;
|
|
41
|
+
/** Rejects all pending reads and drops listeners; later sends fail with `closed`. */
|
|
42
|
+
close: () => void;
|
|
43
|
+
/** Number of reads currently awaiting an answer. */
|
|
44
|
+
readonly pending: number;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Creates the bridge over a transport. Owns the pending-read registry and its
|
|
48
|
+
* timers; imports nothing from the runtime graph so it can load before binding.
|
|
49
|
+
* @param options Transport, default timeout, and an identifier source (tests only).
|
|
50
|
+
* @param options.transport Delivers commands to the ready runtime.
|
|
51
|
+
* @param options.timeoutMs Default read deadline.
|
|
52
|
+
* @param options.createId Overrides identifier generation.
|
|
53
|
+
*/
|
|
54
|
+
export declare function createTerminalBridge<Operations extends TerminalOperationMap = TerminalOperations, Events extends TerminalEventMap = TerminalEvents>(options: {
|
|
55
|
+
transport: TerminalTransport;
|
|
56
|
+
timeoutMs?: number;
|
|
57
|
+
createId?: () => string;
|
|
58
|
+
}): TerminalBridge<Operations, Events>;
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { isBoundedCommand } from '@histoire/dev-proxy/protocol';
|
|
3
|
+
import { TERMINAL_BOUNDS, TerminalError } from './protocol/core.js';
|
|
4
|
+
/** Remembered retired generations; far above any realistic restart burst in one session. */
|
|
5
|
+
const RETIRED_MEMORY = 64;
|
|
6
|
+
/**
|
|
7
|
+
* Creates the bridge over a transport. Owns the pending-read registry and its
|
|
8
|
+
* timers; imports nothing from the runtime graph so it can load before binding.
|
|
9
|
+
* @param options Transport, default timeout, and an identifier source (tests only).
|
|
10
|
+
* @param options.transport Delivers commands to the ready runtime.
|
|
11
|
+
* @param options.timeoutMs Default read deadline.
|
|
12
|
+
* @param options.createId Overrides identifier generation.
|
|
13
|
+
*/
|
|
14
|
+
export function createTerminalBridge(options) {
|
|
15
|
+
const pending = new Map();
|
|
16
|
+
const listeners = new Set();
|
|
17
|
+
const retired = new Set();
|
|
18
|
+
let closed = false;
|
|
19
|
+
/** Settles a read once: removing it first makes every later settle a no-op. */
|
|
20
|
+
function settle(id, action) {
|
|
21
|
+
const read = pending.get(id);
|
|
22
|
+
if (!read)
|
|
23
|
+
return;
|
|
24
|
+
pending.delete(id);
|
|
25
|
+
clearTimeout(read.timer);
|
|
26
|
+
action(read);
|
|
27
|
+
}
|
|
28
|
+
/** Rejects reads for a retired generation. */
|
|
29
|
+
function reject(id, error) {
|
|
30
|
+
settle(id, read => read.reject(error));
|
|
31
|
+
}
|
|
32
|
+
return {
|
|
33
|
+
get pending() {
|
|
34
|
+
return pending.size;
|
|
35
|
+
},
|
|
36
|
+
send(operation, args, sendOptions) {
|
|
37
|
+
if (closed)
|
|
38
|
+
return Promise.reject(new TerminalError('closed', 'Terminal bridge closed'));
|
|
39
|
+
if (pending.size >= TERMINAL_BOUNDS.pendingReads)
|
|
40
|
+
return Promise.reject(new TerminalError('busy', 'Too many pending reads'));
|
|
41
|
+
const id = options.createId?.() ?? randomUUID();
|
|
42
|
+
const command = { id, operation, args };
|
|
43
|
+
if (!isBoundedCommand(command))
|
|
44
|
+
return Promise.reject(new TerminalError('invalid', 'Command exceeds the frame or identifier bounds'));
|
|
45
|
+
return new Promise((resolve, rejectRead) => {
|
|
46
|
+
const timer = setTimeout(() => reject(id, new TerminalError('timeout', `${operation} timed out`)), sendOptions?.timeout ?? options.timeoutMs ?? TERMINAL_BOUNDS.readTimeoutMs);
|
|
47
|
+
// Pending reads must never keep the CLI alive.
|
|
48
|
+
timer.unref();
|
|
49
|
+
pending.set(id, { resolve, reject: rejectRead, timer });
|
|
50
|
+
options.transport.send(command).then((result) => {
|
|
51
|
+
if (result.ok === false)
|
|
52
|
+
return reject(id, new TerminalError('unavailable', `Runtime is ${result.reason}`));
|
|
53
|
+
if (retired.has(result.generation))
|
|
54
|
+
return reject(id, new TerminalError('retired', 'Runtime generation retired'));
|
|
55
|
+
const read = pending.get(id);
|
|
56
|
+
if (read)
|
|
57
|
+
read.generation = result.generation;
|
|
58
|
+
}, () => reject(id, new TerminalError('unavailable', 'Runtime channel failed')));
|
|
59
|
+
});
|
|
60
|
+
},
|
|
61
|
+
subscribe(listener) {
|
|
62
|
+
listeners.add(listener);
|
|
63
|
+
return () => listeners.delete(listener);
|
|
64
|
+
},
|
|
65
|
+
receive(message) {
|
|
66
|
+
if (closed || retired.has(message.generation))
|
|
67
|
+
return;
|
|
68
|
+
if (message.type === 'runtime-event') {
|
|
69
|
+
const event = { generation: message.generation, event: message.event, data: message.data };
|
|
70
|
+
for (const listener of [...listeners]) {
|
|
71
|
+
try {
|
|
72
|
+
listener(event);
|
|
73
|
+
}
|
|
74
|
+
catch { }
|
|
75
|
+
}
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
// A reply only settles the read it was addressed to, in the generation that answered it.
|
|
79
|
+
const read = pending.get(message.id);
|
|
80
|
+
if (!read || (read.generation !== undefined && read.generation !== message.generation))
|
|
81
|
+
return;
|
|
82
|
+
if (message.ok === false)
|
|
83
|
+
settle(message.id, entry => entry.reject(new TerminalError(message.error.code, message.error.message)));
|
|
84
|
+
else
|
|
85
|
+
settle(message.id, entry => entry.resolve(message.result));
|
|
86
|
+
},
|
|
87
|
+
retire(generation) {
|
|
88
|
+
retired.add(generation);
|
|
89
|
+
if (retired.size > RETIRED_MEMORY)
|
|
90
|
+
retired.delete(retired.values().next().value);
|
|
91
|
+
for (const [id, read] of pending) {
|
|
92
|
+
if (read.generation === generation)
|
|
93
|
+
reject(id, new TerminalError('retired', 'Runtime generation retired'));
|
|
94
|
+
}
|
|
95
|
+
},
|
|
96
|
+
close() {
|
|
97
|
+
closed = true;
|
|
98
|
+
listeners.clear();
|
|
99
|
+
for (const id of [...pending.keys()])
|
|
100
|
+
reject(id, new TerminalError('closed', 'Terminal bridge closed'));
|
|
101
|
+
},
|
|
102
|
+
};
|
|
103
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { ChildProcess, SpawnOptions } from 'node:child_process';
|
|
2
|
+
import type { Writable } from 'node:stream';
|
|
3
|
+
/** Outcome of a clipboard write. */
|
|
4
|
+
export interface CopyResult {
|
|
5
|
+
ok: boolean;
|
|
6
|
+
/** Why the write failed; present when `ok` is false. */
|
|
7
|
+
reason?: string;
|
|
8
|
+
/** Caveat on success, e.g. that only a terminal request was sent and its acceptance is unknown. */
|
|
9
|
+
note?: string;
|
|
10
|
+
}
|
|
11
|
+
/** Writes text to the system clipboard; implementations never throw. */
|
|
12
|
+
export type ClipboardWriter = (text: string) => Promise<CopyResult>;
|
|
13
|
+
/** Default writer until a platform clipboard is installed; fails plainly. */
|
|
14
|
+
export declare const unavailableClipboard: ClipboardWriter;
|
|
15
|
+
/** One platform command that reads the clipboard text from stdin. */
|
|
16
|
+
export interface ClipboardCommand {
|
|
17
|
+
command: string;
|
|
18
|
+
args: readonly string[];
|
|
19
|
+
/** Encodes the text the command expects. */
|
|
20
|
+
encode?: (text: string) => string | Uint8Array;
|
|
21
|
+
}
|
|
22
|
+
/** Minimal child process the writer needs; satisfied by `ChildProcess`. */
|
|
23
|
+
export type ClipboardProcess = Pick<ChildProcess, 'stdin' | 'on' | 'kill'>;
|
|
24
|
+
/** Boundary to the platform: process creation and the terminal. Replaced only in tests. */
|
|
25
|
+
export interface ClipboardPlatform {
|
|
26
|
+
platform: NodeJS.Platform;
|
|
27
|
+
env: NodeJS.ProcessEnv;
|
|
28
|
+
/** Starts a command; lazy so `node:child_process` loads only when copying. */
|
|
29
|
+
spawn: (command: string, args: readonly string[], options: SpawnOptions) => ClipboardProcess | Promise<ClipboardProcess>;
|
|
30
|
+
/** Interactive terminal stream for the OSC 52 fallback; undefined when not a TTY. */
|
|
31
|
+
terminal?: Pick<Writable, 'write'>;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Candidate commands for a platform, preferred first. Wayland sessions try
|
|
35
|
+
* `wl-copy` before the X11 tools.
|
|
36
|
+
* @param platform Operating system.
|
|
37
|
+
* @param env Environment (session type detection).
|
|
38
|
+
*/
|
|
39
|
+
export declare function clipboardCommands(platform: NodeJS.Platform, env: NodeJS.ProcessEnv): ClipboardCommand[];
|
|
40
|
+
/**
|
|
41
|
+
* Creates the system clipboard writer. Platform commands are tried in order; when
|
|
42
|
+
* none works and the output is an interactive terminal, an OSC 52 request is sent
|
|
43
|
+
* (works over SSH and in many multiplexers, but acceptance cannot be observed).
|
|
44
|
+
* @param platform Platform boundary; defaults to the real process, spawned lazily.
|
|
45
|
+
*/
|
|
46
|
+
export declare function createClipboardWriter(platform?: Partial<ClipboardPlatform>): ClipboardWriter;
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { Buffer } from 'node:buffer';
|
|
2
|
+
/** Default writer until a platform clipboard is installed; fails plainly. */
|
|
3
|
+
export const unavailableClipboard = async () => ({ ok: false, reason: 'clipboard is not available' });
|
|
4
|
+
/** Marks UTF-16 text for `clip.exe`. */
|
|
5
|
+
const BYTE_ORDER_MARK = String.fromCodePoint(0xFEFF);
|
|
6
|
+
/** Longest wait for a clipboard helper before giving up on it. */
|
|
7
|
+
const COMMAND_TIMEOUT_MS = 3000;
|
|
8
|
+
/** Largest payload sent through OSC 52; terminals commonly cap it near 100 KB. */
|
|
9
|
+
const OSC52_MAX_BYTES = 74_994;
|
|
10
|
+
/**
|
|
11
|
+
* Candidate commands for a platform, preferred first. Wayland sessions try
|
|
12
|
+
* `wl-copy` before the X11 tools.
|
|
13
|
+
* @param platform Operating system.
|
|
14
|
+
* @param env Environment (session type detection).
|
|
15
|
+
*/
|
|
16
|
+
export function clipboardCommands(platform, env) {
|
|
17
|
+
if (platform === 'darwin')
|
|
18
|
+
return [{ command: 'pbcopy', args: [] }];
|
|
19
|
+
// clip.exe reads UTF-16LE with a byte order mark as Unicode text.
|
|
20
|
+
if (platform === 'win32')
|
|
21
|
+
return [{ command: 'clip', args: [], encode: text => Buffer.from(`${BYTE_ORDER_MARK}${text}`, 'utf16le') }];
|
|
22
|
+
const x11 = [{ command: 'xclip', args: ['-selection', 'clipboard', '-in'] }, { command: 'xsel', args: ['--clipboard', '--input'] }];
|
|
23
|
+
return env.WAYLAND_DISPLAY ? [{ command: 'wl-copy', args: [] }, ...x11] : [...x11, { command: 'wl-copy', args: [] }];
|
|
24
|
+
}
|
|
25
|
+
/** Runs one command with the text on stdin; resolves a failure reason or undefined on success. */
|
|
26
|
+
async function run(platform, candidate, text) {
|
|
27
|
+
const child = await platform.spawn(candidate.command, candidate.args, { stdio: ['pipe', 'ignore', 'ignore'], windowsHide: true });
|
|
28
|
+
return new Promise((resolve) => {
|
|
29
|
+
const timer = setTimeout(() => {
|
|
30
|
+
child.kill();
|
|
31
|
+
resolve(`${candidate.command} timed out`);
|
|
32
|
+
}, COMMAND_TIMEOUT_MS);
|
|
33
|
+
timer.unref();
|
|
34
|
+
const done = (reason) => {
|
|
35
|
+
clearTimeout(timer);
|
|
36
|
+
resolve(reason);
|
|
37
|
+
};
|
|
38
|
+
child.on('error', (error) => done(error.code === 'ENOENT' ? 'missing' : error.message));
|
|
39
|
+
child.on('close', code => done(code === 0 ? undefined : `${candidate.command} exited with ${code}`));
|
|
40
|
+
// A helper that dies early closes its pipe; the close event reports it.
|
|
41
|
+
child.stdin?.on('error', () => { });
|
|
42
|
+
child.stdin?.end(candidate.encode ? candidate.encode(text) : text);
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Creates the system clipboard writer. Platform commands are tried in order; when
|
|
47
|
+
* none works and the output is an interactive terminal, an OSC 52 request is sent
|
|
48
|
+
* (works over SSH and in many multiplexers, but acceptance cannot be observed).
|
|
49
|
+
* @param platform Platform boundary; defaults to the real process, spawned lazily.
|
|
50
|
+
*/
|
|
51
|
+
export function createClipboardWriter(platform = {}) {
|
|
52
|
+
const boundary = {
|
|
53
|
+
platform: process.platform,
|
|
54
|
+
env: process.env,
|
|
55
|
+
spawn: async (command, args, options) => (await import('node:child_process')).spawn(command, args, options),
|
|
56
|
+
terminal: process.stdout.isTTY ? process.stdout : undefined,
|
|
57
|
+
...platform,
|
|
58
|
+
};
|
|
59
|
+
return async (text) => {
|
|
60
|
+
const failures = [];
|
|
61
|
+
for (const candidate of clipboardCommands(boundary.platform, boundary.env)) {
|
|
62
|
+
try {
|
|
63
|
+
const failure = await run(boundary, candidate, text);
|
|
64
|
+
if (!failure)
|
|
65
|
+
return { ok: true };
|
|
66
|
+
if (failure !== 'missing')
|
|
67
|
+
failures.push(failure);
|
|
68
|
+
}
|
|
69
|
+
catch (error) {
|
|
70
|
+
failures.push(error instanceof Error ? error.message : String(error));
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
const payload = Buffer.from(text);
|
|
74
|
+
if (boundary.terminal && payload.length <= OSC52_MAX_BYTES) {
|
|
75
|
+
try {
|
|
76
|
+
boundary.terminal.write(`\u001B]52;c;${payload.toString('base64')}\u0007`);
|
|
77
|
+
return { ok: true, note: 'sent as a terminal request, support varies' };
|
|
78
|
+
}
|
|
79
|
+
catch (error) {
|
|
80
|
+
failures.push(error instanceof Error ? error.message : String(error));
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return { ok: false, reason: failures[0] ?? (payload.length > OSC52_MAX_BYTES ? 'text is too large for a terminal request and no clipboard tool was found' : 'no clipboard tool found') };
|
|
84
|
+
};
|
|
85
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { TerminalViewHost } from '../views/registry.js';
|
|
2
|
+
/**
|
|
3
|
+
* Contributes runtime diagnostics to the session info screen and a compact count to the main
|
|
4
|
+
* panel. Runs once per session (called by the requests view factory). Data is read from the
|
|
5
|
+
* current generation after each startup phase, and while the block is displayed at most every
|
|
6
|
+
* two seconds, so changes after startup (story edits, MCP clients) appear without a restart.
|
|
7
|
+
* @param host View host.
|
|
8
|
+
*/
|
|
9
|
+
export declare function attachDiagnostics(host: TerminalViewHost): void;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { createDiagnosticsClient } from './client.js';
|
|
2
|
+
import { buildDiagnosticsLines } from './lines.js';
|
|
3
|
+
/** Shortest time between refreshes triggered by showing the block. */
|
|
4
|
+
const REFRESH_INTERVAL_MS = 2000;
|
|
5
|
+
/**
|
|
6
|
+
* Contributes runtime diagnostics to the session info screen and a compact count to the main
|
|
7
|
+
* panel. Runs once per session (called by the requests view factory). Data is read from the
|
|
8
|
+
* current generation after each startup phase, and while the block is displayed at most every
|
|
9
|
+
* two seconds, so changes after startup (story edits, MCP clients) appear without a restart.
|
|
10
|
+
* @param host View host.
|
|
11
|
+
*/
|
|
12
|
+
export function attachDiagnostics(host) {
|
|
13
|
+
const client = createDiagnosticsClient(host.bridge, host.progress, () => host.requestRender());
|
|
14
|
+
let lastKey = '';
|
|
15
|
+
let lastRefresh = 0;
|
|
16
|
+
let reading = false;
|
|
17
|
+
/** Reads now unless a read is running. */
|
|
18
|
+
function refresh() {
|
|
19
|
+
if (reading)
|
|
20
|
+
return;
|
|
21
|
+
reading = true;
|
|
22
|
+
lastRefresh = Date.now();
|
|
23
|
+
void client.refresh().finally(() => (reading = false));
|
|
24
|
+
}
|
|
25
|
+
// Startup phases finishing (collection, revalidation) change what the runtime reports.
|
|
26
|
+
host.progress.subscribe((snapshot) => {
|
|
27
|
+
const key = `${snapshot.generation}:${snapshot.status}:${snapshot.phases.filter(phase => phase.status === 'completed').length}`;
|
|
28
|
+
if (key === lastKey)
|
|
29
|
+
return;
|
|
30
|
+
lastKey = key;
|
|
31
|
+
if (snapshot.status === 'ready')
|
|
32
|
+
refresh();
|
|
33
|
+
});
|
|
34
|
+
host.addInfoSection(() => {
|
|
35
|
+
if (Date.now() - lastRefresh > REFRESH_INTERVAL_MS)
|
|
36
|
+
refresh();
|
|
37
|
+
const { value, problem } = client.current();
|
|
38
|
+
return { title: 'Diagnostics', lines: buildDiagnosticsLines({ diagnostics: value, problem, requests: host.requests }) };
|
|
39
|
+
});
|
|
40
|
+
host.addPanelSegment(() => {
|
|
41
|
+
const issues = client.current().value?.collection?.issues ?? 0;
|
|
42
|
+
const failed = host.requests.entries().filter(entry => entry.outcome === 'failed').length;
|
|
43
|
+
const parts = [...(issues ? [`${issues} ${issues === 1 ? 'issue' : 'issues'}`] : []), ...(failed ? [`${failed} failed ${failed === 1 ? 'request' : 'requests'}`] : [])];
|
|
44
|
+
return parts.length ? parts.join(' · ') : undefined;
|
|
45
|
+
});
|
|
46
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { TerminalBridge } from '../bridge.js';
|
|
2
|
+
import type { GenerationClient } from '../info/generation-client.js';
|
|
3
|
+
import type { ProgressState } from '../progress/state.js';
|
|
4
|
+
import type { DiagnosticsDto } from '../protocol/diagnostics.js';
|
|
5
|
+
/**
|
|
6
|
+
* Typed `diagnostics.get` wrapper. The reply is tagged with the generation it was requested
|
|
7
|
+
* for and hidden once another generation is current, so diagnostics stay generation-owned.
|
|
8
|
+
* @param bridge Command bridge.
|
|
9
|
+
* @param progress Progress mirror.
|
|
10
|
+
* @param changed Called when a reply or failure was recorded (repaint).
|
|
11
|
+
*/
|
|
12
|
+
export declare function createDiagnosticsClient(bridge: TerminalBridge, progress: ProgressState, changed: () => void): GenerationClient<DiagnosticsDto>;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { createGenerationClient } from '../info/generation-client.js';
|
|
2
|
+
import { isDiagnostics } from '../protocol/diagnostics.js';
|
|
3
|
+
/**
|
|
4
|
+
* Typed `diagnostics.get` wrapper. The reply is tagged with the generation it was requested
|
|
5
|
+
* for and hidden once another generation is current, so diagnostics stay generation-owned.
|
|
6
|
+
* @param bridge Command bridge.
|
|
7
|
+
* @param progress Progress mirror.
|
|
8
|
+
* @param changed Called when a reply or failure was recorded (repaint).
|
|
9
|
+
*/
|
|
10
|
+
export function createDiagnosticsClient(bridge, progress, changed) {
|
|
11
|
+
return createGenerationClient({
|
|
12
|
+
read: () => bridge.send('diagnostics.get', {}),
|
|
13
|
+
validate: isDiagnostics,
|
|
14
|
+
progress,
|
|
15
|
+
changed,
|
|
16
|
+
unusable: 'Runtime sent unusable diagnostics',
|
|
17
|
+
unavailable: 'Diagnostics unavailable',
|
|
18
|
+
});
|
|
19
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { DiagnosticsDto } from '../protocol/diagnostics.js';
|
|
2
|
+
import type { RequestHistory } from '../requests/history.js';
|
|
3
|
+
/** Everything the diagnostics block shows. */
|
|
4
|
+
export interface DiagnosticsLinesInput {
|
|
5
|
+
/** Reply of the current generation only. */
|
|
6
|
+
diagnostics?: DiagnosticsDto;
|
|
7
|
+
/** Why `diagnostics` is missing, when known. */
|
|
8
|
+
problem?: string;
|
|
9
|
+
requests: RequestHistory;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Plain rows of the session screen's diagnostics block. Sections fail independently: a
|
|
13
|
+
* collection problem does not hide the MCP summary, and a failed retrieval does not hide
|
|
14
|
+
* the request summary, which the parent owns.
|
|
15
|
+
* @param input Live state.
|
|
16
|
+
*/
|
|
17
|
+
export declare function buildDiagnosticsLines(input: DiagnosticsLinesInput): string[];
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { sanitizeLine } from '../output/sanitize.js';
|
|
2
|
+
/** Aligned `label value` row; the value may come from the child, so it is made safe here. */
|
|
3
|
+
const field = (label, value) => `${label.padEnd(11)} ${sanitizeLine(value)}`;
|
|
4
|
+
/** `1 client`, `2 clients`. */
|
|
5
|
+
const count = (amount, noun) => `${amount} ${noun}${amount === 1 ? '' : 's'}`;
|
|
6
|
+
/** Request counts, from the parent's own history (independent of runtime availability). */
|
|
7
|
+
function requestSummary(requests) {
|
|
8
|
+
const entries = requests.entries();
|
|
9
|
+
const failed = entries.filter(entry => entry.outcome === 'failed').length;
|
|
10
|
+
const aborted = entries.filter(entry => entry.outcome === 'aborted').length;
|
|
11
|
+
const parts = [`${entries.length} retained`];
|
|
12
|
+
if (failed)
|
|
13
|
+
parts.push(`${failed} failed`);
|
|
14
|
+
if (aborted)
|
|
15
|
+
parts.push(`${aborted} aborted`);
|
|
16
|
+
if (requests.inFlight())
|
|
17
|
+
parts.push(`${requests.inFlight()} in flight`);
|
|
18
|
+
if (requests.omitted)
|
|
19
|
+
parts.push(`${requests.omitted} older omitted`);
|
|
20
|
+
return parts.join(' · ');
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Plain rows of the session screen's diagnostics block. Sections fail independently: a
|
|
24
|
+
* collection problem does not hide the MCP summary, and a failed retrieval does not hide
|
|
25
|
+
* the request summary, which the parent owns.
|
|
26
|
+
* @param input Live state.
|
|
27
|
+
*/
|
|
28
|
+
export function buildDiagnosticsLines(input) {
|
|
29
|
+
const { diagnostics } = input;
|
|
30
|
+
const lines = [];
|
|
31
|
+
if (!diagnostics) {
|
|
32
|
+
lines.push(field('Runtime', input.problem ?? 'Waiting for the runtime'));
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
const { collection, mcp, problems } = diagnostics;
|
|
36
|
+
if (problems.collection) {
|
|
37
|
+
lines.push(field('Collection', `unavailable: ${problems.collection}`));
|
|
38
|
+
}
|
|
39
|
+
else if (collection) {
|
|
40
|
+
lines.push(field('Collection', `${collection.outcome} · ${count(collection.issues, 'issue')}${collection.truncated ? ' (more omitted)' : ''}`));
|
|
41
|
+
for (const issue of collection.items)
|
|
42
|
+
lines.push(` ${sanitizeLine(`${issue.code}${issue.file ? ` ${issue.file}` : ''}: ${issue.message}`)}`);
|
|
43
|
+
if (collection.issues > collection.items.length)
|
|
44
|
+
lines.push(` … ${collection.issues - collection.items.length} more issues`);
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
lines.push(field('Collection', 'not finished yet'));
|
|
48
|
+
}
|
|
49
|
+
if (problems.mcp) {
|
|
50
|
+
lines.push(field('MCP', `unavailable: ${problems.mcp}`));
|
|
51
|
+
}
|
|
52
|
+
else if (mcp) {
|
|
53
|
+
const detail = mcp.status === 'enabled'
|
|
54
|
+
? [mcp.endpoint ?? 'starting', ...(mcp.tokenRequired ? ['bearer token required'] : []), count(mcp.clients.length + mcp.clientsOmitted, 'client') + (mcp.clients.length ? ` (${mcp.clients.map(client => client.name).join(', ')}${mcp.clientsOmitted ? ', …' : ''})` : ''), `${mcp.activeOperations} active`]
|
|
55
|
+
: [mcp.status];
|
|
56
|
+
lines.push(field('MCP', detail.join(' · ')));
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
lines.push(field('Requests', requestSummary(input.requests)));
|
|
60
|
+
return lines;
|
|
61
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { ActionHost } from './actions.js';
|
|
2
|
+
import type { TerminalKey } from './keys.js';
|
|
3
|
+
import type { TerminalView, ViewRegistry } from './views/registry.js';
|
|
4
|
+
/** What key routing can do; implemented by the session. */
|
|
5
|
+
export interface DispatchContext {
|
|
6
|
+
actions: ActionHost;
|
|
7
|
+
registry: ViewRegistry;
|
|
8
|
+
/** View currently owning the screen. */
|
|
9
|
+
activeView: () => TerminalView | undefined;
|
|
10
|
+
/** Gives a view the screen. */
|
|
11
|
+
openView: (view: TerminalView) => void;
|
|
12
|
+
/** Returns to the main screen. */
|
|
13
|
+
closeView: () => void;
|
|
14
|
+
/** Releases the terminal and stops the process until resumed (Unix job control). */
|
|
15
|
+
suspend: () => void;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Routes one key. Order is the whole contract:
|
|
19
|
+
* 1. Ctrl+C always quits, even while a search prompt is open.
|
|
20
|
+
* 2. An open screen owns the keys: a text prompt gets all of them, otherwise the view
|
|
21
|
+
* may consume the key, and unconsumed Escape or `q` returns to main. Main actions
|
|
22
|
+
* never fire inside a screen.
|
|
23
|
+
* 3. On the main screen, a registered view shortcut opens it, a main action runs, and
|
|
24
|
+
* a contracted but unregistered shortcut reports that it is unavailable.
|
|
25
|
+
* @param context Session capabilities.
|
|
26
|
+
* @param key Decoded key.
|
|
27
|
+
*/
|
|
28
|
+
export declare function dispatchKey(context: DispatchContext, key: TerminalKey): void;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { guarded, MAIN_ACTIONS } from './actions.js';
|
|
2
|
+
/**
|
|
3
|
+
* Routes one key. Order is the whole contract:
|
|
4
|
+
* 1. Ctrl+C always quits, even while a search prompt is open.
|
|
5
|
+
* 2. An open screen owns the keys: a text prompt gets all of them, otherwise the view
|
|
6
|
+
* may consume the key, and unconsumed Escape or `q` returns to main. Main actions
|
|
7
|
+
* never fire inside a screen.
|
|
8
|
+
* 3. On the main screen, a registered view shortcut opens it, a main action runs, and
|
|
9
|
+
* a contracted but unregistered shortcut reports that it is unavailable.
|
|
10
|
+
* @param context Session capabilities.
|
|
11
|
+
* @param key Decoded key.
|
|
12
|
+
*/
|
|
13
|
+
export function dispatchKey(context, key) {
|
|
14
|
+
if (key.ctrl) {
|
|
15
|
+
if (key.name === 'c')
|
|
16
|
+
context.actions.quit();
|
|
17
|
+
else if (key.name === 'z')
|
|
18
|
+
context.suspend();
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
const view = context.activeView();
|
|
22
|
+
if (view) {
|
|
23
|
+
if (view.capturesInput?.()) {
|
|
24
|
+
view.handleKey?.(key);
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
if (view.handleKey?.(key))
|
|
28
|
+
return;
|
|
29
|
+
if (key.name === 'escape' || key.char === 'q')
|
|
30
|
+
context.closeView();
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
const char = key.char;
|
|
34
|
+
if (!char)
|
|
35
|
+
return;
|
|
36
|
+
const target = context.registry.byShortcut(char);
|
|
37
|
+
if (target) {
|
|
38
|
+
if (target.run)
|
|
39
|
+
void guarded(context.actions, () => target.run());
|
|
40
|
+
else
|
|
41
|
+
context.openView(target);
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
const action = MAIN_ACTIONS.find(item => item.key === char);
|
|
45
|
+
if (action) {
|
|
46
|
+
void guarded(context.actions, () => action.run(context.actions));
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
const unavailable = context.registry.unavailableLabel(char);
|
|
50
|
+
if (unavailable)
|
|
51
|
+
context.actions.report(`${unavailable} is not available yet`);
|
|
52
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/** Transient one-line feedback (action results) with an expiry. */
|
|
2
|
+
export interface Feedback {
|
|
3
|
+
/** Current message; undefined once expired. */
|
|
4
|
+
text: () => string | undefined;
|
|
5
|
+
/** Replaces the message and restarts the expiry; notifies. */
|
|
6
|
+
show: (text: string) => void;
|
|
7
|
+
/** Drops the message and its timer without notifying. */
|
|
8
|
+
dispose: () => void;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Creates the feedback holder.
|
|
12
|
+
* @param onChange Called when the message appears or expires (repaint).
|
|
13
|
+
*/
|
|
14
|
+
export declare function createFeedback(onChange: () => void): Feedback;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/** How long feedback stays visible. */
|
|
2
|
+
const FEEDBACK_MS = 4000;
|
|
3
|
+
/**
|
|
4
|
+
* Creates the feedback holder.
|
|
5
|
+
* @param onChange Called when the message appears or expires (repaint).
|
|
6
|
+
*/
|
|
7
|
+
export function createFeedback(onChange) {
|
|
8
|
+
let current;
|
|
9
|
+
let timer;
|
|
10
|
+
return {
|
|
11
|
+
text: () => current,
|
|
12
|
+
show(text) {
|
|
13
|
+
clearTimeout(timer);
|
|
14
|
+
current = text;
|
|
15
|
+
timer = setTimeout(() => {
|
|
16
|
+
current = undefined;
|
|
17
|
+
onChange();
|
|
18
|
+
}, FEEDBACK_MS);
|
|
19
|
+
timer.unref();
|
|
20
|
+
onChange();
|
|
21
|
+
},
|
|
22
|
+
dispose() {
|
|
23
|
+
clearTimeout(timer);
|
|
24
|
+
current = undefined;
|
|
25
|
+
},
|
|
26
|
+
};
|
|
27
|
+
}
|