stitchkit-tui 0.2.14 → 0.4.0
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/README.md +133 -4
- package/dist/App.d.ts +2 -2
- package/dist/App.d.ts.map +1 -1
- package/dist/app-2zw4ra7n.js +638 -0
- package/dist/build/index.d.ts +42 -0
- package/dist/build/index.d.ts.map +1 -0
- package/dist/build/index.js +174 -0
- package/dist/cli.js +11 -2195
- package/dist/config.d.ts +16 -16
- package/dist/config.d.ts.map +1 -1
- package/dist/core/chart.d.ts +82 -0
- package/dist/core/chart.d.ts.map +1 -0
- package/dist/core/collection.d.ts.map +1 -1
- package/dist/core/command-palette.d.ts.map +1 -1
- package/dist/core/focus.d.ts +33 -0
- package/dist/core/focus.d.ts.map +1 -0
- package/dist/core/form.d.ts +88 -0
- package/dist/core/form.d.ts.map +1 -0
- package/dist/core/index.d.ts +17 -0
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js +228 -358
- package/dist/core/json.d.ts +24 -0
- package/dist/core/json.d.ts.map +1 -0
- package/dist/core/key-hints.d.ts +28 -0
- package/dist/core/key-hints.d.ts.map +1 -0
- package/dist/core/keys.d.ts +32 -0
- package/dist/core/keys.d.ts.map +1 -0
- package/dist/core/labels.d.ts +48 -0
- package/dist/core/labels.d.ts.map +1 -0
- package/dist/core/list.d.ts +112 -0
- package/dist/core/list.d.ts.map +1 -0
- package/dist/core/meter.d.ts +61 -0
- package/dist/core/meter.d.ts.map +1 -0
- package/dist/core/navigation.d.ts +33 -0
- package/dist/core/navigation.d.ts.map +1 -0
- package/dist/core/notices.d.ts +39 -0
- package/dist/core/notices.d.ts.map +1 -0
- package/dist/core/progress.d.ts +53 -0
- package/dist/core/progress.d.ts.map +1 -0
- package/dist/core/search.d.ts +36 -0
- package/dist/core/search.d.ts.map +1 -0
- package/dist/core/table.d.ts +143 -0
- package/dist/core/table.d.ts.map +1 -0
- package/dist/core/tabs.d.ts +37 -0
- package/dist/core/tabs.d.ts.map +1 -0
- package/dist/core/text.d.ts +36 -0
- package/dist/core/text.d.ts.map +1 -0
- package/dist/core/theme.d.ts +38 -0
- package/dist/core/theme.d.ts.map +1 -0
- package/dist/gallery/app.d.ts +12 -0
- package/dist/gallery/app.d.ts.map +1 -0
- package/dist/gallery/catalogue.d.ts +11 -0
- package/dist/gallery/catalogue.d.ts.map +1 -0
- package/dist/gallery/main.d.ts +3 -0
- package/dist/gallery/main.d.ts.map +1 -0
- package/dist/gallery/main.js +16 -0
- package/dist/gallery/monitor-page.d.ts +8 -0
- package/dist/gallery/monitor-page.d.ts.map +1 -0
- package/dist/gallery/widgets-page.d.ts +7 -0
- package/dist/gallery/widgets-page.d.ts.map +1 -0
- package/dist/index-1qacnvfd.js +336 -0
- package/dist/index-51qwgt9f.js +172 -0
- package/dist/index-ahfc59de.js +495 -0
- package/dist/index-djrp2fey.js +1229 -0
- package/dist/index-ecpc5qkb.js +245 -0
- package/dist/index-m0dhvt6x.js +160 -0
- package/dist/index-p6nn7fsx.js +1599 -0
- package/dist/index-qhj2ne64.js +106 -0
- package/dist/index-wcbe2tnk.js +1237 -0
- package/dist/index.js +80 -2219
- package/dist/run-zchyzhwy.js +18 -0
- package/dist/run.d.ts.map +1 -1
- package/dist/status-line.d.ts +2 -2
- package/dist/status-line.d.ts.map +1 -1
- package/dist/testing/index.d.ts +50 -0
- package/dist/testing/index.d.ts.map +1 -0
- package/dist/testing/index.js +110 -0
- package/dist/transcript.d.ts +2 -1
- package/dist/transcript.d.ts.map +1 -1
- package/dist/widgets/app.d.ts +77 -0
- package/dist/widgets/app.d.ts.map +1 -0
- package/dist/widgets/chart.d.ts +32 -0
- package/dist/widgets/chart.d.ts.map +1 -0
- package/dist/widgets/context.d.ts +29 -0
- package/dist/widgets/context.d.ts.map +1 -0
- package/dist/widgets/feedback.d.ts +65 -0
- package/dist/widgets/feedback.d.ts.map +1 -0
- package/dist/widgets/form.d.ts +23 -0
- package/dist/widgets/form.d.ts.map +1 -0
- package/dist/widgets/index.d.ts +14 -0
- package/dist/widgets/index.d.ts.map +1 -0
- package/dist/widgets/index.js +89 -0
- package/dist/widgets/keyboard.d.ts +28 -0
- package/dist/widgets/keyboard.d.ts.map +1 -0
- package/dist/widgets/layout.d.ts +51 -0
- package/dist/widgets/layout.d.ts.map +1 -0
- package/dist/widgets/list.d.ts +32 -0
- package/dist/widgets/list.d.ts.map +1 -0
- package/dist/widgets/meter.d.ts +26 -0
- package/dist/widgets/meter.d.ts.map +1 -0
- package/dist/widgets/table.d.ts +25 -0
- package/dist/widgets/table.d.ts.map +1 -0
- package/dist/widgets/tabs.d.ts +13 -0
- package/dist/widgets/tabs.d.ts.map +1 -0
- package/dist/widgets/text-view.d.ts +24 -0
- package/dist/widgets/text-view.d.ts.map +1 -0
- package/dist/widgets/theme.d.ts +23 -0
- package/dist/widgets/theme.d.ts.map +1 -0
- package/package.json +29 -8
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// src/session.ts
|
|
3
|
+
import { chmod, mkdir, readdir, readFile, rename, unlink, writeFile } from "fs/promises";
|
|
4
|
+
import path from "path";
|
|
5
|
+
import { z } from "zod";
|
|
6
|
+
var MAX_REQUEST_BYTES = 65536;
|
|
7
|
+
var AgentTuiSessionDescriptorSchema2 = z.object({
|
|
8
|
+
schemaVersion: z.literal(1),
|
|
9
|
+
sessionId: z.string().min(8),
|
|
10
|
+
conversationId: z.string().min(1),
|
|
11
|
+
socketPath: z.string().min(1),
|
|
12
|
+
token: z.string().min(32),
|
|
13
|
+
pid: z.int().positive(),
|
|
14
|
+
startedAt: z.iso.datetime({ offset: true })
|
|
15
|
+
}).strict();
|
|
16
|
+
var AgentTuiSessionRequestSchema2 = z.discriminatedUnion("operation", [
|
|
17
|
+
z.object({ requestId: z.string().min(1), operation: z.literal("status") }).strict(),
|
|
18
|
+
z.object({
|
|
19
|
+
requestId: z.string().min(1),
|
|
20
|
+
operation: z.literal("submit"),
|
|
21
|
+
text: z.string().min(1),
|
|
22
|
+
idempotencyKey: z.string().min(1)
|
|
23
|
+
}).strict(),
|
|
24
|
+
z.object({
|
|
25
|
+
requestId: z.string().min(1),
|
|
26
|
+
operation: z.literal("interrupt"),
|
|
27
|
+
runId: z.string().min(1).optional()
|
|
28
|
+
}).strict()
|
|
29
|
+
]);
|
|
30
|
+
var AgentTuiSessionResponseSchema2 = z.discriminatedUnion("outcome", [
|
|
31
|
+
z.object({
|
|
32
|
+
requestId: z.string().min(1),
|
|
33
|
+
outcome: z.literal("ok"),
|
|
34
|
+
sessionId: z.string().min(1),
|
|
35
|
+
conversationId: z.string().min(1),
|
|
36
|
+
runId: z.string().min(1).optional()
|
|
37
|
+
}).strict(),
|
|
38
|
+
z.object({
|
|
39
|
+
requestId: z.string().min(1),
|
|
40
|
+
outcome: z.literal("error"),
|
|
41
|
+
error: z.object({ code: z.string().min(1), message: z.string().min(1) }).strict()
|
|
42
|
+
}).strict()
|
|
43
|
+
]);
|
|
44
|
+
function createAgentTuiSessionId2() {
|
|
45
|
+
return crypto.randomUUID().replaceAll("-", "").slice(0, 16);
|
|
46
|
+
}
|
|
47
|
+
function createAgentTuiConversationId2() {
|
|
48
|
+
return `conversation-${crypto.randomUUID()}`;
|
|
49
|
+
}
|
|
50
|
+
function sessionsDirectory(rootDirectory) {
|
|
51
|
+
return path.join(rootDirectory, ".stitchkit", "tui", "sessions");
|
|
52
|
+
}
|
|
53
|
+
function descriptorPath(rootDirectory, sessionId) {
|
|
54
|
+
return path.join(sessionsDirectory(rootDirectory), `${sessionId}.json`);
|
|
55
|
+
}
|
|
56
|
+
function processAlive(pid) {
|
|
57
|
+
try {
|
|
58
|
+
process.kill(pid, 0);
|
|
59
|
+
return true;
|
|
60
|
+
} catch (error) {
|
|
61
|
+
return error instanceof Error && "code" in error && error.code === "EPERM";
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
async function readDescriptor(rootDirectory, sessionId) {
|
|
65
|
+
const descriptor = AgentTuiSessionDescriptorSchema2.parse(JSON.parse(await readFile(descriptorPath(rootDirectory, sessionId), "utf8")));
|
|
66
|
+
const expectedSocket = path.join(sessionsDirectory(rootDirectory), `${sessionId}.sock`);
|
|
67
|
+
if (descriptor.sessionId !== sessionId || descriptor.socketPath !== expectedSocket) {
|
|
68
|
+
throw new Error("Agent TUI session descriptor identity does not match its path");
|
|
69
|
+
}
|
|
70
|
+
return descriptor;
|
|
71
|
+
}
|
|
72
|
+
async function probeDescriptor(descriptor, timeoutMs = 500) {
|
|
73
|
+
try {
|
|
74
|
+
const requestId = crypto.randomUUID();
|
|
75
|
+
const response = await fetch("http://localhost/request", {
|
|
76
|
+
method: "POST",
|
|
77
|
+
unix: descriptor.socketPath,
|
|
78
|
+
headers: {
|
|
79
|
+
Authorization: `Bearer ${descriptor.token}`,
|
|
80
|
+
"Content-Type": "application/json"
|
|
81
|
+
},
|
|
82
|
+
body: JSON.stringify({ requestId, operation: "status" }),
|
|
83
|
+
signal: AbortSignal.timeout(timeoutMs)
|
|
84
|
+
});
|
|
85
|
+
if (!response.ok)
|
|
86
|
+
return false;
|
|
87
|
+
const parsed = AgentTuiSessionResponseSchema2.safeParse(await response.json());
|
|
88
|
+
return parsed.success && parsed.data.outcome === "ok" && parsed.data.requestId === requestId && parsed.data.sessionId === descriptor.sessionId;
|
|
89
|
+
} catch {
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
async function writeDescriptor(rootDirectory, descriptor) {
|
|
94
|
+
const directory = sessionsDirectory(rootDirectory);
|
|
95
|
+
await mkdir(directory, { recursive: true, mode: 448 });
|
|
96
|
+
await chmod(directory, 448);
|
|
97
|
+
const filename = descriptorPath(rootDirectory, descriptor.sessionId);
|
|
98
|
+
const temporary = `${filename}.${crypto.randomUUID()}.tmp`;
|
|
99
|
+
await writeFile(temporary, `${JSON.stringify(descriptor)}
|
|
100
|
+
`, {
|
|
101
|
+
encoding: "utf8",
|
|
102
|
+
mode: 384,
|
|
103
|
+
flag: "wx"
|
|
104
|
+
});
|
|
105
|
+
await rename(temporary, filename);
|
|
106
|
+
await chmod(filename, 384);
|
|
107
|
+
}
|
|
108
|
+
async function removeDescriptor(rootDirectory, descriptor) {
|
|
109
|
+
const expectedSocket = path.join(sessionsDirectory(rootDirectory), `${descriptor.sessionId}.sock`);
|
|
110
|
+
if (descriptor.socketPath === expectedSocket) {
|
|
111
|
+
await unlink(expectedSocket).catch(() => {
|
|
112
|
+
return;
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
await unlink(descriptorPath(rootDirectory, descriptor.sessionId)).catch(() => {
|
|
116
|
+
return;
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
async function listAgentTuiSessions2(rootDirectory) {
|
|
120
|
+
const directory = sessionsDirectory(rootDirectory);
|
|
121
|
+
const names = await readdir(directory).catch((error) => {
|
|
122
|
+
if (error.code === "ENOENT")
|
|
123
|
+
return [];
|
|
124
|
+
throw error;
|
|
125
|
+
});
|
|
126
|
+
const descriptors = [];
|
|
127
|
+
for (const name of names.sort()) {
|
|
128
|
+
if (!name.endsWith(".json"))
|
|
129
|
+
continue;
|
|
130
|
+
try {
|
|
131
|
+
const sessionId = name.slice(0, -".json".length);
|
|
132
|
+
const descriptor = await readDescriptor(rootDirectory, sessionId);
|
|
133
|
+
if (!processAlive(descriptor.pid) || !await probeDescriptor(descriptor)) {
|
|
134
|
+
await removeDescriptor(rootDirectory, descriptor);
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
descriptors.push(descriptor);
|
|
138
|
+
} catch {
|
|
139
|
+
await unlink(path.join(directory, name)).catch(() => {
|
|
140
|
+
return;
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return descriptors;
|
|
145
|
+
}
|
|
146
|
+
async function startAgentTuiSessionHost2(input) {
|
|
147
|
+
for (const descriptor of await listAgentTuiSessions2(input.rootDirectory)) {
|
|
148
|
+
if (!processAlive(descriptor.pid))
|
|
149
|
+
await removeDescriptor(input.rootDirectory, descriptor);
|
|
150
|
+
}
|
|
151
|
+
const sessionId = input.sessionId ?? createAgentTuiSessionId2();
|
|
152
|
+
const socketPath = path.join(sessionsDirectory(input.rootDirectory), `${sessionId}.sock`);
|
|
153
|
+
const token = `${crypto.randomUUID()}${crypto.randomUUID()}`;
|
|
154
|
+
let conversationId = input.conversationId;
|
|
155
|
+
let descriptor = AgentTuiSessionDescriptorSchema2.parse({
|
|
156
|
+
schemaVersion: 1,
|
|
157
|
+
sessionId,
|
|
158
|
+
conversationId,
|
|
159
|
+
socketPath,
|
|
160
|
+
token,
|
|
161
|
+
pid: process.pid,
|
|
162
|
+
startedAt: new Date().toISOString()
|
|
163
|
+
});
|
|
164
|
+
await mkdir(path.dirname(socketPath), { recursive: true, mode: 448 });
|
|
165
|
+
let server;
|
|
166
|
+
try {
|
|
167
|
+
server = Bun.serve({
|
|
168
|
+
unix: socketPath,
|
|
169
|
+
async fetch(request) {
|
|
170
|
+
if (request.headers.get("authorization") !== `Bearer ${token}`) {
|
|
171
|
+
return Response.json({ error: "UNAUTHORIZED" }, { status: 401 });
|
|
172
|
+
}
|
|
173
|
+
const declaredLength = Number(request.headers.get("content-length") ?? "0");
|
|
174
|
+
if (declaredLength > MAX_REQUEST_BYTES) {
|
|
175
|
+
return Response.json({ error: "REQUEST_TOO_LARGE" }, { status: 413 });
|
|
176
|
+
}
|
|
177
|
+
const body = await request.text();
|
|
178
|
+
if (Buffer.byteLength(body) > MAX_REQUEST_BYTES) {
|
|
179
|
+
return Response.json({ error: "REQUEST_TOO_LARGE" }, { status: 413 });
|
|
180
|
+
}
|
|
181
|
+
try {
|
|
182
|
+
const parsed = AgentTuiSessionRequestSchema2.parse(JSON.parse(body));
|
|
183
|
+
return Response.json(await input.handle(parsed));
|
|
184
|
+
} catch {
|
|
185
|
+
return Response.json({ error: "INVALID_REQUEST" }, { status: 400 });
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
await chmod(socketPath, 384);
|
|
190
|
+
await writeDescriptor(input.rootDirectory, descriptor);
|
|
191
|
+
} catch (error) {
|
|
192
|
+
server?.stop(true);
|
|
193
|
+
await unlink(socketPath).catch(() => {
|
|
194
|
+
return;
|
|
195
|
+
});
|
|
196
|
+
await unlink(descriptorPath(input.rootDirectory, sessionId)).catch(() => {
|
|
197
|
+
return;
|
|
198
|
+
});
|
|
199
|
+
throw error;
|
|
200
|
+
}
|
|
201
|
+
return {
|
|
202
|
+
sessionId,
|
|
203
|
+
socketPath,
|
|
204
|
+
async setConversationId(nextConversationId) {
|
|
205
|
+
conversationId = nextConversationId;
|
|
206
|
+
descriptor = AgentTuiSessionDescriptorSchema2.parse({
|
|
207
|
+
...descriptor,
|
|
208
|
+
conversationId
|
|
209
|
+
});
|
|
210
|
+
await writeDescriptor(input.rootDirectory, descriptor);
|
|
211
|
+
},
|
|
212
|
+
async close() {
|
|
213
|
+
server.stop(true);
|
|
214
|
+
await unlink(socketPath).catch(() => {
|
|
215
|
+
return;
|
|
216
|
+
});
|
|
217
|
+
await unlink(descriptorPath(input.rootDirectory, sessionId)).catch(() => {
|
|
218
|
+
return;
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
async function createAgentTuiClient2(input) {
|
|
224
|
+
const descriptor = await readDescriptor(input.rootDirectory, input.sessionId);
|
|
225
|
+
const timeoutMs = input.timeoutMs ?? 5000;
|
|
226
|
+
return {
|
|
227
|
+
async request(request) {
|
|
228
|
+
const response = await fetch("http://localhost/request", {
|
|
229
|
+
method: "POST",
|
|
230
|
+
unix: descriptor.socketPath,
|
|
231
|
+
headers: {
|
|
232
|
+
Authorization: `Bearer ${descriptor.token}`,
|
|
233
|
+
"Content-Type": "application/json"
|
|
234
|
+
},
|
|
235
|
+
body: JSON.stringify(AgentTuiSessionRequestSchema2.parse(request)),
|
|
236
|
+
signal: AbortSignal.timeout(timeoutMs)
|
|
237
|
+
});
|
|
238
|
+
if (!response.ok)
|
|
239
|
+
throw new Error(`TUI session request failed with HTTP ${response.status}`);
|
|
240
|
+
return AgentTuiSessionResponseSchema2.parse(await response.json());
|
|
241
|
+
}
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
export { AgentTuiSessionDescriptorSchema2, AgentTuiSessionRequestSchema2, AgentTuiSessionResponseSchema2, createAgentTuiSessionId2, createAgentTuiConversationId2, listAgentTuiSessions2, startAgentTuiSessionHost2, createAgentTuiClient2 };
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
import {
|
|
3
|
+
terminalPalettes2,
|
|
4
|
+
resolveTerminalPalette2,
|
|
5
|
+
terminalLabelsEn2
|
|
6
|
+
} from "./index-ahfc59de.js";
|
|
7
|
+
|
|
8
|
+
// src/widgets/context.tsx
|
|
9
|
+
import { createContext, useContext, useMemo } from "react";
|
|
10
|
+
import { jsx } from "@opentui/react/jsx-runtime";
|
|
11
|
+
var LabelsContext = createContext(terminalLabelsEn2);
|
|
12
|
+
function TerminalLabelsProvider2({
|
|
13
|
+
labels,
|
|
14
|
+
children
|
|
15
|
+
}) {
|
|
16
|
+
return /* @__PURE__ */ jsx(LabelsContext.Provider, {
|
|
17
|
+
value: labels,
|
|
18
|
+
children
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
function useTerminalLabels2() {
|
|
22
|
+
return useContext(LabelsContext);
|
|
23
|
+
}
|
|
24
|
+
var FocusContext = createContext(null);
|
|
25
|
+
function TerminalFocusProvider2({
|
|
26
|
+
state,
|
|
27
|
+
dispatch,
|
|
28
|
+
children
|
|
29
|
+
}) {
|
|
30
|
+
const value = useMemo(() => ({ state, dispatch }), [state, dispatch]);
|
|
31
|
+
return /* @__PURE__ */ jsx(FocusContext.Provider, {
|
|
32
|
+
value,
|
|
33
|
+
children
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
function useTerminalFocusContext2() {
|
|
37
|
+
return useContext(FocusContext);
|
|
38
|
+
}
|
|
39
|
+
function useTerminalFocused2(focusId, focused) {
|
|
40
|
+
const context = useContext(FocusContext);
|
|
41
|
+
if (focused !== undefined)
|
|
42
|
+
return focused;
|
|
43
|
+
if (focusId === undefined || context === null)
|
|
44
|
+
return true;
|
|
45
|
+
return context.state.focused === focusId;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// src/widgets/keyboard.ts
|
|
49
|
+
import { useRenderer } from "@opentui/react";
|
|
50
|
+
import { useEffect, useRef } from "react";
|
|
51
|
+
var TerminalKeyPriority2 = Object.freeze({
|
|
52
|
+
input: 300,
|
|
53
|
+
layer: 200,
|
|
54
|
+
widget: 100,
|
|
55
|
+
global: 0
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
class KeyRouter {
|
|
59
|
+
registrations = new Set;
|
|
60
|
+
next = 0;
|
|
61
|
+
constructor(renderer) {
|
|
62
|
+
renderer.keyInput.on("keypress", (key) => this.route(key));
|
|
63
|
+
}
|
|
64
|
+
register(priority, handler) {
|
|
65
|
+
const registration = { order: this.next, priority, handler };
|
|
66
|
+
this.next += 1;
|
|
67
|
+
this.registrations.add(registration);
|
|
68
|
+
return registration;
|
|
69
|
+
}
|
|
70
|
+
unregister(registration) {
|
|
71
|
+
this.registrations.delete(registration);
|
|
72
|
+
}
|
|
73
|
+
route(key) {
|
|
74
|
+
if (key.propagationStopped)
|
|
75
|
+
return;
|
|
76
|
+
const ordered = [...this.registrations].sort((a, b) => b.priority - a.priority || a.order - b.order);
|
|
77
|
+
for (const registration of ordered) {
|
|
78
|
+
if (registration.handler(key)) {
|
|
79
|
+
key.preventDefault();
|
|
80
|
+
key.stopPropagation();
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
var routers = new WeakMap;
|
|
87
|
+
function routerFor(renderer) {
|
|
88
|
+
const existing = routers.get(renderer);
|
|
89
|
+
if (existing)
|
|
90
|
+
return existing;
|
|
91
|
+
const created = new KeyRouter(renderer);
|
|
92
|
+
routers.set(renderer, created);
|
|
93
|
+
return created;
|
|
94
|
+
}
|
|
95
|
+
function useTerminalKeyboard2(handler, options = {}) {
|
|
96
|
+
const renderer = useRenderer();
|
|
97
|
+
const priority = options.priority ?? TerminalKeyPriority2.widget;
|
|
98
|
+
const active = options.active ?? true;
|
|
99
|
+
const latest = useRef(handler);
|
|
100
|
+
latest.current = handler;
|
|
101
|
+
const live = useRef(active);
|
|
102
|
+
live.current = active;
|
|
103
|
+
useEffect(() => {
|
|
104
|
+
const router = routerFor(renderer);
|
|
105
|
+
const entry = router.register(priority, (key) => live.current && latest.current(key));
|
|
106
|
+
return () => router.unregister(entry);
|
|
107
|
+
}, [renderer, priority]);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// src/widgets/theme.tsx
|
|
111
|
+
import { TextAttributes } from "@opentui/core";
|
|
112
|
+
import { useRenderer as useRenderer2 } from "@opentui/react";
|
|
113
|
+
import {
|
|
114
|
+
createContext as createContext2,
|
|
115
|
+
useContext as useContext2,
|
|
116
|
+
useEffect as useEffect2,
|
|
117
|
+
useMemo as useMemo2,
|
|
118
|
+
useState
|
|
119
|
+
} from "react";
|
|
120
|
+
import { jsx as jsx2 } from "@opentui/react/jsx-runtime";
|
|
121
|
+
function terminalToneAttributes2(tone) {
|
|
122
|
+
if (tone === "danger" || tone === "warning")
|
|
123
|
+
return TextAttributes.BOLD;
|
|
124
|
+
if (tone === "muted")
|
|
125
|
+
return TextAttributes.DIM;
|
|
126
|
+
return TextAttributes.NONE;
|
|
127
|
+
}
|
|
128
|
+
var PaletteContext = createContext2(terminalPalettes2.dark);
|
|
129
|
+
function useTerminalPalette2() {
|
|
130
|
+
return useContext2(PaletteContext);
|
|
131
|
+
}
|
|
132
|
+
function useTerminalThemeMode2(mode) {
|
|
133
|
+
const renderer = useRenderer2();
|
|
134
|
+
const [reported, setReported] = useState(renderer.themeMode);
|
|
135
|
+
useEffect2(() => {
|
|
136
|
+
if (mode !== "auto")
|
|
137
|
+
return;
|
|
138
|
+
const update = (next) => setReported(next);
|
|
139
|
+
renderer.on("theme_mode", update);
|
|
140
|
+
setReported(renderer.themeMode);
|
|
141
|
+
return () => {
|
|
142
|
+
renderer.off("theme_mode", update);
|
|
143
|
+
};
|
|
144
|
+
}, [mode, renderer]);
|
|
145
|
+
return mode === "auto" ? reported ?? "dark" : mode;
|
|
146
|
+
}
|
|
147
|
+
function TerminalThemeProvider2({
|
|
148
|
+
mode = "auto",
|
|
149
|
+
palette,
|
|
150
|
+
children
|
|
151
|
+
}) {
|
|
152
|
+
const resolved = useTerminalThemeMode2(mode);
|
|
153
|
+
const value = useMemo2(() => resolveTerminalPalette2(resolved, palette), [resolved, palette]);
|
|
154
|
+
return /* @__PURE__ */ jsx2(PaletteContext.Provider, {
|
|
155
|
+
value,
|
|
156
|
+
children
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export { TerminalLabelsProvider2, useTerminalLabels2, TerminalFocusProvider2, useTerminalFocusContext2, useTerminalFocused2, TerminalKeyPriority2, useTerminalKeyboard2, terminalToneAttributes2, useTerminalPalette2, useTerminalThemeMode2, TerminalThemeProvider2 };
|