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,1229 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
import {
|
|
3
|
+
TerminalToneSchema2,
|
|
4
|
+
sanitizeTerminalText2,
|
|
5
|
+
terminalTextWidth2,
|
|
6
|
+
truncateTerminalText2,
|
|
7
|
+
fitTerminalText2,
|
|
8
|
+
applyTerminalTextEdit2,
|
|
9
|
+
terminalLabelsEn2
|
|
10
|
+
} from "./index-ahfc59de.js";
|
|
11
|
+
|
|
12
|
+
// src/core/search.ts
|
|
13
|
+
function fold(text) {
|
|
14
|
+
return Array.from(text, (char) => char.toLocaleLowerCase());
|
|
15
|
+
}
|
|
16
|
+
function indexOfUnits(haystack, needle) {
|
|
17
|
+
outer:
|
|
18
|
+
for (let start = 0;start + needle.length <= haystack.length; start += 1) {
|
|
19
|
+
for (let offset = 0;offset < needle.length; offset += 1) {
|
|
20
|
+
if (haystack[start + offset] !== needle[offset])
|
|
21
|
+
continue outer;
|
|
22
|
+
}
|
|
23
|
+
return start;
|
|
24
|
+
}
|
|
25
|
+
return -1;
|
|
26
|
+
}
|
|
27
|
+
function rangesOf(positions) {
|
|
28
|
+
const ranges = [];
|
|
29
|
+
for (const position of positions) {
|
|
30
|
+
const last = ranges.at(-1);
|
|
31
|
+
if (last && last[1] === position)
|
|
32
|
+
last[1] += 1;
|
|
33
|
+
else
|
|
34
|
+
ranges.push([position, position + 1]);
|
|
35
|
+
}
|
|
36
|
+
return ranges;
|
|
37
|
+
}
|
|
38
|
+
function matchTerminalSearch2(query, text, options = {}) {
|
|
39
|
+
const words = query.trim().split(/\s+/).filter(Boolean);
|
|
40
|
+
if (words.length === 0)
|
|
41
|
+
return { score: 0, ranges: [] };
|
|
42
|
+
const haystack = fold(text);
|
|
43
|
+
const isBoundary = (index) => index === 0 || /[\s\-_./:\u00B7,(]/.test(haystack[index - 1] ?? " ");
|
|
44
|
+
let score = 0;
|
|
45
|
+
const positions = new Set;
|
|
46
|
+
for (const word of words) {
|
|
47
|
+
const needle = fold(word);
|
|
48
|
+
const start = indexOfUnits(haystack, needle);
|
|
49
|
+
if (start >= 0) {
|
|
50
|
+
for (let offset = 0;offset < needle.length; offset += 1)
|
|
51
|
+
positions.add(start + offset);
|
|
52
|
+
score += 100 + (isBoundary(start) ? 50 : 0) - Math.min(start, 40);
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
if (options.scattered === false)
|
|
56
|
+
return null;
|
|
57
|
+
let cursor = 0;
|
|
58
|
+
let gaps = 0;
|
|
59
|
+
const found = [];
|
|
60
|
+
for (const letter of needle) {
|
|
61
|
+
let index = cursor;
|
|
62
|
+
while (index < haystack.length && haystack[index] !== letter)
|
|
63
|
+
index += 1;
|
|
64
|
+
if (index >= haystack.length)
|
|
65
|
+
return null;
|
|
66
|
+
if (found.length > 0)
|
|
67
|
+
gaps += index - cursor;
|
|
68
|
+
found.push(index);
|
|
69
|
+
cursor = index + 1;
|
|
70
|
+
}
|
|
71
|
+
for (const index of found)
|
|
72
|
+
positions.add(index);
|
|
73
|
+
score += 20 + found.filter(isBoundary).length * 5 - Math.min(gaps, 30);
|
|
74
|
+
}
|
|
75
|
+
return { score, ranges: rangesOf([...positions].sort((a, b) => a - b)) };
|
|
76
|
+
}
|
|
77
|
+
function searchTerminalItems2(items, query, text, options = {}) {
|
|
78
|
+
const results = [];
|
|
79
|
+
items.forEach((item, index) => {
|
|
80
|
+
const match = matchTerminalSearch2(query, text(item), options);
|
|
81
|
+
if (match)
|
|
82
|
+
results.push({ item, match, index });
|
|
83
|
+
});
|
|
84
|
+
if (query.trim() !== "") {
|
|
85
|
+
results.sort((a, b) => b.match.score - a.match.score || a.index - b.index);
|
|
86
|
+
}
|
|
87
|
+
return results.map(({ item, match }) => ({ item, match }));
|
|
88
|
+
}
|
|
89
|
+
function terminalHighlightRuns2(text, ranges) {
|
|
90
|
+
const points = Array.from(text);
|
|
91
|
+
const runs = [];
|
|
92
|
+
let cursor = 0;
|
|
93
|
+
for (const [start, end] of ranges) {
|
|
94
|
+
if (start > cursor)
|
|
95
|
+
runs.push({ text: points.slice(cursor, start).join(""), highlighted: false });
|
|
96
|
+
runs.push({ text: points.slice(start, end).join(""), highlighted: true });
|
|
97
|
+
cursor = end;
|
|
98
|
+
}
|
|
99
|
+
if (cursor < points.length)
|
|
100
|
+
runs.push({ text: points.slice(cursor).join(""), highlighted: false });
|
|
101
|
+
return runs;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// src/core/chart.ts
|
|
105
|
+
import { z } from "zod";
|
|
106
|
+
var SampleSchema = z.object({
|
|
107
|
+
at: z.number(),
|
|
108
|
+
value: z.number().nullable()
|
|
109
|
+
}).strict();
|
|
110
|
+
var TimeSeriesOptionsSchema = z.object({
|
|
111
|
+
windowMs: z.int().positive(),
|
|
112
|
+
maxSamples: z.int().min(2).default(4096)
|
|
113
|
+
}).strict();
|
|
114
|
+
function createTerminalTimeSeries2(options) {
|
|
115
|
+
return { ...TimeSeriesOptionsSchema.parse(options), samples: [] };
|
|
116
|
+
}
|
|
117
|
+
function appendTerminalSample2(series, sample) {
|
|
118
|
+
const next = SampleSchema.parse(sample);
|
|
119
|
+
const last = series.samples.at(-1);
|
|
120
|
+
if (last !== undefined && next.at < last.at) {
|
|
121
|
+
throw new Error(`Terminal samples must arrive in time order: ${next.at} is before ${last.at}`);
|
|
122
|
+
}
|
|
123
|
+
const horizon = next.at - series.windowMs;
|
|
124
|
+
let first = Math.max(0, series.samples.length + 1 - series.maxSamples);
|
|
125
|
+
while (first + 1 < series.samples.length && (series.samples[first + 1]?.at ?? 0) <= horizon) {
|
|
126
|
+
first += 1;
|
|
127
|
+
}
|
|
128
|
+
return { ...series, samples: [...series.samples.slice(first), next] };
|
|
129
|
+
}
|
|
130
|
+
function sampleTerminalTimeSeries2(series, input) {
|
|
131
|
+
const count = z.int().nonnegative().parse(input.count);
|
|
132
|
+
const values = [];
|
|
133
|
+
let cursor = -1;
|
|
134
|
+
for (let index = 0;index < count; index += 1) {
|
|
135
|
+
const instant = count === 1 ? input.now : input.now - series.windowMs + series.windowMs * index / (count - 1);
|
|
136
|
+
while (cursor + 1 < series.samples.length && (series.samples[cursor + 1]?.at ?? 0) <= instant) {
|
|
137
|
+
cursor += 1;
|
|
138
|
+
}
|
|
139
|
+
values.push(cursor < 0 ? null : series.samples[cursor]?.value ?? null);
|
|
140
|
+
}
|
|
141
|
+
return values;
|
|
142
|
+
}
|
|
143
|
+
function terminalChartScale2(values, fixed = {}) {
|
|
144
|
+
const present = values.filter((value) => value !== null);
|
|
145
|
+
const min = fixed.min ?? Math.min(0, ...present);
|
|
146
|
+
const max = fixed.max ?? Math.max(min, ...present);
|
|
147
|
+
return { min, max: max > min ? max : min + 1 };
|
|
148
|
+
}
|
|
149
|
+
var BARS = ["\u2581", "\u2582", "\u2583", "\u2584", "\u2585", "\u2586", "\u2587", "\u2588"];
|
|
150
|
+
function terminalSparkline2(values, scale = {}) {
|
|
151
|
+
const { min, max } = terminalChartScale2(values, scale);
|
|
152
|
+
return values.map((value) => {
|
|
153
|
+
if (value === null)
|
|
154
|
+
return " ";
|
|
155
|
+
const ratio = Math.min(1, Math.max(0, (value - min) / (max - min)));
|
|
156
|
+
return BARS[Math.round(ratio * (BARS.length - 1))] ?? " ";
|
|
157
|
+
}).join("");
|
|
158
|
+
}
|
|
159
|
+
var DOT_BITS = [
|
|
160
|
+
[1, 8],
|
|
161
|
+
[2, 16],
|
|
162
|
+
[4, 32],
|
|
163
|
+
[64, 128]
|
|
164
|
+
];
|
|
165
|
+
var ChartInputSchema = z.object({
|
|
166
|
+
series: z.array(z.object({
|
|
167
|
+
values: z.array(z.number().nullable()),
|
|
168
|
+
tone: TerminalToneSchema2
|
|
169
|
+
}).strict()),
|
|
170
|
+
width: z.int().positive(),
|
|
171
|
+
height: z.int().positive(),
|
|
172
|
+
min: z.number().optional(),
|
|
173
|
+
max: z.number().optional(),
|
|
174
|
+
fill: z.boolean().default(false)
|
|
175
|
+
}).strict();
|
|
176
|
+
function terminalBrailleChart2(input) {
|
|
177
|
+
const { series, width, height, fill, ...fixed } = ChartInputSchema.parse(input);
|
|
178
|
+
const dotsHigh = height * 4;
|
|
179
|
+
const dotsWide = width * 2;
|
|
180
|
+
const bits = new Uint8Array(width * height);
|
|
181
|
+
const tones = new Array(width * height).fill(null);
|
|
182
|
+
const scale = terminalChartScale2(series.flatMap(({ values }) => values), fixed);
|
|
183
|
+
const level = (value) => Math.round(Math.min(1, Math.max(0, (value - scale.min) / (scale.max - scale.min))) * (dotsHigh - 1));
|
|
184
|
+
const plot = (x, y, tone) => {
|
|
185
|
+
const row = dotsHigh - 1 - y;
|
|
186
|
+
const cell = Math.floor(row / 4) * width + Math.floor(x / 2);
|
|
187
|
+
bits[cell] = (bits[cell] ?? 0) | (DOT_BITS[row % 4]?.[x % 2] ?? 0);
|
|
188
|
+
tones[cell] = tone;
|
|
189
|
+
};
|
|
190
|
+
for (const { values, tone } of series) {
|
|
191
|
+
let previous = null;
|
|
192
|
+
for (let x = 0;x < Math.min(dotsWide, values.length); x += 1) {
|
|
193
|
+
const value = values[x] ?? null;
|
|
194
|
+
if (value === null) {
|
|
195
|
+
previous = null;
|
|
196
|
+
continue;
|
|
197
|
+
}
|
|
198
|
+
const y = level(value);
|
|
199
|
+
const from = fill ? 0 : previous === null ? y : Math.min(previous, y);
|
|
200
|
+
const to = fill ? y : previous === null ? y : Math.max(previous, y);
|
|
201
|
+
for (let dot = from;dot <= to; dot += 1)
|
|
202
|
+
plot(x, dot, tone);
|
|
203
|
+
previous = y;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
return Array.from({ length: height }, (_, row) => {
|
|
207
|
+
const segments = [];
|
|
208
|
+
for (let column = 0;column < width; column += 1) {
|
|
209
|
+
const cell = row * width + column;
|
|
210
|
+
const value = bits[cell] ?? 0;
|
|
211
|
+
const tone = value === 0 ? null : tones[cell] ?? null;
|
|
212
|
+
const text = value === 0 ? " " : String.fromCharCode(10240 + value);
|
|
213
|
+
const last = segments.at(-1);
|
|
214
|
+
if (last !== undefined && last.tone === tone)
|
|
215
|
+
last.text += text;
|
|
216
|
+
else
|
|
217
|
+
segments.push({ text, tone });
|
|
218
|
+
}
|
|
219
|
+
return segments;
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
function durationLabel(ms) {
|
|
223
|
+
const exact = ms / 1000;
|
|
224
|
+
if (!Number.isInteger(exact) && exact < 60)
|
|
225
|
+
return `${Number(exact.toFixed(1))}s`;
|
|
226
|
+
const seconds = Math.round(exact);
|
|
227
|
+
if (seconds % 3600 === 0 && seconds >= 3600)
|
|
228
|
+
return `${seconds / 3600}h`;
|
|
229
|
+
if (seconds % 60 === 0 && seconds >= 60)
|
|
230
|
+
return `${seconds / 60}m`;
|
|
231
|
+
return `${seconds}s`;
|
|
232
|
+
}
|
|
233
|
+
function terminalTimeAxis2(windowMs, width) {
|
|
234
|
+
const cells = z.int().nonnegative().parse(width);
|
|
235
|
+
const left = `-${durationLabel(z.int().positive().parse(windowMs))}`;
|
|
236
|
+
const right = "now";
|
|
237
|
+
if (cells < left.length + right.length + 1)
|
|
238
|
+
return right.padStart(cells).slice(0, cells);
|
|
239
|
+
const middle = `-${durationLabel(windowMs / 2)}`;
|
|
240
|
+
const gap = cells - left.length - right.length;
|
|
241
|
+
if (gap < middle.length + 2)
|
|
242
|
+
return left + " ".repeat(gap) + right;
|
|
243
|
+
const before = Math.floor((gap - middle.length) / 2);
|
|
244
|
+
return left + " ".repeat(before) + middle + " ".repeat(gap - middle.length - before) + right;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
// src/core/collection.ts
|
|
248
|
+
import { z as z2 } from "zod";
|
|
249
|
+
var TerminalCollectionStateSchema2 = z2.object({
|
|
250
|
+
keys: z2.array(z2.string().min(1)),
|
|
251
|
+
selectedKey: z2.string().min(1).nullable(),
|
|
252
|
+
start: z2.int().nonnegative(),
|
|
253
|
+
capacity: z2.int().positive()
|
|
254
|
+
}).strict();
|
|
255
|
+
function maximumStart(total, capacity) {
|
|
256
|
+
return Math.max(0, total - capacity);
|
|
257
|
+
}
|
|
258
|
+
function reveal(start, capacity, index, total) {
|
|
259
|
+
const bounded = Math.min(Math.max(0, start), maximumStart(total, capacity));
|
|
260
|
+
if (index < bounded)
|
|
261
|
+
return index;
|
|
262
|
+
if (index >= bounded + capacity)
|
|
263
|
+
return index - capacity + 1;
|
|
264
|
+
return bounded;
|
|
265
|
+
}
|
|
266
|
+
function uniqueKeys(keys) {
|
|
267
|
+
const result = z2.array(z2.string().min(1)).parse(keys);
|
|
268
|
+
if (new Set(result).size !== result.length) {
|
|
269
|
+
throw new Error("Terminal collection keys must be unique");
|
|
270
|
+
}
|
|
271
|
+
return result;
|
|
272
|
+
}
|
|
273
|
+
function createTerminalCollection2(keys, capacity, selectedKey) {
|
|
274
|
+
const parsedKeys = uniqueKeys(keys);
|
|
275
|
+
const parsedCapacity = z2.int().positive().parse(capacity);
|
|
276
|
+
const selected = selectedKey !== undefined && parsedKeys.includes(selectedKey) ? selectedKey : parsedKeys[0] ?? null;
|
|
277
|
+
const index = selected === null ? -1 : parsedKeys.indexOf(selected);
|
|
278
|
+
return TerminalCollectionStateSchema2.parse({
|
|
279
|
+
keys: parsedKeys,
|
|
280
|
+
selectedKey: selected,
|
|
281
|
+
start: index < 0 ? 0 : reveal(0, parsedCapacity, index, parsedKeys.length),
|
|
282
|
+
capacity: parsedCapacity
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
function reduceTerminalCollection2(state, action) {
|
|
286
|
+
const current = state;
|
|
287
|
+
if (action.type === "reconcile") {
|
|
288
|
+
const keys = uniqueKeys(action.keys);
|
|
289
|
+
if (keys.length === 0)
|
|
290
|
+
return { keys, selectedKey: null, start: 0, capacity: current.capacity };
|
|
291
|
+
const previousIndex = current.selectedKey === null ? 0 : Math.max(0, current.keys.indexOf(current.selectedKey));
|
|
292
|
+
const selectedKey = current.selectedKey !== null && keys.includes(current.selectedKey) ? current.selectedKey : keys[Math.min(previousIndex, keys.length - 1)];
|
|
293
|
+
if (!selectedKey)
|
|
294
|
+
throw new Error("Terminal collection survivor is unavailable");
|
|
295
|
+
const index = keys.indexOf(selectedKey);
|
|
296
|
+
return {
|
|
297
|
+
...current,
|
|
298
|
+
keys,
|
|
299
|
+
selectedKey,
|
|
300
|
+
start: reveal(current.start, current.capacity, index, keys.length)
|
|
301
|
+
};
|
|
302
|
+
}
|
|
303
|
+
if (action.type === "resize") {
|
|
304
|
+
const capacity = z2.int().positive().parse(action.capacity);
|
|
305
|
+
const index = current.selectedKey === null ? -1 : current.keys.indexOf(current.selectedKey);
|
|
306
|
+
return {
|
|
307
|
+
...current,
|
|
308
|
+
capacity,
|
|
309
|
+
start: index < 0 ? 0 : reveal(current.start, capacity, index, current.keys.length)
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
if (action.type === "scroll") {
|
|
313
|
+
return {
|
|
314
|
+
...current,
|
|
315
|
+
start: Math.min(Math.max(0, current.start + z2.int().parse(action.lines)), maximumStart(current.keys.length, current.capacity))
|
|
316
|
+
};
|
|
317
|
+
}
|
|
318
|
+
if (current.keys.length === 0)
|
|
319
|
+
return current;
|
|
320
|
+
const currentIndex = current.selectedKey === null ? 0 : Math.max(0, current.keys.indexOf(current.selectedKey));
|
|
321
|
+
const nextIndex = action.type === "select" ? current.keys.indexOf(action.key) : action.wrap ? ((currentIndex + action.delta) % current.keys.length + current.keys.length) % current.keys.length : Math.min(Math.max(0, currentIndex + action.delta), current.keys.length - 1);
|
|
322
|
+
if (nextIndex < 0) {
|
|
323
|
+
throw new Error(action.type === "select" ? `Unknown terminal collection key: ${action.key}` : "Terminal collection movement could not resolve a selection");
|
|
324
|
+
}
|
|
325
|
+
const selectedKey = current.keys[nextIndex];
|
|
326
|
+
if (!selectedKey)
|
|
327
|
+
throw new Error("Terminal collection selection is unavailable");
|
|
328
|
+
return {
|
|
329
|
+
...current,
|
|
330
|
+
selectedKey,
|
|
331
|
+
start: reveal(current.start, current.capacity, nextIndex, current.keys.length)
|
|
332
|
+
};
|
|
333
|
+
}
|
|
334
|
+
function visibleTerminalCollectionRange2(state) {
|
|
335
|
+
return {
|
|
336
|
+
start: state.start,
|
|
337
|
+
end: Math.min(state.keys.length, state.start + state.capacity)
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
// src/core/json.ts
|
|
342
|
+
function terminalJsonLines2(value, indent = 2) {
|
|
343
|
+
const lines = [];
|
|
344
|
+
const pad = (depth) => ({
|
|
345
|
+
text: " ".repeat(depth * indent),
|
|
346
|
+
tone: "muted"
|
|
347
|
+
});
|
|
348
|
+
const scalar = (input) => {
|
|
349
|
+
if (typeof input === "string")
|
|
350
|
+
return { text: sanitizeTerminalText2(JSON.stringify(input)), tone: "success" };
|
|
351
|
+
if (input === null || typeof input === "boolean")
|
|
352
|
+
return { text: String(input), tone: "accent" };
|
|
353
|
+
if (typeof input === "number" || typeof input === "bigint")
|
|
354
|
+
return { text: String(input), tone: "warning" };
|
|
355
|
+
return { text: JSON.stringify(input) ?? "undefined", tone: "muted" };
|
|
356
|
+
};
|
|
357
|
+
const seen = new WeakSet;
|
|
358
|
+
const visit = (raw, depth, prefix, last) => {
|
|
359
|
+
const comma = last ? [] : [{ text: ",", tone: "muted" }];
|
|
360
|
+
const input = typeof raw === "object" && raw !== null && "toJSON" in raw && typeof raw.toJSON === "function" ? raw.toJSON() : raw;
|
|
361
|
+
if (typeof input === "object" && input !== null) {
|
|
362
|
+
if (seen.has(input)) {
|
|
363
|
+
lines.push([pad(depth), ...prefix, { text: "[Circular]", tone: "muted" }, ...comma]);
|
|
364
|
+
return;
|
|
365
|
+
}
|
|
366
|
+
seen.add(input);
|
|
367
|
+
}
|
|
368
|
+
const entries = Array.isArray(input) ? input.map((item) => [null, item]) : typeof input === "object" && input !== null ? Object.entries(input) : null;
|
|
369
|
+
if (entries === null) {
|
|
370
|
+
lines.push([pad(depth), ...prefix, scalar(input), ...comma]);
|
|
371
|
+
return;
|
|
372
|
+
}
|
|
373
|
+
const [open, close] = Array.isArray(input) ? ["[", "]"] : ["{", "}"];
|
|
374
|
+
if (entries.length === 0) {
|
|
375
|
+
lines.push([
|
|
376
|
+
pad(depth),
|
|
377
|
+
...prefix,
|
|
378
|
+
{ text: `${open}${close}`, tone: "muted" },
|
|
379
|
+
...comma
|
|
380
|
+
]);
|
|
381
|
+
return;
|
|
382
|
+
}
|
|
383
|
+
lines.push([pad(depth), ...prefix, { text: open, tone: "muted" }]);
|
|
384
|
+
entries.forEach(([key, item], index) => {
|
|
385
|
+
const keyRuns = key === null ? [] : [
|
|
386
|
+
{ text: sanitizeTerminalText2(JSON.stringify(key)), tone: "accent" },
|
|
387
|
+
{ text: ": ", tone: "muted" }
|
|
388
|
+
];
|
|
389
|
+
visit(item, depth + 1, keyRuns, index === entries.length - 1);
|
|
390
|
+
});
|
|
391
|
+
lines.push([pad(depth), { text: close, tone: "muted" }, ...comma]);
|
|
392
|
+
};
|
|
393
|
+
visit(value, 0, [], true);
|
|
394
|
+
return lines.map((line) => line.filter(({ text }) => text !== ""));
|
|
395
|
+
}
|
|
396
|
+
function layoutTerminalDetails2(pairs, width, gap = 2) {
|
|
397
|
+
const keyWidth = Math.min(Math.max(0, ...pairs.map(({ key }) => terminalTextWidth2(key))), Math.max(1, Math.floor(width / 3)));
|
|
398
|
+
return pairs.map(({ key, value, tone }) => ({
|
|
399
|
+
key: fitTerminalText2(sanitizeTerminalText2(key), keyWidth) + " ".repeat(gap),
|
|
400
|
+
value: truncateTerminalText2(sanitizeTerminalText2(value).replace(/\n/g, " \u23CE "), Math.max(1, width - keyWidth - gap)),
|
|
401
|
+
tone: tone ?? "primary"
|
|
402
|
+
}));
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
// src/core/key-hints.ts
|
|
406
|
+
import { z as z3 } from "zod";
|
|
407
|
+
var TerminalKeyBindingSchema2 = z3.object({
|
|
408
|
+
id: z3.string().min(1),
|
|
409
|
+
keys: z3.array(z3.string().min(1)).min(1),
|
|
410
|
+
display: z3.string().min(1),
|
|
411
|
+
label: z3.string().min(1),
|
|
412
|
+
short: z3.string().min(1).optional(),
|
|
413
|
+
priority: z3.int().nonnegative().default(1),
|
|
414
|
+
ctrl: z3.boolean().default(false),
|
|
415
|
+
shift: z3.boolean().optional(),
|
|
416
|
+
hidden: z3.boolean().default(false)
|
|
417
|
+
}).strict();
|
|
418
|
+
function defineTerminalKeymap2(bindings) {
|
|
419
|
+
const parsed = z3.array(TerminalKeyBindingSchema2).parse(bindings);
|
|
420
|
+
const ids = new Set;
|
|
421
|
+
const chords = new Map;
|
|
422
|
+
for (const binding of parsed) {
|
|
423
|
+
if (ids.has(binding.id))
|
|
424
|
+
throw new Error(`Terminal key binding ${binding.id} is declared twice`);
|
|
425
|
+
ids.add(binding.id);
|
|
426
|
+
for (const key of binding.keys) {
|
|
427
|
+
const shifts = binding.shift === undefined ? [false, true] : [binding.shift];
|
|
428
|
+
for (const shift of shifts) {
|
|
429
|
+
const chord = `${binding.ctrl ? "ctrl+" : ""}${shift ? "shift+" : ""}${key}`;
|
|
430
|
+
const owner = chords.get(chord);
|
|
431
|
+
if (owner)
|
|
432
|
+
throw new Error(`Key ${chord} is bound to both ${owner} and ${binding.id}`);
|
|
433
|
+
chords.set(chord, binding.id);
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
return parsed;
|
|
438
|
+
}
|
|
439
|
+
function matchTerminalKey2(bindings, key) {
|
|
440
|
+
for (const binding of bindings) {
|
|
441
|
+
if (binding.ctrl !== Boolean(key.ctrl))
|
|
442
|
+
continue;
|
|
443
|
+
if (binding.shift !== undefined && binding.shift !== Boolean(key.shift))
|
|
444
|
+
continue;
|
|
445
|
+
if (binding.keys.some((name) => name === key.name || name === key.sequence))
|
|
446
|
+
return binding;
|
|
447
|
+
}
|
|
448
|
+
return null;
|
|
449
|
+
}
|
|
450
|
+
function fitTerminalKeyHints2(bindings, width, separator = " \xB7 ") {
|
|
451
|
+
const shown = bindings.filter(({ hidden }) => !hidden);
|
|
452
|
+
const render = (items, short) => items.map((binding) => `${binding.display} ${short ? binding.short ?? binding.label : binding.label}`).join(separator);
|
|
453
|
+
let items = [...shown];
|
|
454
|
+
for (;; ) {
|
|
455
|
+
const full = render(items, false);
|
|
456
|
+
if (terminalTextWidth2(full) <= width)
|
|
457
|
+
return full;
|
|
458
|
+
const short = render(items, true);
|
|
459
|
+
if (terminalTextWidth2(short) <= width)
|
|
460
|
+
return short;
|
|
461
|
+
if (items.length <= 1)
|
|
462
|
+
return truncateTerminalText2(short, width);
|
|
463
|
+
let drop = 0;
|
|
464
|
+
items.forEach((binding, index) => {
|
|
465
|
+
if (binding.priority >= (items[drop]?.priority ?? 0))
|
|
466
|
+
drop = index;
|
|
467
|
+
});
|
|
468
|
+
items = items.filter((_, index) => index !== drop);
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
// src/core/keys.ts
|
|
473
|
+
function terminalTableKeymap2(labels = terminalLabelsEn2) {
|
|
474
|
+
return defineTerminalKeymap2([
|
|
475
|
+
{
|
|
476
|
+
id: "up",
|
|
477
|
+
keys: ["up", "k"],
|
|
478
|
+
shift: false,
|
|
479
|
+
display: "\u2191\u2193",
|
|
480
|
+
label: labels.move,
|
|
481
|
+
priority: 0
|
|
482
|
+
},
|
|
483
|
+
{
|
|
484
|
+
id: "down",
|
|
485
|
+
keys: ["down", "j"],
|
|
486
|
+
shift: false,
|
|
487
|
+
display: "\u2193",
|
|
488
|
+
label: labels.move,
|
|
489
|
+
hidden: true
|
|
490
|
+
},
|
|
491
|
+
{
|
|
492
|
+
id: "activate",
|
|
493
|
+
keys: ["return", "enter"],
|
|
494
|
+
display: "Enter",
|
|
495
|
+
label: labels.open,
|
|
496
|
+
priority: 0
|
|
497
|
+
},
|
|
498
|
+
{ id: "filter", keys: ["/"], display: "/", label: labels.filter, priority: 1 },
|
|
499
|
+
{
|
|
500
|
+
id: "expand",
|
|
501
|
+
keys: ["right", "+"],
|
|
502
|
+
display: "\u2192\u2190",
|
|
503
|
+
label: `${labels.expand}/${labels.collapse}`,
|
|
504
|
+
short: labels.expand,
|
|
505
|
+
priority: 2
|
|
506
|
+
},
|
|
507
|
+
{
|
|
508
|
+
id: "collapse",
|
|
509
|
+
keys: ["left", "-"],
|
|
510
|
+
display: "\u2190",
|
|
511
|
+
label: labels.collapse,
|
|
512
|
+
hidden: true
|
|
513
|
+
},
|
|
514
|
+
{ id: "sort-next", keys: [">", "."], display: "<>", label: labels.sort, priority: 2 },
|
|
515
|
+
{ id: "sort-previous", keys: ["<", ","], display: "<", label: labels.sort, hidden: true },
|
|
516
|
+
{ id: "sort-invert", keys: ["I"], display: "I", label: labels.invert, priority: 4 },
|
|
517
|
+
{ id: "page-up", keys: ["pageup"], display: "PgUp/PgDn", label: labels.page, priority: 3 },
|
|
518
|
+
{ id: "page-down", keys: ["pagedown"], display: "PgDn", label: labels.page, hidden: true },
|
|
519
|
+
{ id: "home", keys: ["home"], display: "Home", label: labels.top, hidden: true },
|
|
520
|
+
{ id: "end", keys: ["end"], display: "End", label: labels.bottom, hidden: true }
|
|
521
|
+
]);
|
|
522
|
+
}
|
|
523
|
+
var defaultTableKeymap = terminalTableKeymap2();
|
|
524
|
+
function terminalTableKey2(state, key, keymap = defaultTableKeymap) {
|
|
525
|
+
if (key.meta)
|
|
526
|
+
return null;
|
|
527
|
+
const binding = matchTerminalKey2(keymap, key);
|
|
528
|
+
if (binding === null)
|
|
529
|
+
return null;
|
|
530
|
+
const page = state.collection.capacity;
|
|
531
|
+
const all = Math.max(1, state.collection.keys.length);
|
|
532
|
+
const act = (action) => ({
|
|
533
|
+
kind: "action",
|
|
534
|
+
action
|
|
535
|
+
});
|
|
536
|
+
switch (binding.id) {
|
|
537
|
+
case "up":
|
|
538
|
+
return act({ type: "move", delta: -1 });
|
|
539
|
+
case "down":
|
|
540
|
+
return act({ type: "move", delta: 1 });
|
|
541
|
+
case "page-up":
|
|
542
|
+
return act({ type: "move", delta: -page });
|
|
543
|
+
case "page-down":
|
|
544
|
+
return act({ type: "move", delta: page });
|
|
545
|
+
case "home":
|
|
546
|
+
return act({ type: "move", delta: -all });
|
|
547
|
+
case "end":
|
|
548
|
+
return act({ type: "move", delta: all });
|
|
549
|
+
case "activate":
|
|
550
|
+
return { kind: "activate" };
|
|
551
|
+
case "expand":
|
|
552
|
+
return act({ type: "expand" });
|
|
553
|
+
case "collapse":
|
|
554
|
+
return act({ type: "collapse" });
|
|
555
|
+
case "sort-next":
|
|
556
|
+
return act({ type: "sort-next", delta: 1 });
|
|
557
|
+
case "sort-previous":
|
|
558
|
+
return act({ type: "sort-next", delta: -1 });
|
|
559
|
+
case "sort-invert":
|
|
560
|
+
return act({ type: "sort-invert" });
|
|
561
|
+
case "filter":
|
|
562
|
+
return { kind: "filter-start" };
|
|
563
|
+
default:
|
|
564
|
+
return null;
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
function terminalTabsKeymap2(labels = terminalLabelsEn2) {
|
|
568
|
+
return defineTerminalKeymap2([
|
|
569
|
+
{
|
|
570
|
+
id: "next",
|
|
571
|
+
keys: ["tab"],
|
|
572
|
+
shift: false,
|
|
573
|
+
display: "Tab",
|
|
574
|
+
label: labels.tabs,
|
|
575
|
+
priority: 1
|
|
576
|
+
},
|
|
577
|
+
{
|
|
578
|
+
id: "previous",
|
|
579
|
+
keys: ["tab"],
|
|
580
|
+
shift: true,
|
|
581
|
+
display: "\u21E7Tab",
|
|
582
|
+
label: labels.tabs,
|
|
583
|
+
hidden: true
|
|
584
|
+
},
|
|
585
|
+
...Array.from({ length: 9 }, (_, index) => ({
|
|
586
|
+
id: `tab-${index + 1}`,
|
|
587
|
+
keys: [String(index + 1)],
|
|
588
|
+
display: index === 0 ? "1\u20139" : String(index + 1),
|
|
589
|
+
label: labels.tabs,
|
|
590
|
+
hidden: index > 0,
|
|
591
|
+
priority: 3
|
|
592
|
+
}))
|
|
593
|
+
]);
|
|
594
|
+
}
|
|
595
|
+
var defaultTabsKeymap = terminalTabsKeymap2();
|
|
596
|
+
function terminalTabsKey2(key, keymap = defaultTabsKeymap) {
|
|
597
|
+
if (key.meta)
|
|
598
|
+
return null;
|
|
599
|
+
const binding = matchTerminalKey2(keymap, key);
|
|
600
|
+
if (binding === null)
|
|
601
|
+
return null;
|
|
602
|
+
if (binding.id === "next")
|
|
603
|
+
return { type: "move", delta: 1 };
|
|
604
|
+
if (binding.id === "previous")
|
|
605
|
+
return { type: "move", delta: -1 };
|
|
606
|
+
const digit = /^tab-([1-9])$/.exec(binding.id)?.[1];
|
|
607
|
+
return digit ? { type: "select-index", index: Number(digit) - 1 } : null;
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
// src/core/list.ts
|
|
611
|
+
import { z as z4 } from "zod";
|
|
612
|
+
var TerminalListItemSchema2 = z4.object({
|
|
613
|
+
key: z4.string().min(1),
|
|
614
|
+
title: z4.string().min(1),
|
|
615
|
+
meta: z4.string().optional(),
|
|
616
|
+
description: z4.string().optional(),
|
|
617
|
+
section: z4.string().min(1).optional(),
|
|
618
|
+
badge: z4.union([z4.string(), z4.int()]).optional(),
|
|
619
|
+
tone: TerminalToneSchema2.optional(),
|
|
620
|
+
disabled: z4.boolean().default(false),
|
|
621
|
+
keywords: z4.array(z4.string()).default([])
|
|
622
|
+
}).strict();
|
|
623
|
+
function parseItems(items) {
|
|
624
|
+
const parsed = z4.array(TerminalListItemSchema2).parse(items);
|
|
625
|
+
const keys = new Set;
|
|
626
|
+
for (const item of parsed) {
|
|
627
|
+
if (keys.has(item.key))
|
|
628
|
+
throw new Error(`Terminal list keys must be unique: ${item.key}`);
|
|
629
|
+
keys.add(item.key);
|
|
630
|
+
}
|
|
631
|
+
return parsed;
|
|
632
|
+
}
|
|
633
|
+
function project(state) {
|
|
634
|
+
const searching = state.query.trim() !== "";
|
|
635
|
+
const results = searchTerminalItems2(state.items, state.query, (item) => [item.title, item.meta ?? "", item.description ?? "", ...item.keywords].join(" \xB7 "));
|
|
636
|
+
const titleMatch = (item, match) => {
|
|
637
|
+
const width = Array.from(item.title).length;
|
|
638
|
+
return match.ranges.filter(([start]) => start < width).map(([start, end]) => [start, Math.min(end, width)]);
|
|
639
|
+
};
|
|
640
|
+
if (searching) {
|
|
641
|
+
return results.map(({ item, match }) => ({
|
|
642
|
+
kind: "item",
|
|
643
|
+
key: item.key,
|
|
644
|
+
item,
|
|
645
|
+
titleRanges: titleMatch(item, match)
|
|
646
|
+
}));
|
|
647
|
+
}
|
|
648
|
+
const order = new Map(state.sections.map(({ id }, index) => [id, index]));
|
|
649
|
+
const groups = new Map;
|
|
650
|
+
for (const { item } of results) {
|
|
651
|
+
const section = item.section ?? null;
|
|
652
|
+
const group = groups.get(section);
|
|
653
|
+
if (group)
|
|
654
|
+
group.push(item);
|
|
655
|
+
else
|
|
656
|
+
groups.set(section, [item]);
|
|
657
|
+
}
|
|
658
|
+
const ids = [...groups.keys()].sort((a, b) => {
|
|
659
|
+
if (a === null)
|
|
660
|
+
return -1;
|
|
661
|
+
if (b === null)
|
|
662
|
+
return 1;
|
|
663
|
+
return (order.get(a) ?? Number.MAX_SAFE_INTEGER) - (order.get(b) ?? Number.MAX_SAFE_INTEGER);
|
|
664
|
+
});
|
|
665
|
+
const lines = [];
|
|
666
|
+
for (const id of ids) {
|
|
667
|
+
const group = groups.get(id) ?? [];
|
|
668
|
+
if (id !== null) {
|
|
669
|
+
const title = state.sections.find((section) => section.id === id)?.title ?? id;
|
|
670
|
+
lines.push({ kind: "section", key: `\xA7${id}`, title, count: group.length });
|
|
671
|
+
}
|
|
672
|
+
for (const item of group)
|
|
673
|
+
lines.push({ kind: "item", key: item.key, item, titleRanges: [] });
|
|
674
|
+
}
|
|
675
|
+
return lines;
|
|
676
|
+
}
|
|
677
|
+
function withLines(state, collection, queryChanged = false) {
|
|
678
|
+
const lines = project(state);
|
|
679
|
+
const keys = lines.filter((line) => line.kind === "item").map(({ key }) => key);
|
|
680
|
+
let next = reduceTerminalCollection2(collection, { type: "reconcile", keys });
|
|
681
|
+
if (queryChanged && state.query.trim() !== "" && keys[0] !== undefined) {
|
|
682
|
+
next = reduceTerminalCollection2(next, { type: "select", key: keys[0] });
|
|
683
|
+
}
|
|
684
|
+
return { ...state, lines, collection: next };
|
|
685
|
+
}
|
|
686
|
+
function createTerminalList2(input) {
|
|
687
|
+
const items = parseItems(input.items);
|
|
688
|
+
const base = {
|
|
689
|
+
items,
|
|
690
|
+
sections: input.sections ?? [],
|
|
691
|
+
query: "",
|
|
692
|
+
picked: new Set,
|
|
693
|
+
multiple: input.multiple ?? false,
|
|
694
|
+
activation: null
|
|
695
|
+
};
|
|
696
|
+
const state = withLines(base, createTerminalCollection2([], 1));
|
|
697
|
+
return input.selectedKey !== undefined && state.collection.keys.includes(input.selectedKey) ? {
|
|
698
|
+
...state,
|
|
699
|
+
collection: reduceTerminalCollection2(state.collection, {
|
|
700
|
+
type: "select",
|
|
701
|
+
key: input.selectedKey
|
|
702
|
+
})
|
|
703
|
+
} : state;
|
|
704
|
+
}
|
|
705
|
+
function reduceTerminalList2(state, action) {
|
|
706
|
+
const base = {
|
|
707
|
+
items: state.items,
|
|
708
|
+
sections: state.sections,
|
|
709
|
+
query: state.query,
|
|
710
|
+
picked: state.picked,
|
|
711
|
+
multiple: state.multiple,
|
|
712
|
+
activation: state.activation
|
|
713
|
+
};
|
|
714
|
+
switch (action.type) {
|
|
715
|
+
case "items": {
|
|
716
|
+
const items = parseItems(action.items);
|
|
717
|
+
const keys = new Set(items.map(({ key }) => key));
|
|
718
|
+
return withLines({
|
|
719
|
+
...base,
|
|
720
|
+
items,
|
|
721
|
+
sections: action.sections ?? state.sections,
|
|
722
|
+
picked: new Set([...state.picked].filter((key) => keys.has(key)))
|
|
723
|
+
}, state.collection);
|
|
724
|
+
}
|
|
725
|
+
case "query":
|
|
726
|
+
case "query-edit": {
|
|
727
|
+
const query = action.type === "query" ? z4.string().parse(action.query) : applyTerminalTextEdit2(state.query, action.edit);
|
|
728
|
+
return query === state.query ? state : withLines({ ...base, query }, state.collection, true);
|
|
729
|
+
}
|
|
730
|
+
case "activate": {
|
|
731
|
+
const item = selectedTerminalListItem2(state);
|
|
732
|
+
if (!item || item.disabled)
|
|
733
|
+
return state;
|
|
734
|
+
return {
|
|
735
|
+
...state,
|
|
736
|
+
activation: { key: item.key, serial: (state.activation?.serial ?? 0) + 1 }
|
|
737
|
+
};
|
|
738
|
+
}
|
|
739
|
+
case "section": {
|
|
740
|
+
const items = state.lines.filter((line) => line.kind === "item");
|
|
741
|
+
const current = items.findIndex(({ key }) => key === state.collection.selectedKey);
|
|
742
|
+
const sectionOf = (index) => {
|
|
743
|
+
const line = items[index];
|
|
744
|
+
return line?.kind === "item" ? line.item.section ?? "" : undefined;
|
|
745
|
+
};
|
|
746
|
+
const here = sectionOf(current);
|
|
747
|
+
let index = current;
|
|
748
|
+
const step = Math.sign(action.delta);
|
|
749
|
+
if (step === 0)
|
|
750
|
+
return state;
|
|
751
|
+
while (index + step >= 0 && index + step < items.length && sectionOf(index) === here)
|
|
752
|
+
index += step;
|
|
753
|
+
if (step < 0) {
|
|
754
|
+
const target = sectionOf(index);
|
|
755
|
+
while (index - 1 >= 0 && sectionOf(index - 1) === target)
|
|
756
|
+
index -= 1;
|
|
757
|
+
}
|
|
758
|
+
const key = items[index]?.key;
|
|
759
|
+
return key === undefined ? state : {
|
|
760
|
+
...state,
|
|
761
|
+
collection: reduceTerminalCollection2(state.collection, { type: "select", key })
|
|
762
|
+
};
|
|
763
|
+
}
|
|
764
|
+
case "toggle": {
|
|
765
|
+
if (!state.multiple)
|
|
766
|
+
return state;
|
|
767
|
+
const key = action.key ?? state.collection.selectedKey;
|
|
768
|
+
const item = state.items.find((candidate) => candidate.key === key);
|
|
769
|
+
if (!item || item.disabled)
|
|
770
|
+
return state;
|
|
771
|
+
const picked = new Set(state.picked);
|
|
772
|
+
if (picked.has(item.key))
|
|
773
|
+
picked.delete(item.key);
|
|
774
|
+
else
|
|
775
|
+
picked.add(item.key);
|
|
776
|
+
return { ...state, picked };
|
|
777
|
+
}
|
|
778
|
+
case "pick-all": {
|
|
779
|
+
if (!state.multiple)
|
|
780
|
+
return state;
|
|
781
|
+
const visible = state.lines.flatMap((line) => line.kind === "item" && !line.item.disabled ? [line.key] : []);
|
|
782
|
+
const picked = new Set(state.picked);
|
|
783
|
+
for (const key of visible) {
|
|
784
|
+
if (action.picked)
|
|
785
|
+
picked.add(key);
|
|
786
|
+
else
|
|
787
|
+
picked.delete(key);
|
|
788
|
+
}
|
|
789
|
+
return { ...state, picked };
|
|
790
|
+
}
|
|
791
|
+
default:
|
|
792
|
+
return { ...state, collection: reduceTerminalCollection2(state.collection, action) };
|
|
793
|
+
}
|
|
794
|
+
}
|
|
795
|
+
function selectedTerminalListItem2(state) {
|
|
796
|
+
return state.items.find(({ key }) => key === state.collection.selectedKey) ?? null;
|
|
797
|
+
}
|
|
798
|
+
function visibleTerminalListLines2(state, height) {
|
|
799
|
+
const rows = Math.max(1, height);
|
|
800
|
+
const index = state.lines.findIndex((line) => line.kind === "item" && line.key === state.collection.selectedKey);
|
|
801
|
+
const anchor = Math.max(0, index);
|
|
802
|
+
let start = Math.max(0, Math.min(anchor - Math.floor(rows / 2), state.lines.length - rows));
|
|
803
|
+
if (start > 0 && state.lines[start - 1]?.kind === "section" && anchor - start < rows - 1)
|
|
804
|
+
start -= 1;
|
|
805
|
+
const lines = state.lines.slice(start, start + rows);
|
|
806
|
+
return {
|
|
807
|
+
lines,
|
|
808
|
+
above: start,
|
|
809
|
+
below: Math.max(0, state.lines.length - start - lines.length)
|
|
810
|
+
};
|
|
811
|
+
}
|
|
812
|
+
|
|
813
|
+
// src/core/meter.ts
|
|
814
|
+
import { z as z5 } from "zod";
|
|
815
|
+
var EIGHTHS = ["", "\u258F", "\u258E", "\u258D", "\u258C", "\u258B", "\u258A", "\u2589"];
|
|
816
|
+
var FULL = "\u2588";
|
|
817
|
+
var TerminalMeterThresholdSchema2 = z5.object({
|
|
818
|
+
ratio: z5.number().min(0).max(1),
|
|
819
|
+
tone: TerminalToneSchema2
|
|
820
|
+
}).strict();
|
|
821
|
+
var defaultTerminalMeterThresholds2 = Object.freeze([
|
|
822
|
+
{ ratio: 0, tone: "success" },
|
|
823
|
+
{ ratio: 0.6, tone: "warning" },
|
|
824
|
+
{ ratio: 0.85, tone: "danger" }
|
|
825
|
+
]);
|
|
826
|
+
var MeterInputSchema = z5.object({
|
|
827
|
+
value: z5.number().nullable(),
|
|
828
|
+
max: z5.number().positive(),
|
|
829
|
+
width: z5.int().nonnegative(),
|
|
830
|
+
thresholds: z5.array(TerminalMeterThresholdSchema2).min(1).optional()
|
|
831
|
+
}).strict();
|
|
832
|
+
function toneFor(ratio, thresholds) {
|
|
833
|
+
const sorted = [...thresholds].sort((a, b) => a.ratio - b.ratio);
|
|
834
|
+
let tone = sorted[0]?.tone ?? "success";
|
|
835
|
+
for (const threshold of sorted) {
|
|
836
|
+
if (ratio >= threshold.ratio)
|
|
837
|
+
tone = threshold.tone;
|
|
838
|
+
}
|
|
839
|
+
return tone;
|
|
840
|
+
}
|
|
841
|
+
function terminalMeter2(input) {
|
|
842
|
+
const { value, max, width, thresholds } = MeterInputSchema.parse(input);
|
|
843
|
+
if (value === null) {
|
|
844
|
+
return { filled: "", empty: " ".repeat(width), ratio: null, tone: "muted" };
|
|
845
|
+
}
|
|
846
|
+
const ratio = Math.min(1, Math.max(0, value / max));
|
|
847
|
+
const eighths = Math.round(ratio * width * 8);
|
|
848
|
+
const whole = Math.floor(eighths / 8);
|
|
849
|
+
const partial = EIGHTHS[eighths % 8] ?? "";
|
|
850
|
+
const filled = FULL.repeat(whole) + partial;
|
|
851
|
+
const used = whole + (partial ? 1 : 0);
|
|
852
|
+
return {
|
|
853
|
+
filled,
|
|
854
|
+
empty: " ".repeat(width - used),
|
|
855
|
+
ratio,
|
|
856
|
+
tone: toneFor(ratio, thresholds ?? defaultTerminalMeterThresholds2)
|
|
857
|
+
};
|
|
858
|
+
}
|
|
859
|
+
var GridInputSchema = z5.object({
|
|
860
|
+
count: z5.int().nonnegative(),
|
|
861
|
+
width: z5.int().positive(),
|
|
862
|
+
minMeterWidth: z5.int().positive(),
|
|
863
|
+
gap: z5.int().nonnegative().default(1)
|
|
864
|
+
}).strict();
|
|
865
|
+
function layoutTerminalMeterGrid2(input) {
|
|
866
|
+
const { count, width, minMeterWidth, gap } = GridInputSchema.parse(input);
|
|
867
|
+
if (count === 0)
|
|
868
|
+
return { columns: 0, rows: 0, meterWidth: width, cells: [] };
|
|
869
|
+
const fit = Math.max(1, Math.floor((width + gap) / (minMeterWidth + gap)));
|
|
870
|
+
const rows = Math.ceil(count / Math.min(count, fit));
|
|
871
|
+
const columns = Math.ceil(count / rows);
|
|
872
|
+
const meterWidth = Math.max(1, Math.floor((width - gap * (columns - 1)) / columns));
|
|
873
|
+
const cells = Array.from({ length: rows }, (_, row) => Array.from({ length: columns }, (_, column) => column * rows + row).filter((index) => index < count));
|
|
874
|
+
return { columns, rows, meterWidth, cells };
|
|
875
|
+
}
|
|
876
|
+
|
|
877
|
+
// src/core/progress.ts
|
|
878
|
+
import { z as z6 } from "zod";
|
|
879
|
+
var TerminalSpinnerKindSchema2 = z6.enum(["dots", "line", "pulse", "arc"]);
|
|
880
|
+
var terminalSpinners2 = Object.freeze({
|
|
881
|
+
dots: { intervalMs: 80, frames: ["\u280B", "\u2819", "\u2839", "\u2838", "\u283C", "\u2834", "\u2826", "\u2827", "\u2807", "\u280F"] },
|
|
882
|
+
line: { intervalMs: 120, frames: ["-", "\\", "|", "/"] },
|
|
883
|
+
pulse: { intervalMs: 160, frames: ["\xB7", "\u2022", "\u25CF", "\u2022"] },
|
|
884
|
+
arc: { intervalMs: 100, frames: ["\u25DC", "\u25E0", "\u25DD", "\u25DE", "\u25E1", "\u25DF"] }
|
|
885
|
+
});
|
|
886
|
+
function terminalSpinnerFrame2(kind, elapsedMs) {
|
|
887
|
+
const spinner = terminalSpinners2[kind];
|
|
888
|
+
const index = Math.floor(Math.max(0, elapsedMs) / spinner.intervalMs) % spinner.frames.length;
|
|
889
|
+
return spinner.frames[index] ?? "";
|
|
890
|
+
}
|
|
891
|
+
function formatTerminalDuration2(ms) {
|
|
892
|
+
const value = Math.max(0, ms);
|
|
893
|
+
if (value < 1e4)
|
|
894
|
+
return `${(value / 1000).toFixed(1)}s`;
|
|
895
|
+
const seconds = Math.floor(value / 1000);
|
|
896
|
+
if (seconds < 60)
|
|
897
|
+
return `${seconds}s`;
|
|
898
|
+
const minutes = Math.floor(seconds / 60);
|
|
899
|
+
if (minutes < 60)
|
|
900
|
+
return `${minutes}m ${String(seconds % 60).padStart(2, "0")}s`;
|
|
901
|
+
return `${Math.floor(minutes / 60)}h ${String(minutes % 60).padStart(2, "0")}m`;
|
|
902
|
+
}
|
|
903
|
+
var ProgressInputSchema = z6.object({
|
|
904
|
+
done: z6.number().nonnegative().nullable(),
|
|
905
|
+
total: z6.number().positive().nullable(),
|
|
906
|
+
startedAt: z6.number(),
|
|
907
|
+
now: z6.number()
|
|
908
|
+
}).strict();
|
|
909
|
+
function terminalProgress2(input) {
|
|
910
|
+
const { done, total, startedAt, now } = ProgressInputSchema.parse(input);
|
|
911
|
+
const elapsedMs = Math.max(0, now - startedAt);
|
|
912
|
+
const ratio = done === null || total === null ? null : Math.min(1, done / total);
|
|
913
|
+
const rate = done === null || elapsedMs === 0 ? null : done * 1000 / elapsedMs;
|
|
914
|
+
const etaMs = done === null || total === null || rate === null || rate === 0 ? null : Math.max(0, (total - done) / rate * 1000);
|
|
915
|
+
return { ratio, elapsedMs, rate, etaMs };
|
|
916
|
+
}
|
|
917
|
+
function terminalIndeterminateBlock2(width, elapsedMs, periodMs = 1600) {
|
|
918
|
+
const length = Math.max(1, Math.floor(width / 4));
|
|
919
|
+
const travel = Math.max(0, width - length);
|
|
920
|
+
const phase = Math.max(0, elapsedMs) % periodMs / periodMs;
|
|
921
|
+
const position = phase < 0.5 ? phase * 2 : 2 - phase * 2;
|
|
922
|
+
return [Math.round(position * travel), length];
|
|
923
|
+
}
|
|
924
|
+
|
|
925
|
+
// src/core/table.ts
|
|
926
|
+
import { z as z7 } from "zod";
|
|
927
|
+
var TerminalTableColumnSchema2 = z7.object({
|
|
928
|
+
id: z7.string().min(1),
|
|
929
|
+
title: z7.string(),
|
|
930
|
+
kind: z7.enum(["number", "text"]),
|
|
931
|
+
width: z7.union([z7.int().positive(), z7.object({ flex: z7.int().positive() }).strict()]),
|
|
932
|
+
align: z7.enum(["left", "right"]).optional(),
|
|
933
|
+
sortable: z7.boolean().default(true),
|
|
934
|
+
filterable: z7.boolean().optional()
|
|
935
|
+
}).strict();
|
|
936
|
+
var TerminalTableRowSchema2 = z7.object({
|
|
937
|
+
key: z7.string().min(1),
|
|
938
|
+
parentKey: z7.string().min(1).nullable().default(null),
|
|
939
|
+
cells: z7.record(z7.string(), z7.union([z7.string(), z7.number(), z7.null()])),
|
|
940
|
+
tone: TerminalToneSchema2.optional()
|
|
941
|
+
}).strict();
|
|
942
|
+
var TerminalTableSortSchema2 = z7.object({ columnId: z7.string().min(1), direction: z7.enum(["asc", "desc"]) }).strict();
|
|
943
|
+
var collator = new Intl.Collator(undefined, { numeric: true, sensitivity: "base" });
|
|
944
|
+
function parseColumns(columns) {
|
|
945
|
+
if (columns.length === 0)
|
|
946
|
+
throw new Error("A terminal table needs at least one column");
|
|
947
|
+
const parsed = columns.map((column) => {
|
|
948
|
+
const { format, tone, ...shape } = column;
|
|
949
|
+
const value = TerminalTableColumnSchema2.parse(shape);
|
|
950
|
+
return {
|
|
951
|
+
...value,
|
|
952
|
+
...format !== undefined && { format },
|
|
953
|
+
...tone !== undefined && { tone }
|
|
954
|
+
};
|
|
955
|
+
});
|
|
956
|
+
if (new Set(parsed.map(({ id }) => id)).size !== parsed.length) {
|
|
957
|
+
throw new Error("Terminal table column ids must be unique");
|
|
958
|
+
}
|
|
959
|
+
return parsed;
|
|
960
|
+
}
|
|
961
|
+
function parseRows(columns, rows) {
|
|
962
|
+
const parsed = z7.array(TerminalTableRowSchema2).parse(rows);
|
|
963
|
+
const byKey = new Map;
|
|
964
|
+
for (const row of parsed) {
|
|
965
|
+
if (byKey.has(row.key))
|
|
966
|
+
throw new Error(`Terminal table row keys must be unique: ${row.key}`);
|
|
967
|
+
byKey.set(row.key, row);
|
|
968
|
+
for (const column of columns) {
|
|
969
|
+
if (!(column.id in row.cells)) {
|
|
970
|
+
throw new Error(`Terminal table row ${row.key} has no cell for column ${column.id}`);
|
|
971
|
+
}
|
|
972
|
+
const value = row.cells[column.id];
|
|
973
|
+
if (value !== null && typeof value !== (column.kind === "number" ? "number" : "string")) {
|
|
974
|
+
throw new Error(`Terminal table row ${row.key} holds a ${typeof value} in ${column.kind} column ${column.id}`);
|
|
975
|
+
}
|
|
976
|
+
}
|
|
977
|
+
}
|
|
978
|
+
for (const row of parsed) {
|
|
979
|
+
const seen = new Set([row.key]);
|
|
980
|
+
for (let parent = row.parentKey;parent !== null; ) {
|
|
981
|
+
if (seen.has(parent))
|
|
982
|
+
throw new Error(`Terminal table rows form a cycle through ${parent}`);
|
|
983
|
+
seen.add(parent);
|
|
984
|
+
parent = byKey.get(parent)?.parentKey ?? null;
|
|
985
|
+
}
|
|
986
|
+
}
|
|
987
|
+
return parsed;
|
|
988
|
+
}
|
|
989
|
+
function formatTerminalTableCell2(column, value) {
|
|
990
|
+
if (value === null)
|
|
991
|
+
return "\u2014";
|
|
992
|
+
return sanitizeTerminalText2(column.format ? column.format(value) : String(value)).replace(/\n/g, " ");
|
|
993
|
+
}
|
|
994
|
+
function sortColumn(state) {
|
|
995
|
+
if (state.sort === null)
|
|
996
|
+
return null;
|
|
997
|
+
return state.columns.find(({ id }) => id === state.sort?.columnId) ?? null;
|
|
998
|
+
}
|
|
999
|
+
function compareRows(column, direction) {
|
|
1000
|
+
return (a, b) => {
|
|
1001
|
+
if (column !== null) {
|
|
1002
|
+
const left = a.cells[column.id] ?? null;
|
|
1003
|
+
const right = b.cells[column.id] ?? null;
|
|
1004
|
+
if (left === null && right !== null)
|
|
1005
|
+
return 1;
|
|
1006
|
+
if (right === null && left !== null)
|
|
1007
|
+
return -1;
|
|
1008
|
+
if (left !== null && right !== null) {
|
|
1009
|
+
const order = typeof left === "number" && typeof right === "number" ? left - right : collator.compare(String(left), String(right));
|
|
1010
|
+
if (order !== 0)
|
|
1011
|
+
return direction === "asc" ? order : -order;
|
|
1012
|
+
}
|
|
1013
|
+
}
|
|
1014
|
+
return a.key < b.key ? -1 : a.key > b.key ? 1 : 0;
|
|
1015
|
+
};
|
|
1016
|
+
}
|
|
1017
|
+
function projectTerminalTable2(state) {
|
|
1018
|
+
const keys = new Set(state.rows.map(({ key }) => key));
|
|
1019
|
+
const children = new Map;
|
|
1020
|
+
for (const row of state.rows) {
|
|
1021
|
+
const parent = row.parentKey !== null && keys.has(row.parentKey) ? row.parentKey : null;
|
|
1022
|
+
const siblings = children.get(parent);
|
|
1023
|
+
if (siblings)
|
|
1024
|
+
siblings.push(row);
|
|
1025
|
+
else
|
|
1026
|
+
children.set(parent, [row]);
|
|
1027
|
+
}
|
|
1028
|
+
const compare = compareRows(sortColumn(state), state.sort?.direction ?? "asc");
|
|
1029
|
+
for (const siblings of children.values())
|
|
1030
|
+
siblings.sort(compare);
|
|
1031
|
+
const query = state.filter.trim().toLocaleLowerCase();
|
|
1032
|
+
const matched = new Set;
|
|
1033
|
+
const shown = new Set;
|
|
1034
|
+
if (query !== "") {
|
|
1035
|
+
const searched = state.columns.filter((column) => column.filterable ?? column.kind === "text");
|
|
1036
|
+
const byKey = new Map(state.rows.map((row) => [row.key, row]));
|
|
1037
|
+
for (const row of state.rows) {
|
|
1038
|
+
const hit = searched.some((column) => formatTerminalTableCell2(column, row.cells[column.id] ?? null).toLocaleLowerCase().includes(query));
|
|
1039
|
+
if (!hit)
|
|
1040
|
+
continue;
|
|
1041
|
+
matched.add(row.key);
|
|
1042
|
+
for (let key = row.key;key !== null && !shown.has(key); ) {
|
|
1043
|
+
shown.add(key);
|
|
1044
|
+
const parent = byKey.get(key)?.parentKey ?? null;
|
|
1045
|
+
key = parent !== null && keys.has(parent) ? parent : null;
|
|
1046
|
+
}
|
|
1047
|
+
}
|
|
1048
|
+
}
|
|
1049
|
+
const view = [];
|
|
1050
|
+
const visit = (parent, depth) => {
|
|
1051
|
+
for (const row of children.get(parent) ?? []) {
|
|
1052
|
+
if (query !== "" && !shown.has(row.key))
|
|
1053
|
+
continue;
|
|
1054
|
+
const hasChildren = (children.get(row.key) ?? []).some((child) => query === "" || shown.has(child.key));
|
|
1055
|
+
const expanded = hasChildren && (query !== "" || state.expanded.has(row.key));
|
|
1056
|
+
view.push({
|
|
1057
|
+
row,
|
|
1058
|
+
depth,
|
|
1059
|
+
hasChildren,
|
|
1060
|
+
expanded,
|
|
1061
|
+
matched: query === "" || matched.has(row.key)
|
|
1062
|
+
});
|
|
1063
|
+
if (expanded)
|
|
1064
|
+
visit(row.key, depth + 1);
|
|
1065
|
+
}
|
|
1066
|
+
};
|
|
1067
|
+
visit(null, 0);
|
|
1068
|
+
return view;
|
|
1069
|
+
}
|
|
1070
|
+
function withView(state, collection, fallback) {
|
|
1071
|
+
const view = projectTerminalTable2(state);
|
|
1072
|
+
const keys = view.map(({ row }) => row.key);
|
|
1073
|
+
const lost = collection.selectedKey !== null && !keys.includes(collection.selectedKey);
|
|
1074
|
+
let next = reduceTerminalCollection2(collection, { type: "reconcile", keys });
|
|
1075
|
+
if (lost && fallback !== undefined && keys.includes(fallback)) {
|
|
1076
|
+
next = reduceTerminalCollection2(next, { type: "select", key: fallback });
|
|
1077
|
+
}
|
|
1078
|
+
return { ...state, activation: state.activation ?? null, view, collection: next };
|
|
1079
|
+
}
|
|
1080
|
+
function createTerminalTable2(input) {
|
|
1081
|
+
const columns = parseColumns(input.columns);
|
|
1082
|
+
const rows = parseRows(columns, input.rows ?? []);
|
|
1083
|
+
const sort = input.sort ? TerminalTableSortSchema2.parse(input.sort) : null;
|
|
1084
|
+
if (sort && !columns.some(({ id, sortable }) => id === sort.columnId && sortable)) {
|
|
1085
|
+
throw new Error(`Terminal table cannot sort by column ${sort.columnId}`);
|
|
1086
|
+
}
|
|
1087
|
+
const keys = new Set(rows.map(({ key }) => key));
|
|
1088
|
+
return withView({
|
|
1089
|
+
columns,
|
|
1090
|
+
rows,
|
|
1091
|
+
sort,
|
|
1092
|
+
expanded: new Set((input.expanded ?? []).filter((key) => keys.has(key))),
|
|
1093
|
+
filter: ""
|
|
1094
|
+
}, createTerminalCollection2([], input.capacity));
|
|
1095
|
+
}
|
|
1096
|
+
function selectedRow(state, key) {
|
|
1097
|
+
const target = key ?? state.collection.selectedKey;
|
|
1098
|
+
return state.view.find(({ row }) => row.key === target) ?? null;
|
|
1099
|
+
}
|
|
1100
|
+
function reduceTerminalTable2(state, action) {
|
|
1101
|
+
const { collection } = state;
|
|
1102
|
+
const base = {
|
|
1103
|
+
columns: state.columns,
|
|
1104
|
+
rows: state.rows,
|
|
1105
|
+
sort: state.sort,
|
|
1106
|
+
expanded: state.expanded,
|
|
1107
|
+
filter: state.filter,
|
|
1108
|
+
activation: state.activation
|
|
1109
|
+
};
|
|
1110
|
+
switch (action.type) {
|
|
1111
|
+
case "rows": {
|
|
1112
|
+
const rows = parseRows(base.columns, action.rows);
|
|
1113
|
+
const keys = new Set(rows.map(({ key }) => key));
|
|
1114
|
+
const expanded = new Set([...base.expanded].filter((key) => keys.has(key)));
|
|
1115
|
+
return withView({ ...base, rows, expanded }, collection);
|
|
1116
|
+
}
|
|
1117
|
+
case "sort": {
|
|
1118
|
+
const column = base.columns.find(({ id }) => id === action.columnId);
|
|
1119
|
+
if (!column?.sortable)
|
|
1120
|
+
throw new Error(`Terminal table cannot sort by column ${action.columnId}`);
|
|
1121
|
+
const direction = action.direction ?? (base.sort?.columnId === column.id ? base.sort.direction === "asc" ? "desc" : "asc" : column.kind === "number" ? "desc" : "asc");
|
|
1122
|
+
return withView({ ...base, sort: { columnId: column.id, direction } }, collection);
|
|
1123
|
+
}
|
|
1124
|
+
case "sort-next": {
|
|
1125
|
+
const sortable = base.columns.filter(({ sortable }) => sortable);
|
|
1126
|
+
if (sortable.length === 0)
|
|
1127
|
+
return state;
|
|
1128
|
+
const index = sortable.findIndex(({ id }) => id === base.sort?.columnId);
|
|
1129
|
+
const step = z7.int().parse(action.delta);
|
|
1130
|
+
const next = sortable[(((index < 0 ? step > 0 ? -1 : 0 : index) + step) % sortable.length + sortable.length) % sortable.length];
|
|
1131
|
+
if (!next)
|
|
1132
|
+
return state;
|
|
1133
|
+
return withView({
|
|
1134
|
+
...base,
|
|
1135
|
+
sort: { columnId: next.id, direction: next.kind === "number" ? "desc" : "asc" }
|
|
1136
|
+
}, collection);
|
|
1137
|
+
}
|
|
1138
|
+
case "sort-invert": {
|
|
1139
|
+
if (base.sort === null)
|
|
1140
|
+
return state;
|
|
1141
|
+
return withView({
|
|
1142
|
+
...base,
|
|
1143
|
+
sort: { ...base.sort, direction: base.sort.direction === "asc" ? "desc" : "asc" }
|
|
1144
|
+
}, collection);
|
|
1145
|
+
}
|
|
1146
|
+
case "expand":
|
|
1147
|
+
case "collapse":
|
|
1148
|
+
case "toggle": {
|
|
1149
|
+
const target = selectedRow(state, action.key);
|
|
1150
|
+
if (target === null || state.filter.trim() !== "")
|
|
1151
|
+
return state;
|
|
1152
|
+
const open = action.type === "expand" ? true : action.type === "collapse" ? false : !target.expanded;
|
|
1153
|
+
if (!target.hasChildren || target.expanded === open) {
|
|
1154
|
+
if (action.type === "collapse" && action.key === undefined && target.row.parentKey) {
|
|
1155
|
+
const parent = state.view.find(({ row }) => row.key === target.row.parentKey);
|
|
1156
|
+
if (parent) {
|
|
1157
|
+
return {
|
|
1158
|
+
...state,
|
|
1159
|
+
collection: reduceTerminalCollection2(collection, {
|
|
1160
|
+
type: "select",
|
|
1161
|
+
key: parent.row.key
|
|
1162
|
+
})
|
|
1163
|
+
};
|
|
1164
|
+
}
|
|
1165
|
+
}
|
|
1166
|
+
return state;
|
|
1167
|
+
}
|
|
1168
|
+
const expanded = new Set(base.expanded);
|
|
1169
|
+
if (open)
|
|
1170
|
+
expanded.add(target.row.key);
|
|
1171
|
+
else
|
|
1172
|
+
expanded.delete(target.row.key);
|
|
1173
|
+
return withView({ ...base, expanded }, collection, open ? undefined : target.row.key);
|
|
1174
|
+
}
|
|
1175
|
+
case "filter":
|
|
1176
|
+
return withView({ ...base, filter: z7.string().parse(action.query) }, collection);
|
|
1177
|
+
case "activate": {
|
|
1178
|
+
const key = state.collection.selectedKey;
|
|
1179
|
+
if (key === null)
|
|
1180
|
+
return state;
|
|
1181
|
+
return { ...state, activation: { key, serial: (state.activation?.serial ?? 0) + 1 } };
|
|
1182
|
+
}
|
|
1183
|
+
case "filter-edit": {
|
|
1184
|
+
const filter = applyTerminalTextEdit2(state.filter, action.edit);
|
|
1185
|
+
return filter === state.filter ? state : withView({ ...base, filter }, collection);
|
|
1186
|
+
}
|
|
1187
|
+
default:
|
|
1188
|
+
return { ...state, collection: reduceTerminalCollection2(collection, action) };
|
|
1189
|
+
}
|
|
1190
|
+
}
|
|
1191
|
+
function visibleTerminalTableRows2(state) {
|
|
1192
|
+
const { start, capacity } = state.collection;
|
|
1193
|
+
return state.view.slice(start, start + capacity);
|
|
1194
|
+
}
|
|
1195
|
+
function selectedTerminalTableRow2(state) {
|
|
1196
|
+
return selectedRow(state)?.row ?? null;
|
|
1197
|
+
}
|
|
1198
|
+
function layoutTerminalTableColumns2(columns, width, gap = 1) {
|
|
1199
|
+
const total = z7.int().nonnegative().parse(width);
|
|
1200
|
+
let remaining = total;
|
|
1201
|
+
const widths = columns.map((column, index) => {
|
|
1202
|
+
if (typeof column.width !== "number")
|
|
1203
|
+
return { id: column.id, width: 0 };
|
|
1204
|
+
const needed = column.width + (index === 0 ? 0 : gap);
|
|
1205
|
+
if (needed > remaining)
|
|
1206
|
+
return { id: column.id, width: 0 };
|
|
1207
|
+
remaining -= needed;
|
|
1208
|
+
return { id: column.id, width: column.width };
|
|
1209
|
+
});
|
|
1210
|
+
const flexible = columns.map((column, index) => ({ column, index })).filter(({ column }) => typeof column.width !== "number");
|
|
1211
|
+
const gaps = flexible.filter(({ index }) => index > 0).length * gap;
|
|
1212
|
+
const share = Math.max(0, remaining - gaps);
|
|
1213
|
+
const weights = flexible.reduce((sum, { column }) => sum + (typeof column.width === "number" ? 0 : column.width.flex), 0);
|
|
1214
|
+
let given = 0;
|
|
1215
|
+
flexible.forEach(({ column, index }, position) => {
|
|
1216
|
+
if (typeof column.width === "number")
|
|
1217
|
+
return;
|
|
1218
|
+
const cells = position === flexible.length - 1 ? share - given : Math.floor(share * column.width.flex / Math.max(1, weights));
|
|
1219
|
+
given += cells;
|
|
1220
|
+
widths[index] = { id: column.id, width: Math.max(0, cells) };
|
|
1221
|
+
});
|
|
1222
|
+
return widths;
|
|
1223
|
+
}
|
|
1224
|
+
function terminalTableTreePrefix2(row) {
|
|
1225
|
+
const mark = row.hasChildren ? row.expanded ? "\u25BE " : "\u25B8 " : " ";
|
|
1226
|
+
return " ".repeat(row.depth) + mark;
|
|
1227
|
+
}
|
|
1228
|
+
|
|
1229
|
+
export { matchTerminalSearch2, searchTerminalItems2, terminalHighlightRuns2, createTerminalTimeSeries2, appendTerminalSample2, sampleTerminalTimeSeries2, terminalChartScale2, terminalSparkline2, terminalBrailleChart2, terminalTimeAxis2, TerminalCollectionStateSchema2, createTerminalCollection2, reduceTerminalCollection2, visibleTerminalCollectionRange2, terminalJsonLines2, layoutTerminalDetails2, TerminalKeyBindingSchema2, defineTerminalKeymap2, matchTerminalKey2, fitTerminalKeyHints2, terminalTableKeymap2, terminalTableKey2, terminalTabsKeymap2, terminalTabsKey2, TerminalListItemSchema2, createTerminalList2, reduceTerminalList2, selectedTerminalListItem2, visibleTerminalListLines2, TerminalMeterThresholdSchema2, defaultTerminalMeterThresholds2, terminalMeter2, layoutTerminalMeterGrid2, TerminalSpinnerKindSchema2, terminalSpinners2, terminalSpinnerFrame2, formatTerminalDuration2, terminalProgress2, terminalIndeterminateBlock2, TerminalTableColumnSchema2, TerminalTableRowSchema2, TerminalTableSortSchema2, formatTerminalTableCell2, projectTerminalTable2, createTerminalTable2, reduceTerminalTable2, visibleTerminalTableRows2, selectedTerminalTableRow2, layoutTerminalTableColumns2, terminalTableTreePrefix2 };
|