stitchkit-tui 0.3.0 → 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.
Files changed (39) hide show
  1. package/README.md +61 -16
  2. package/dist/{app-mqzzrp1t.js → app-2zw4ra7n.js} +36 -16
  3. package/dist/build/index.d.ts +42 -0
  4. package/dist/build/index.d.ts.map +1 -0
  5. package/dist/build/index.js +174 -0
  6. package/dist/cli.js +1 -1
  7. package/dist/config.d.ts +8 -0
  8. package/dist/config.d.ts.map +1 -1
  9. package/dist/core/index.js +27 -25
  10. package/dist/core/labels.d.ts +4 -0
  11. package/dist/core/labels.d.ts.map +1 -1
  12. package/dist/core/table.d.ts +18 -1
  13. package/dist/core/table.d.ts.map +1 -1
  14. package/dist/gallery/main.js +1 -1
  15. package/dist/gallery/monitor-page.d.ts.map +1 -1
  16. package/dist/{index-kz62sdr9.js → index-1qacnvfd.js} +1 -1
  17. package/dist/index-51qwgt9f.js +172 -0
  18. package/dist/index-ahfc59de.js +495 -0
  19. package/dist/{index-z4c2ehg4.js → index-djrp2fey.js} +107 -583
  20. package/dist/index-m0dhvt6x.js +160 -0
  21. package/dist/{index-fxx9ktw7.js → index-p6nn7fsx.js} +35 -39
  22. package/dist/{index-251fexwh.js → index-qhj2ne64.js} +1 -1
  23. package/dist/{index-54sqbm3r.js → index-wcbe2tnk.js} +168 -285
  24. package/dist/index.js +7 -4
  25. package/dist/{run-e1kdpzna.js → run-zchyzhwy.js} +7 -4
  26. package/dist/run.d.ts.map +1 -1
  27. package/dist/testing/index.d.ts +50 -0
  28. package/dist/testing/index.d.ts.map +1 -0
  29. package/dist/testing/index.js +110 -0
  30. package/dist/widgets/app.d.ts +77 -0
  31. package/dist/widgets/app.d.ts.map +1 -0
  32. package/dist/widgets/form.d.ts.map +1 -1
  33. package/dist/widgets/index.d.ts +1 -0
  34. package/dist/widgets/index.d.ts.map +1 -1
  35. package/dist/widgets/index.js +20 -7
  36. package/dist/widgets/table.d.ts.map +1 -1
  37. package/dist/widgets/theme.d.ts +7 -1
  38. package/dist/widgets/theme.d.ts.map +1 -1
  39. package/package.json +12 -4
@@ -6,7 +6,7 @@ process.env.NODE_ENV ??= "production";
6
6
  var { createCliRenderer } = await import("@opentui/core");
7
7
  var { createRoot } = await import("@opentui/react");
8
8
  var { createElement } = await import("react");
9
- var { TerminalGallery } = await import("../app-mqzzrp1t.js");
9
+ var { TerminalGallery } = await import("../app-2zw4ra7n.js");
10
10
  var renderer = await createCliRenderer({ exitOnCtrlC: false });
11
11
  createRoot(renderer).render(createElement(TerminalGallery, {
12
12
  onExit: () => {
@@ -1 +1 @@
1
- {"version":3,"file":"monitor-page.d.ts","sourceRoot":"","sources":["../../src/gallery/monitor-page.tsx"],"names":[],"mappings":"AA8DA,oDAAoD;AACpD,wBAAgB,WAAW,CAAC,EAC1B,KAAK,EACL,MAAM,EACN,MAAM,EACN,KAAK,GACN,EAAE;IACD,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,OAAO,CAAC;IAChB,KAAK,EAAE,MAAM,MAAM,CAAC;CACrB,6BAyEA"}
1
+ {"version":3,"file":"monitor-page.d.ts","sourceRoot":"","sources":["../../src/gallery/monitor-page.tsx"],"names":[],"mappings":"AAmFA,oDAAoD;AACpD,wBAAgB,WAAW,CAAC,EAC1B,KAAK,EACL,MAAM,EACN,MAAM,EACN,KAAK,GACN,EAAE;IACD,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,OAAO,CAAC;IAChB,KAAK,EAAE,MAAM,MAAM,CAAC;CACrB,6BAyEA"}
@@ -1,7 +1,7 @@
1
1
  // @bun
2
2
  import {
3
3
  searchTerminalItems2
4
- } from "./index-z4c2ehg4.js";
4
+ } from "./index-djrp2fey.js";
5
5
 
6
6
  // src/core/command-palette.ts
7
7
  import { z } from "zod";
@@ -0,0 +1,172 @@
1
+ // @bun
2
+ import {
3
+ sanitizeTerminalText2,
4
+ wrapTerminalText2,
5
+ terminalLabelsEn2
6
+ } from "./index-ahfc59de.js";
7
+ import {
8
+ TerminalLabelsProvider2,
9
+ useTerminalLabels2,
10
+ TerminalKeyPriority2,
11
+ useTerminalKeyboard2,
12
+ useTerminalPalette2,
13
+ TerminalThemeProvider2
14
+ } from "./index-m0dhvt6x.js";
15
+
16
+ // src/widgets/app.tsx
17
+ import { constants } from "os";
18
+ import { createCliRenderer } from "@opentui/core";
19
+ import { createRoot, useTerminalDimensions } from "@opentui/react";
20
+ import { Component } from "react";
21
+ import { jsx, jsxs, Fragment } from "@opentui/react/jsx-runtime";
22
+ function TerminalErrorScreen({ error }) {
23
+ const palette = useTerminalPalette2();
24
+ const labels = useTerminalLabels2();
25
+ const dimensions = useTerminalDimensions();
26
+ const width = Math.max(20, dimensions.width - 4);
27
+ const detail = error instanceof Error ? [`${error.name}: ${error.message}`, ...error.stack?.split(`
28
+ `).slice(1, 9) ?? []] : [String(error)];
29
+ const lines = detail.flatMap((line) => wrapTerminalText2(sanitizeTerminalText2(line), width)).slice(0, Math.max(1, dimensions.height - 4));
30
+ return /* @__PURE__ */ jsxs("box", {
31
+ style: { flexDirection: "column", padding: 1, backgroundColor: palette.canvas },
32
+ children: [
33
+ /* @__PURE__ */ jsx("text", {
34
+ fg: palette.danger,
35
+ style: { flexShrink: 0 },
36
+ children: labels.crashed
37
+ }),
38
+ lines.map((line, index) => /* @__PURE__ */ jsx("text", {
39
+ fg: index === 0 ? palette.primary : palette.muted,
40
+ wrapMode: "none",
41
+ style: { flexShrink: 0 },
42
+ children: line
43
+ }, index))
44
+ ]
45
+ });
46
+ }
47
+
48
+ class TerminalErrorBoundary2 extends Component {
49
+ state = { error: undefined };
50
+ static getDerivedStateFromError(error) {
51
+ return { error: error ?? new Error("Unknown render error") };
52
+ }
53
+ componentDidCatch(error, info) {
54
+ this.props.onError?.(error, info);
55
+ }
56
+ render() {
57
+ return this.state.error === undefined ? this.props.children : /* @__PURE__ */ jsx(TerminalErrorScreen, {
58
+ error: this.state.error
59
+ });
60
+ }
61
+ }
62
+ var TERMINAL_EXIT_SIGNALS2 = [
63
+ "SIGINT",
64
+ "SIGTERM",
65
+ "SIGHUP",
66
+ "SIGQUIT",
67
+ "SIGABRT",
68
+ "SIGPIPE",
69
+ "SIGBUS",
70
+ "SIGBREAK"
71
+ ];
72
+ function exitCode(signal) {
73
+ return 128 + (constants.signals[signal] ?? 0);
74
+ }
75
+ function CtrlCExit({ onExit }) {
76
+ useTerminalKeyboard2((key) => {
77
+ if (!key.ctrl || key.name !== "c")
78
+ return false;
79
+ onExit();
80
+ return true;
81
+ }, { priority: TerminalKeyPriority2.global - 1 });
82
+ return null;
83
+ }
84
+ function FactoryScreen({
85
+ factory,
86
+ app
87
+ }) {
88
+ return /* @__PURE__ */ jsx(Fragment, {
89
+ children: factory(app)
90
+ });
91
+ }
92
+ async function runTerminalApp2(screen, options = {}) {
93
+ const renderer = await createCliRenderer({
94
+ ...options.renderer,
95
+ exitOnCtrlC: false,
96
+ exitSignals: []
97
+ });
98
+ const signals = (options.exitSignals ?? TERMINAL_EXIT_SIGNALS2).filter((signal) => signal !== "SIGBREAK" || process.platform === "win32");
99
+ let resolveClosed = () => {
100
+ return;
101
+ };
102
+ const closed = new Promise((resolve) => {
103
+ resolveClosed = resolve;
104
+ });
105
+ let root;
106
+ let closing;
107
+ const listeners = new Map;
108
+ const close = () => {
109
+ closing ??= (async () => {
110
+ for (const [signal, listener] of listeners)
111
+ process.off(signal, listener);
112
+ try {
113
+ root?.unmount();
114
+ } finally {
115
+ renderer.destroy();
116
+ try {
117
+ await options.onClose?.();
118
+ } finally {
119
+ resolveClosed();
120
+ }
121
+ }
122
+ })();
123
+ return closing;
124
+ };
125
+ const exitWith = (code) => {
126
+ const exit = () => process.exit(code);
127
+ setTimeout(exit, options.closeTimeoutMs ?? 1000).unref();
128
+ close().then(exit, (error) => {
129
+ console.error("runTerminalApp: onClose failed while exiting", error);
130
+ exit();
131
+ });
132
+ };
133
+ for (const signal of signals) {
134
+ const listener = () => exitWith(exitCode(signal));
135
+ listeners.set(signal, listener);
136
+ process.on(signal, listener);
137
+ }
138
+ try {
139
+ const mode = options.themeMode ?? "auto";
140
+ if (mode === "auto")
141
+ await renderer.waitForThemeMode(options.themeTimeoutMs ?? 250);
142
+ if (closing)
143
+ return { renderer, close, closed };
144
+ root = createRoot(renderer);
145
+ const app = { close };
146
+ root.render(/* @__PURE__ */ jsx(TerminalThemeProvider2, {
147
+ mode,
148
+ ...options.palette && { palette: options.palette },
149
+ children: /* @__PURE__ */ jsxs(TerminalLabelsProvider2, {
150
+ labels: options.labels ?? terminalLabelsEn2,
151
+ children: [
152
+ options.exitOnCtrlC === false ? null : /* @__PURE__ */ jsx(CtrlCExit, {
153
+ onExit: () => exitWith(0)
154
+ }),
155
+ /* @__PURE__ */ jsx(TerminalErrorBoundary2, {
156
+ onError: options.onError,
157
+ children: typeof screen === "function" ? /* @__PURE__ */ jsx(FactoryScreen, {
158
+ factory: screen,
159
+ app
160
+ }) : screen
161
+ })
162
+ ]
163
+ })
164
+ }));
165
+ } catch (error) {
166
+ await close();
167
+ throw error;
168
+ }
169
+ return { renderer, close, closed };
170
+ }
171
+
172
+ export { TerminalErrorBoundary2, TERMINAL_EXIT_SIGNALS2, runTerminalApp2 };
@@ -0,0 +1,495 @@
1
+ // @bun
2
+ // src/core/theme.ts
3
+ import { z } from "zod";
4
+ var TerminalToneSchema2 = z.enum([
5
+ "primary",
6
+ "muted",
7
+ "accent",
8
+ "success",
9
+ "warning",
10
+ "danger"
11
+ ]);
12
+ var TerminalThemeModeSchema2 = z.enum(["dark", "light"]);
13
+ var ColorSchema = z.string().min(1);
14
+ var TerminalPaletteSchema2 = z.object({
15
+ canvas: ColorSchema,
16
+ panel: ColorSchema,
17
+ panelRaised: ColorSchema,
18
+ border: ColorSchema,
19
+ borderActive: ColorSchema,
20
+ primary: ColorSchema,
21
+ muted: ColorSchema,
22
+ accent: ColorSchema,
23
+ success: ColorSchema,
24
+ warning: ColorSchema,
25
+ danger: ColorSchema
26
+ }).strict();
27
+ var terminalPalettes2 = Object.freeze({
28
+ dark: Object.freeze({
29
+ canvas: "#111111",
30
+ panel: "#1e2129",
31
+ panelRaised: "#303441",
32
+ border: "#454a57",
33
+ borderActive: "#d9dde7",
34
+ primary: "#e6ebf5",
35
+ muted: "#828aa0",
36
+ accent: "#e58a66",
37
+ success: "#64df98",
38
+ warning: "#ffd04f",
39
+ danger: "#ff6b78"
40
+ }),
41
+ light: Object.freeze({
42
+ canvas: "#fafafa",
43
+ panel: "#eef0f4",
44
+ panelRaised: "#dfe3ea",
45
+ border: "#b4bac6",
46
+ borderActive: "#2b303b",
47
+ primary: "#1d2129",
48
+ muted: "#646b7a",
49
+ accent: "#b5502b",
50
+ success: "#1a7f45",
51
+ warning: "#946200",
52
+ danger: "#c4283a"
53
+ })
54
+ });
55
+ function resolveTerminalPalette2(mode, overrides = {}) {
56
+ return TerminalPaletteSchema2.parse({
57
+ ...terminalPalettes2[TerminalThemeModeSchema2.parse(mode)],
58
+ ...overrides
59
+ });
60
+ }
61
+
62
+ // src/core/form.ts
63
+ import { z as z2 } from "zod";
64
+
65
+ // src/core/text.ts
66
+ var STRING_SEQUENCE = /(?:\u001b[\]PX^_]|[\u0090\u0098\u009d\u009e\u009f])[\s\S]*?(?:\u0007|\u001b\\|\u009c|$)/g;
67
+ var CSI = /(?:\u001b\[|\u009b)[0-?]*[ -/]*[@-~]?/g;
68
+ var ESCAPE = /\u001b[ -/]*[0-~]?/g;
69
+ var CONTROL = /[\u0000-\u0008\u000b-\u001f\u007f-\u009f]/g;
70
+ function sanitizeTerminalText2(text, tabWidth = 2) {
71
+ return text.replace(STRING_SEQUENCE, "").replace(CSI, "").replace(ESCAPE, "").replace(/\r\n?/g, `
72
+ `).replace(/\t/g, " ".repeat(tabWidth)).replace(CONTROL, "");
73
+ }
74
+ var WIDE = [
75
+ [4352, 4447],
76
+ [11904, 12350],
77
+ [12353, 13311],
78
+ [13312, 19903],
79
+ [19968, 40959],
80
+ [40960, 42191],
81
+ [44032, 55203],
82
+ [63744, 64255],
83
+ [65072, 65103],
84
+ [65280, 65376],
85
+ [65504, 65510],
86
+ [127744, 128591],
87
+ [129280, 129535],
88
+ [131072, 262141]
89
+ ];
90
+ var segmenter = new Intl.Segmenter(undefined, { granularity: "grapheme" });
91
+ function terminalGraphemes2(text) {
92
+ return Array.from(segmenter.segment(text), ({ segment }) => segment);
93
+ }
94
+ function terminalGraphemeWidth2(grapheme) {
95
+ if (/^[\p{M}\p{Cf}]+$/u.test(grapheme))
96
+ return 0;
97
+ if (/\p{Emoji_Presentation}/u.test(grapheme) || grapheme.includes("\uFE0F") && /\p{Extended_Pictographic}/u.test(grapheme))
98
+ return 2;
99
+ const code = grapheme.codePointAt(0) ?? 0;
100
+ for (const [start, end] of WIDE)
101
+ if (code >= start && code <= end)
102
+ return 2;
103
+ return 1;
104
+ }
105
+ function terminalTextWidth2(text) {
106
+ let width = 0;
107
+ for (const grapheme of terminalGraphemes2(text))
108
+ width += terminalGraphemeWidth2(grapheme);
109
+ return width;
110
+ }
111
+ function truncateTerminalText2(text, width) {
112
+ if (width <= 0)
113
+ return "";
114
+ if (terminalTextWidth2(text) <= width)
115
+ return text;
116
+ let used = 0;
117
+ let result = "";
118
+ for (const grapheme of terminalGraphemes2(text)) {
119
+ const cells = terminalGraphemeWidth2(grapheme);
120
+ if (used + cells > width - 1)
121
+ break;
122
+ result += grapheme;
123
+ used += cells;
124
+ }
125
+ return `${result}\u2026`;
126
+ }
127
+ function fitTerminalText2(text, width, align = "left") {
128
+ const cut = truncateTerminalText2(text, width);
129
+ const pad = Math.max(0, width - terminalTextWidth2(cut));
130
+ if (align === "right")
131
+ return " ".repeat(pad) + cut;
132
+ if (align === "center") {
133
+ const before = Math.floor(pad / 2);
134
+ return " ".repeat(before) + cut + " ".repeat(pad - before);
135
+ }
136
+ return cut + " ".repeat(pad);
137
+ }
138
+ function wrapTerminalText2(text, width) {
139
+ if (width <= 0)
140
+ return [];
141
+ const lines = [];
142
+ for (const paragraph of text.split(`
143
+ `)) {
144
+ const indent = /^ */.exec(paragraph)?.[0] ?? "";
145
+ const words = paragraph.slice(indent.length).split(/ +/).filter(Boolean);
146
+ if (words.length === 0) {
147
+ lines.push("");
148
+ continue;
149
+ }
150
+ const first = indent.length < width ? indent : "";
151
+ const hang = indent.length < width / 2 ? indent : "";
152
+ let line = first;
153
+ let start = first.length;
154
+ let used = start;
155
+ const flush = () => {
156
+ lines.push(line.trimEnd());
157
+ line = hang;
158
+ start = hang.length;
159
+ used = start;
160
+ };
161
+ for (const word of words) {
162
+ const cells = terminalTextWidth2(word);
163
+ const separator = used > start ? 1 : 0;
164
+ if (used + separator + cells <= width) {
165
+ line += (separator ? " " : "") + word;
166
+ used += separator + cells;
167
+ continue;
168
+ }
169
+ if (used > start)
170
+ flush();
171
+ if (cells <= width - used) {
172
+ line += word;
173
+ used += cells;
174
+ continue;
175
+ }
176
+ for (const grapheme of terminalGraphemes2(word)) {
177
+ const graphemeCells = terminalGraphemeWidth2(grapheme);
178
+ if (used + graphemeCells > width && used > start)
179
+ flush();
180
+ line += grapheme;
181
+ used += graphemeCells;
182
+ }
183
+ }
184
+ lines.push(line.trimEnd());
185
+ }
186
+ return lines;
187
+ }
188
+ function terminalTextEdit2(key) {
189
+ if (key.name === "escape")
190
+ return { kind: "clear" };
191
+ if (key.name === "return" || key.name === "enter")
192
+ return { kind: "commit" };
193
+ if (key.name === "backspace")
194
+ return { kind: "erase" };
195
+ const text = key.sequence ?? "";
196
+ const control = Array.from(text).some((char) => {
197
+ const code = char.codePointAt(0) ?? 0;
198
+ return code < 32 || code === 127;
199
+ });
200
+ if (key.ctrl || key.meta || text.length === 0 || control)
201
+ return null;
202
+ return { kind: "insert", text };
203
+ }
204
+ function applyTerminalTextEdit2(text, edit) {
205
+ if (edit.kind === "insert")
206
+ return text + edit.text;
207
+ if (edit.kind === "erase")
208
+ return terminalGraphemes2(text).slice(0, -1).join("");
209
+ if (edit.kind === "clear")
210
+ return "";
211
+ return text;
212
+ }
213
+
214
+ // src/core/form.ts
215
+ var TerminalFormFieldSchema2 = z2.object({
216
+ name: z2.string().min(1),
217
+ label: z2.string().min(1),
218
+ kind: z2.enum(["text", "secret", "number", "integer", "boolean", "choice", "json"]),
219
+ required: z2.boolean().default(false),
220
+ choices: z2.array(z2.union([z2.string(), z2.number(), z2.boolean()])).optional(),
221
+ defaultValue: z2.unknown().optional(),
222
+ description: z2.string().optional(),
223
+ placeholder: z2.string().optional()
224
+ }).strict().refine((field) => field.kind !== "choice" || (field.choices?.length ?? 0) > 0, {
225
+ message: "A choice field needs at least one choice"
226
+ });
227
+ var terminalFormMessages2 = {
228
+ required: "Required",
229
+ number: "Expected a number",
230
+ integer: "Expected a whole number",
231
+ choice: (choices) => `Choose one of: ${choices.join(", ")}`,
232
+ json: (reason) => `Invalid JSON: ${reason}`
233
+ };
234
+ function draftOf(field) {
235
+ const value = field.defaultValue;
236
+ if (value === undefined || value === null)
237
+ return "";
238
+ if (field.kind === "json")
239
+ return JSON.stringify(value);
240
+ return String(value);
241
+ }
242
+ function readField(field, draft, messages) {
243
+ const text = field.kind === "text" || field.kind === "secret" ? draft : draft.trim();
244
+ if (text === "")
245
+ return field.required ? { error: messages.required } : {};
246
+ switch (field.kind) {
247
+ case "text":
248
+ case "secret":
249
+ return { value: text };
250
+ case "number":
251
+ case "integer": {
252
+ if (!/^[+-]?\d+(\.\d+)?([eE][+-]?\d+)?$/.test(text))
253
+ return { error: messages.number };
254
+ const value = Number(text);
255
+ if (!Number.isFinite(value))
256
+ return { error: messages.number };
257
+ if (field.kind === "integer" && !Number.isInteger(value))
258
+ return { error: messages.integer };
259
+ return { value };
260
+ }
261
+ case "boolean":
262
+ return text === "true" || text === "false" ? { value: text === "true" } : { error: messages.choice(["true", "false"]) };
263
+ case "choice": {
264
+ const choice = field.choices?.find((candidate) => String(candidate) === text);
265
+ return choice === undefined ? { error: messages.choice((field.choices ?? []).map(String)) } : { value: choice };
266
+ }
267
+ case "json":
268
+ try {
269
+ return { value: JSON.parse(text) };
270
+ } catch (error) {
271
+ return {
272
+ error: messages.json(error instanceof Error ? error.message : String(error))
273
+ };
274
+ }
275
+ }
276
+ }
277
+ function createTerminalForm2(fields) {
278
+ const parsed = z2.array(TerminalFormFieldSchema2).parse(fields);
279
+ const names = new Set;
280
+ for (const field of parsed) {
281
+ if (names.has(field.name))
282
+ throw new Error(`Terminal form field ${field.name} is declared twice`);
283
+ names.add(field.name);
284
+ }
285
+ return {
286
+ fields: parsed,
287
+ drafts: Object.fromEntries(parsed.map((field) => [field.name, draftOf(field)])),
288
+ errors: {},
289
+ focus: 0,
290
+ submitted: false,
291
+ accepted: null
292
+ };
293
+ }
294
+ function fieldNamed(state, name) {
295
+ const field = name === undefined ? state.fields[state.focus] : state.fields.find((f) => f.name === name);
296
+ if (!field)
297
+ throw new Error(`Unknown terminal form field: ${name ?? `#${state.focus}`}`);
298
+ return field;
299
+ }
300
+ function readTerminalForm2(state, messages = terminalFormMessages2) {
301
+ const values = {};
302
+ const errors = {};
303
+ for (const field of state.fields) {
304
+ const read = readField(field, state.drafts[field.name] ?? "", messages);
305
+ if (read.error !== undefined)
306
+ errors[field.name] = read.error;
307
+ else if ("value" in read)
308
+ values[field.name] = read.value;
309
+ }
310
+ return Object.keys(errors).length > 0 ? { ok: false, errors } : { ok: true, values };
311
+ }
312
+ function reduceTerminalForm2(state, action, messages = terminalFormMessages2) {
313
+ const validate = (next, name) => {
314
+ const field = fieldNamed(next, name);
315
+ const read = readField(field, next.drafts[name] ?? "", messages);
316
+ const errors = { ...next.errors };
317
+ if (read.error !== undefined)
318
+ errors[name] = read.error;
319
+ else
320
+ delete errors[name];
321
+ return { ...next, errors };
322
+ };
323
+ switch (action.type) {
324
+ case "focus":
325
+ case "move": {
326
+ if (state.fields.length === 0)
327
+ return state;
328
+ const target = action.type === "focus" ? state.fields.findIndex(({ name }) => name === action.name) : Math.min(Math.max(0, state.focus + action.delta), state.fields.length - 1);
329
+ if (target < 0)
330
+ throw new Error(`Unknown terminal form field: ${action.type === "focus" ? action.name : ""}`);
331
+ const left = state.fields[state.focus];
332
+ const checked = left && target !== state.focus ? validate(state, left.name) : state;
333
+ return { ...checked, focus: target };
334
+ }
335
+ case "set":
336
+ case "edit": {
337
+ const field = fieldNamed(state, action.name);
338
+ const text = action.type === "set" ? action.text : applyTerminalTextEdit2(state.drafts[field.name] ?? "", action.edit);
339
+ const next = { ...state, drafts: { ...state.drafts, [field.name]: text } };
340
+ return state.errors[field.name] !== undefined || state.submitted ? validate(next, field.name) : next;
341
+ }
342
+ case "cycle": {
343
+ const field = fieldNamed(state, action.name);
344
+ const options = field.kind === "boolean" ? ["true", "false"] : field.kind === "choice" ? (field.choices ?? []).map(String) : null;
345
+ if (options === null)
346
+ return state;
347
+ const all = field.required ? options : ["", ...options];
348
+ const index = all.indexOf(state.drafts[field.name] ?? "");
349
+ const next = all[((index + action.delta) % all.length + all.length) % all.length] ?? "";
350
+ return validate({ ...state, drafts: { ...state.drafts, [field.name]: next } }, field.name);
351
+ }
352
+ case "clear": {
353
+ const field = fieldNamed(state, action.name);
354
+ const next = { ...state, drafts: { ...state.drafts, [field.name]: "" } };
355
+ return state.errors[field.name] !== undefined || state.submitted ? validate(next, field.name) : next;
356
+ }
357
+ case "submit": {
358
+ const result = readTerminalForm2(state, messages);
359
+ if (result.ok) {
360
+ const serial = (state.accepted?.serial ?? 0) + 1;
361
+ return {
362
+ ...state,
363
+ errors: {},
364
+ submitted: true,
365
+ accepted: { serial, values: result.values }
366
+ };
367
+ }
368
+ const first = state.fields.findIndex(({ name }) => result.errors[name] !== undefined);
369
+ return {
370
+ ...state,
371
+ errors: result.errors,
372
+ submitted: true,
373
+ focus: first < 0 ? state.focus : first
374
+ };
375
+ }
376
+ }
377
+ }
378
+ var JsonSchemaPropertySchema = z2.object({
379
+ type: z2.union([z2.string(), z2.array(z2.string())]).optional(),
380
+ enum: z2.array(z2.unknown()).optional(),
381
+ title: z2.string().optional(),
382
+ description: z2.string().optional(),
383
+ default: z2.unknown().optional(),
384
+ format: z2.string().optional(),
385
+ writeOnly: z2.boolean().optional()
386
+ }).loose();
387
+ var JsonSchemaObjectSchema = z2.object({
388
+ properties: z2.record(z2.string(), z2.unknown()).default({}),
389
+ required: z2.array(z2.string()).default([])
390
+ }).loose();
391
+ function terminalFormFieldsFromJsonSchema2(schema) {
392
+ const object = JsonSchemaObjectSchema.parse(schema);
393
+ return Object.entries(object.properties).map(([name, raw]) => {
394
+ const property = JsonSchemaPropertySchema.parse(typeof raw === "boolean" ? {} : raw ?? {});
395
+ const types = (Array.isArray(property.type) ? property.type : [property.type]).filter((type) => type !== undefined && type !== "null");
396
+ const type = types.length === 1 ? types[0] : undefined;
397
+ const listed = property.enum?.filter((value) => value !== null);
398
+ const scalars = listed?.filter((value) => typeof value === "string" || typeof value === "number" || typeof value === "boolean");
399
+ const choices = scalars !== undefined && scalars.length > 0 && scalars.length === listed?.length ? scalars : undefined;
400
+ const kind = choices ? "choice" : type === "string" ? property.format === "password" || property.writeOnly ? "secret" : "text" : type === "number" ? "number" : type === "integer" ? "integer" : type === "boolean" ? "boolean" : "json";
401
+ return {
402
+ name,
403
+ label: property.title ?? name,
404
+ kind,
405
+ required: object.required.includes(name),
406
+ ...choices && { choices },
407
+ ...property.default !== undefined && { defaultValue: property.default },
408
+ ...property.description !== undefined && { description: property.description }
409
+ };
410
+ });
411
+ }
412
+
413
+ // src/core/labels.ts
414
+ var terminalLabelsEn2 = Object.freeze({
415
+ form: terminalFormMessages2,
416
+ rows: (shown, total) => shown === total ? `${total} ${total === 1 ? "row" : "rows"}` : `${shown} of ${total} rows`,
417
+ filter: "filter",
418
+ nothingFound: "Nothing found",
419
+ moreAbove: (count) => `\u2191 ${count} more`,
420
+ moreBelow: (count) => `\u2193 ${count} more`,
421
+ loading: "Loading",
422
+ empty: "Nothing here yet",
423
+ error: "Failed",
424
+ retry: "retry",
425
+ yes: "Yes",
426
+ no: "No",
427
+ back: "back",
428
+ open: "open",
429
+ move: "move",
430
+ page: "page",
431
+ search: "search",
432
+ typing: "type",
433
+ submit: "submit",
434
+ next: "next field",
435
+ change: "change",
436
+ sort: "sort",
437
+ invert: "invert",
438
+ expand: "expand",
439
+ collapse: "collapse",
440
+ tabs: "views",
441
+ pick: "pick",
442
+ help: "help",
443
+ quit: "quit",
444
+ top: "top",
445
+ bottom: "bottom",
446
+ required: "required",
447
+ noFields: "No parameters \xB7 Enter to run",
448
+ crashed: "The application stopped on an error",
449
+ scroll: (percent) => `${percent}%`
450
+ });
451
+ var terminalLabelsRu2 = Object.freeze({
452
+ form: {
453
+ required: "\u041E\u0431\u044F\u0437\u0430\u0442\u0435\u043B\u044C\u043D\u043E\u0435 \u043F\u043E\u043B\u0435",
454
+ number: "\u041D\u0443\u0436\u043D\u043E \u0447\u0438\u0441\u043B\u043E",
455
+ integer: "\u041D\u0443\u0436\u043D\u043E \u0446\u0435\u043B\u043E\u0435 \u0447\u0438\u0441\u043B\u043E",
456
+ choice: (choices) => `\u0412\u044B\u0431\u0435\u0440\u0438\u0442\u0435 \u043E\u0434\u043D\u043E \u0438\u0437: ${choices.join(", ")}`,
457
+ json: (reason) => `\u041D\u0435\u0432\u0435\u0440\u043D\u044B\u0439 JSON: ${reason}`
458
+ },
459
+ rows: (shown, total) => shown === total ? `\u0441\u0442\u0440\u043E\u043A: ${total}` : `${shown} \u0438\u0437 ${total}`,
460
+ filter: "\u0444\u0438\u043B\u044C\u0442\u0440",
461
+ nothingFound: "\u041D\u0438\u0447\u0435\u0433\u043E \u043D\u0435 \u043D\u0430\u0439\u0434\u0435\u043D\u043E",
462
+ moreAbove: (count) => `\u2191 \u0435\u0449\u0451 ${count}`,
463
+ moreBelow: (count) => `\u2193 \u0435\u0449\u0451 ${count}`,
464
+ loading: "\u0417\u0430\u0433\u0440\u0443\u0437\u043A\u0430",
465
+ empty: "\u041F\u043E\u043A\u0430 \u043F\u0443\u0441\u0442\u043E",
466
+ error: "\u041E\u0448\u0438\u0431\u043A\u0430",
467
+ retry: "\u043F\u043E\u0432\u0442\u043E\u0440\u0438\u0442\u044C",
468
+ yes: "\u0414\u0430",
469
+ no: "\u041D\u0435\u0442",
470
+ back: "\u043D\u0430\u0437\u0430\u0434",
471
+ open: "\u043E\u0442\u043A\u0440\u044B\u0442\u044C",
472
+ move: "\u0432\u044B\u0431\u043E\u0440",
473
+ page: "\u0441\u0442\u0440\u0430\u043D\u0438\u0446\u0430",
474
+ search: "\u043F\u043E\u0438\u0441\u043A",
475
+ typing: "\u043F\u0435\u0447\u0430\u0442\u0430\u0439\u0442\u0435",
476
+ submit: "\u0432\u044B\u043F\u043E\u043B\u043D\u0438\u0442\u044C",
477
+ next: "\u0441\u043B\u0435\u0434\u0443\u044E\u0449\u0435\u0435 \u043F\u043E\u043B\u0435",
478
+ change: "\u0438\u0437\u043C\u0435\u043D\u0438\u0442\u044C",
479
+ sort: "\u0441\u043E\u0440\u0442\u0438\u0440\u043E\u0432\u043A\u0430",
480
+ invert: "\u043E\u0431\u0440\u0430\u0442\u043D\u044B\u0439 \u043F\u043E\u0440\u044F\u0434\u043E\u043A",
481
+ expand: "\u0440\u0430\u0441\u043A\u0440\u044B\u0442\u044C",
482
+ collapse: "\u0441\u0432\u0435\u0440\u043D\u0443\u0442\u044C",
483
+ tabs: "\u0432\u043A\u043B\u0430\u0434\u043A\u0438",
484
+ pick: "\u043E\u0442\u043C\u0435\u0442\u0438\u0442\u044C",
485
+ help: "\u0441\u043F\u0440\u0430\u0432\u043A\u0430",
486
+ quit: "\u0432\u044B\u0445\u043E\u0434",
487
+ top: "\u0432 \u043D\u0430\u0447\u0430\u043B\u043E",
488
+ bottom: "\u0432 \u043A\u043E\u043D\u0435\u0446",
489
+ required: "\u043E\u0431\u044F\u0437\u0430\u0442\u0435\u043B\u044C\u043D\u043E",
490
+ noFields: "\u041F\u0430\u0440\u0430\u043C\u0435\u0442\u0440\u043E\u0432 \u043D\u0435\u0442 \xB7 Enter \u2014 \u0432\u044B\u043F\u043E\u043B\u043D\u0438\u0442\u044C",
491
+ crashed: "\u041F\u0440\u0438\u043B\u043E\u0436\u0435\u043D\u0438\u0435 \u043E\u0441\u0442\u0430\u043D\u043E\u0432\u0438\u043B\u043E\u0441\u044C \u0438\u0437-\u0437\u0430 \u043E\u0448\u0438\u0431\u043A\u0438",
492
+ scroll: (percent) => `${percent}%`
493
+ });
494
+
495
+ export { TerminalToneSchema2, TerminalThemeModeSchema2, TerminalPaletteSchema2, terminalPalettes2, resolveTerminalPalette2, sanitizeTerminalText2, terminalGraphemes2, terminalGraphemeWidth2, terminalTextWidth2, truncateTerminalText2, fitTerminalText2, wrapTerminalText2, terminalTextEdit2, applyTerminalTextEdit2, TerminalFormFieldSchema2, terminalFormMessages2, createTerminalForm2, readTerminalForm2, reduceTerminalForm2, terminalFormFieldsFromJsonSchema2, terminalLabelsEn2, terminalLabelsRu2 };