@bypane/cli 0.0.0-stage → 0.2.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/LICENSE +21 -0
- package/README.md +188 -2
- package/dist/app-host.js +408 -0
- package/dist/app-protocol.js +1 -0
- package/dist/app-worker.js +556 -0
- package/dist/cli.js +810 -0
- package/dist/config.js +42 -0
- package/dist/demo.js +430 -0
- package/dist/expr.js +460 -0
- package/dist/ipc.js +201 -0
- package/dist/mcp.js +416 -0
- package/dist/multiplexer.js +281 -0
- package/dist/png.js +208 -0
- package/dist/primitives.js +669 -0
- package/dist/selection.js +170 -0
- package/dist/setup.js +329 -0
- package/dist/skill.js +219 -0
- package/dist/state.js +1147 -0
- package/dist/types.js +1 -0
- package/dist/ui-surface.js +744 -0
- package/dist/ui.js +1466 -0
- package/examples/spring-studio.ts +420 -0
- package/package.json +58 -4
- package/skills/bypane/SKILL.md +136 -0
|
@@ -0,0 +1,669 @@
|
|
|
1
|
+
import { controllable, definePrimitive, drawMeter, paint, text, truncate, widthOf, } from "tui";
|
|
2
|
+
function isSpace(key) {
|
|
3
|
+
return key.name === "char" && key.text === " ";
|
|
4
|
+
}
|
|
5
|
+
function clamp(v, lo, hi) {
|
|
6
|
+
return Math.max(lo, Math.min(hi, v));
|
|
7
|
+
}
|
|
8
|
+
function snapStep(value, min, max, step) {
|
|
9
|
+
const safeStep = step > 0 ? step : 1;
|
|
10
|
+
const steps = Math.round((value - min) / safeStep);
|
|
11
|
+
const snapped = min + steps * safeStep;
|
|
12
|
+
const clamped = clamp(snapped, min, max);
|
|
13
|
+
return Math.round(clamped * 10000) / 10000;
|
|
14
|
+
}
|
|
15
|
+
const COMPACT_UNITS = new Set(["%", "s", "ms", "us", "µs", "m", "h", "d", "x", "×"]);
|
|
16
|
+
function formatNum(v, unit) {
|
|
17
|
+
const numStr = Number.isInteger(v) ? String(v) : String(Math.round(v * 100) / 100);
|
|
18
|
+
if (!unit)
|
|
19
|
+
return numStr;
|
|
20
|
+
return COMPACT_UNITS.has(unit) ? `${numStr}${unit}` : `${numStr} ${unit}`;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Interactive terminal slider primitive built on `tui`'s `definePrimitive` + `drawMeter`.
|
|
24
|
+
* Supports keyboard (`←/→`, `h/l`, `Shift+←/→`, `Home/End`), mouse click/drag, and `tui/agent`.
|
|
25
|
+
*/
|
|
26
|
+
export const Slider = definePrimitive({
|
|
27
|
+
name: "slider",
|
|
28
|
+
state: (context) => ({
|
|
29
|
+
value: controllable(context, "value", (props) => props.value, context.props().defaultValue ?? context.props().min, (props, value, details) => props.onValueChange?.(value, details)),
|
|
30
|
+
}),
|
|
31
|
+
api: (context, state) => {
|
|
32
|
+
function bounds() {
|
|
33
|
+
const p = context.props();
|
|
34
|
+
const min = Number.isFinite(p.min) ? p.min : 0;
|
|
35
|
+
const max = Number.isFinite(p.max) && p.max > min ? p.max : min + 100;
|
|
36
|
+
const step = p.step && p.step > 0 ? p.step : 1;
|
|
37
|
+
return { min, max, step };
|
|
38
|
+
}
|
|
39
|
+
function value() {
|
|
40
|
+
const { min, max, step } = bounds();
|
|
41
|
+
return snapStep(state.value.get(), min, max, step);
|
|
42
|
+
}
|
|
43
|
+
function set(next, reason, key = null) {
|
|
44
|
+
const { min, max, step } = bounds();
|
|
45
|
+
const clamped = snapStep(next, min, max, step);
|
|
46
|
+
return clamped !== value() && state.value.set(clamped, reason, key);
|
|
47
|
+
}
|
|
48
|
+
function nudge(deltaSteps, key) {
|
|
49
|
+
const { step } = bounds();
|
|
50
|
+
return set(value() + deltaSteps * step, "slider-step", key);
|
|
51
|
+
}
|
|
52
|
+
function onKey(key) {
|
|
53
|
+
if (key.ctrl || key.alt)
|
|
54
|
+
return false;
|
|
55
|
+
const mult = key.shift ? 5 : 1;
|
|
56
|
+
if (key.name === "left" || (key.name === "char" && key.text === "h")) {
|
|
57
|
+
nudge(-mult, key);
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
60
|
+
if (key.name === "right" || (key.name === "char" && key.text === "l")) {
|
|
61
|
+
nudge(mult, key);
|
|
62
|
+
return true;
|
|
63
|
+
}
|
|
64
|
+
if (key.name === "pageup") {
|
|
65
|
+
nudge(5, key);
|
|
66
|
+
return true;
|
|
67
|
+
}
|
|
68
|
+
if (key.name === "pagedown") {
|
|
69
|
+
nudge(-5, key);
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
72
|
+
if (key.name === "home") {
|
|
73
|
+
set(bounds().min, "slider-home", key);
|
|
74
|
+
return true;
|
|
75
|
+
}
|
|
76
|
+
if (key.name === "end") {
|
|
77
|
+
set(bounds().max, "slider-end", key);
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
return { bounds, value, set, nudge, onKey };
|
|
83
|
+
},
|
|
84
|
+
agent: (root) => {
|
|
85
|
+
const { min, max } = root.api.bounds();
|
|
86
|
+
return {
|
|
87
|
+
state: { value: root.api.value(), min, max },
|
|
88
|
+
items: [],
|
|
89
|
+
actions: {
|
|
90
|
+
set: (args) => {
|
|
91
|
+
const parsed = Number(args[0]);
|
|
92
|
+
if (!Number.isFinite(parsed))
|
|
93
|
+
return `invalid number "${args[0] ?? ""}"`;
|
|
94
|
+
root.api.set(parsed, "agent-set", null);
|
|
95
|
+
return "";
|
|
96
|
+
},
|
|
97
|
+
},
|
|
98
|
+
};
|
|
99
|
+
},
|
|
100
|
+
slots: (slot, Root) => {
|
|
101
|
+
const Track = slot("track", {
|
|
102
|
+
focusable: true,
|
|
103
|
+
keys: (key, part) => part.api.onKey(key),
|
|
104
|
+
mouse: (key, part) => {
|
|
105
|
+
const m = key.mouse;
|
|
106
|
+
if (!m || m.button !== "left" || (m.action !== "press" && m.action !== "drag")) {
|
|
107
|
+
return false;
|
|
108
|
+
}
|
|
109
|
+
const rect = part.rect;
|
|
110
|
+
const { min, max } = part.api.bounds();
|
|
111
|
+
const twoRow = rect.rows >= 2;
|
|
112
|
+
const label = part.props.label ?? "";
|
|
113
|
+
const valText = formatNum(part.api.value(), part.props.unit);
|
|
114
|
+
const trackX = twoRow
|
|
115
|
+
? rect.x + 2
|
|
116
|
+
: rect.x + Math.min(Math.max(12, widthOf(label) + 3), Math.floor(rect.columns * 0.45));
|
|
117
|
+
const trackW = twoRow
|
|
118
|
+
? Math.max(6, rect.columns - 3)
|
|
119
|
+
: Math.max(6, rect.x + rect.columns - trackX - widthOf(valText) - 2);
|
|
120
|
+
if (m.x < trackX - 1 || m.x > trackX + trackW)
|
|
121
|
+
return false;
|
|
122
|
+
const frac = clamp((m.x - trackX) / Math.max(1, trackW - 1), 0, 1);
|
|
123
|
+
part.api.set(min + frac * (max - min), "slider-mouse", key);
|
|
124
|
+
return true;
|
|
125
|
+
},
|
|
126
|
+
draw: (part) => {
|
|
127
|
+
const { frame, rect, props, focused } = part;
|
|
128
|
+
if (rect.columns < 8 || rect.rows < 1)
|
|
129
|
+
return;
|
|
130
|
+
const { min, max } = part.api.bounds();
|
|
131
|
+
const val = part.api.value();
|
|
132
|
+
const ratio = clamp((val - min) / Math.max(1e-9, max - min), 0, 1);
|
|
133
|
+
const valStr = formatNum(val, props.unit);
|
|
134
|
+
const valW = widthOf(valStr);
|
|
135
|
+
const label = props.label ?? "";
|
|
136
|
+
const prefix = focused ? "▸ " : " ";
|
|
137
|
+
const labelStyle = focused ? frame.theme.primary : frame.theme.heading;
|
|
138
|
+
const valStyle = focused ? frame.theme.primary : frame.theme.code;
|
|
139
|
+
if (rect.rows >= 2) {
|
|
140
|
+
text(frame, rect.x, rect.y, `${prefix}${truncate(label, Math.max(4, rect.columns - valW - 4))}`, labelStyle, rect);
|
|
141
|
+
text(frame, rect.x + rect.columns - valW, rect.y, valStr, valStyle, rect);
|
|
142
|
+
const barRect = {
|
|
143
|
+
x: rect.x + 2,
|
|
144
|
+
y: rect.y + 1,
|
|
145
|
+
columns: Math.max(6, rect.columns - 3),
|
|
146
|
+
rows: 1,
|
|
147
|
+
};
|
|
148
|
+
drawMeter(frame, barRect, ratio, {
|
|
149
|
+
style: focused ? { fg: "#7aa2ff", bold: true } : { fg: "#5fd787" },
|
|
150
|
+
track: { fg: "#2d364a" },
|
|
151
|
+
});
|
|
152
|
+
const thumbX = barRect.x + Math.round(ratio * Math.max(0, barRect.columns - 1));
|
|
153
|
+
text(frame, thumbX, barRect.y, focused ? "◆" : "●", focused ? { fg: "#e6edf7", bold: true } : { fg: "#7aa2ff" }, barRect);
|
|
154
|
+
}
|
|
155
|
+
else {
|
|
156
|
+
const labelW = Math.min(Math.max(10, widthOf(label) + 2), Math.floor(rect.columns * 0.42));
|
|
157
|
+
text(frame, rect.x, rect.y, `${prefix}${truncate(label, Math.max(3, labelW - 2))}`, labelStyle, rect);
|
|
158
|
+
const barX = rect.x + labelW + 1;
|
|
159
|
+
const barW = Math.max(6, rect.columns - labelW - valW - 3);
|
|
160
|
+
const barRect = { x: barX, y: rect.y, columns: barW, rows: 1 };
|
|
161
|
+
drawMeter(frame, barRect, ratio, {
|
|
162
|
+
style: focused ? { fg: "#7aa2ff", bold: true } : { fg: "#5fd787" },
|
|
163
|
+
track: { fg: "#2d364a" },
|
|
164
|
+
});
|
|
165
|
+
const thumbX = barRect.x + Math.round(ratio * Math.max(0, barRect.columns - 1));
|
|
166
|
+
text(frame, thumbX, barRect.y, focused ? "◆" : "●", focused ? { fg: "#e6edf7", bold: true } : { fg: "#7aa2ff" }, barRect);
|
|
167
|
+
text(frame, rect.x + rect.columns - valW, rect.y, valStr, valStyle, rect);
|
|
168
|
+
}
|
|
169
|
+
},
|
|
170
|
+
});
|
|
171
|
+
return { Root, Track };
|
|
172
|
+
},
|
|
173
|
+
});
|
|
174
|
+
/**
|
|
175
|
+
* Interactive boolean toggle pill (`[● ON]` / `[○ OFF]`).
|
|
176
|
+
*/
|
|
177
|
+
export const Toggle = definePrimitive({
|
|
178
|
+
name: "toggle",
|
|
179
|
+
state: (context) => ({
|
|
180
|
+
value: controllable(context, "value", (props) => props.value, context.props().defaultValue ?? false, (props, value, details) => props.onValueChange?.(value, details)),
|
|
181
|
+
}),
|
|
182
|
+
api: (_context, state) => {
|
|
183
|
+
function value() {
|
|
184
|
+
return Boolean(state.value.get());
|
|
185
|
+
}
|
|
186
|
+
function set(next, reason, key = null) {
|
|
187
|
+
return next !== value() && state.value.set(next, reason, key);
|
|
188
|
+
}
|
|
189
|
+
function toggle(key = null) {
|
|
190
|
+
return set(!value(), key?.mouse ? "mouse-toggle" : "keyboard-toggle", key);
|
|
191
|
+
}
|
|
192
|
+
return { value, set, toggle };
|
|
193
|
+
},
|
|
194
|
+
agent: (root) => ({
|
|
195
|
+
state: { value: root.api.value() },
|
|
196
|
+
items: [],
|
|
197
|
+
actions: {
|
|
198
|
+
toggle: () => {
|
|
199
|
+
root.api.toggle(null);
|
|
200
|
+
return "";
|
|
201
|
+
},
|
|
202
|
+
set: (args) => {
|
|
203
|
+
const raw = (args[0] ?? "").toLowerCase();
|
|
204
|
+
if (raw !== "true" && raw !== "false")
|
|
205
|
+
return `expected true/false, got "${args[0] ?? ""}"`;
|
|
206
|
+
root.api.set(raw === "true", "agent-set", null);
|
|
207
|
+
return "";
|
|
208
|
+
},
|
|
209
|
+
},
|
|
210
|
+
}),
|
|
211
|
+
slots: (slot, Root) => {
|
|
212
|
+
const Switch = slot("switch", {
|
|
213
|
+
focusable: true,
|
|
214
|
+
keys: (key, part) => {
|
|
215
|
+
if (key.ctrl || key.alt)
|
|
216
|
+
return false;
|
|
217
|
+
if (isSpace(key) || key.name === "enter" || key.name === "left" || key.name === "right") {
|
|
218
|
+
part.api.toggle(key);
|
|
219
|
+
return true;
|
|
220
|
+
}
|
|
221
|
+
return false;
|
|
222
|
+
},
|
|
223
|
+
mouse: (key, part) => {
|
|
224
|
+
const m = key.mouse;
|
|
225
|
+
if (!m || m.action !== "press" || m.button !== "left")
|
|
226
|
+
return false;
|
|
227
|
+
part.api.toggle(key);
|
|
228
|
+
return true;
|
|
229
|
+
},
|
|
230
|
+
draw: (part) => {
|
|
231
|
+
const { frame, rect, props, focused } = part;
|
|
232
|
+
if (rect.columns < 8 || rect.rows < 1)
|
|
233
|
+
return;
|
|
234
|
+
const on = part.api.value();
|
|
235
|
+
const onLbl = props.onLabel || "ON";
|
|
236
|
+
const offLbl = props.offLabel || "OFF";
|
|
237
|
+
const pill = on ? `[● ${onLbl}]` : `[○ ${offLbl}]`;
|
|
238
|
+
const prefix = focused ? "▸ " : " ";
|
|
239
|
+
let x = text(frame, rect.x, rect.y, prefix, focused ? frame.theme.primary : frame.theme.muted, rect);
|
|
240
|
+
const pillStyle = focused
|
|
241
|
+
? on
|
|
242
|
+
? { fg: "#0e131d", bg: "#5fd787", bold: true }
|
|
243
|
+
: frame.theme.highlight
|
|
244
|
+
: on
|
|
245
|
+
? frame.theme.success
|
|
246
|
+
: frame.theme.muted;
|
|
247
|
+
x = text(frame, x, rect.y, pill, pillStyle, rect);
|
|
248
|
+
x = text(frame, x + 2, rect.y, props.label, focused ? frame.theme.primary : frame.theme.heading, rect);
|
|
249
|
+
if (props.description && rect.x + rect.columns - x > 10) {
|
|
250
|
+
text(frame, x + 2, rect.y, truncate(props.description, Math.max(4, rect.x + rect.columns - x - 2)), frame.theme.muted, rect);
|
|
251
|
+
}
|
|
252
|
+
},
|
|
253
|
+
});
|
|
254
|
+
return { Root, Switch };
|
|
255
|
+
},
|
|
256
|
+
});
|
|
257
|
+
/**
|
|
258
|
+
* Horizontal inline pill selector (`[ us-east ] eu-west global`).
|
|
259
|
+
*/
|
|
260
|
+
export const Segmented = definePrimitive({
|
|
261
|
+
name: "segmented",
|
|
262
|
+
state: (context) => ({
|
|
263
|
+
value: controllable(context, "value", (props) => props.value, context.props().defaultValue ?? context.props().options[0]?.value ?? "", (props, value, details) => props.onValueChange?.(value, details)),
|
|
264
|
+
}),
|
|
265
|
+
api: (context, state) => {
|
|
266
|
+
function options() {
|
|
267
|
+
return context.props().options;
|
|
268
|
+
}
|
|
269
|
+
function value() {
|
|
270
|
+
const cur = state.value.get();
|
|
271
|
+
const opts = options();
|
|
272
|
+
return opts.some((o) => o.value === cur) ? cur : (opts[0]?.value ?? "");
|
|
273
|
+
}
|
|
274
|
+
function select(next, key = null) {
|
|
275
|
+
const reason = key?.mouse ? "item-press" : key ? "list-navigation" : "imperative";
|
|
276
|
+
return next !== value() && state.value.set(next, reason, key);
|
|
277
|
+
}
|
|
278
|
+
function onKey(key) {
|
|
279
|
+
const opts = options();
|
|
280
|
+
if (!opts.length || key.ctrl || key.alt)
|
|
281
|
+
return false;
|
|
282
|
+
const idx = Math.max(0, opts.findIndex((o) => o.value === value()));
|
|
283
|
+
if (key.name === "left" || (key.name === "char" && key.text === "h")) {
|
|
284
|
+
select(opts[(idx - 1 + opts.length) % opts.length].value, key);
|
|
285
|
+
return true;
|
|
286
|
+
}
|
|
287
|
+
if (key.name === "right" || isSpace(key) || (key.name === "char" && key.text === "l")) {
|
|
288
|
+
select(opts[(idx + 1) % opts.length].value, key);
|
|
289
|
+
return true;
|
|
290
|
+
}
|
|
291
|
+
if (key.name === "home") {
|
|
292
|
+
select(opts[0].value, key);
|
|
293
|
+
return true;
|
|
294
|
+
}
|
|
295
|
+
if (key.name === "end") {
|
|
296
|
+
select(opts[opts.length - 1].value, key);
|
|
297
|
+
return true;
|
|
298
|
+
}
|
|
299
|
+
return false;
|
|
300
|
+
}
|
|
301
|
+
return { options, value, select, onKey };
|
|
302
|
+
},
|
|
303
|
+
agent: (root) => ({
|
|
304
|
+
state: { value: root.api.value() },
|
|
305
|
+
items: root.api.options(),
|
|
306
|
+
actions: {
|
|
307
|
+
select: (args) => {
|
|
308
|
+
const next = args[0] ?? "";
|
|
309
|
+
if (!root.api.options().some((o) => o.value === next))
|
|
310
|
+
return `no option "${next}"`;
|
|
311
|
+
root.api.select(next, null);
|
|
312
|
+
return "";
|
|
313
|
+
},
|
|
314
|
+
},
|
|
315
|
+
}),
|
|
316
|
+
slots: (slot, Root) => {
|
|
317
|
+
const Group = slot("group", {
|
|
318
|
+
focusable: true,
|
|
319
|
+
keys: (key, part) => part.api.onKey(key),
|
|
320
|
+
mouse: (key, part) => {
|
|
321
|
+
const m = key.mouse;
|
|
322
|
+
if (!m || m.action !== "press" || m.button !== "left")
|
|
323
|
+
return false;
|
|
324
|
+
const rect = part.rect;
|
|
325
|
+
const opts = part.api.options();
|
|
326
|
+
const twoRow = Boolean(part.props.label) && rect.rows >= 2;
|
|
327
|
+
const y = twoRow ? rect.y + 1 : rect.y;
|
|
328
|
+
if (m.y !== y)
|
|
329
|
+
return false;
|
|
330
|
+
let col = twoRow
|
|
331
|
+
? rect.x + 2
|
|
332
|
+
: rect.x + (part.props.label ? widthOf(part.props.label) + 4 : 2);
|
|
333
|
+
for (const opt of opts) {
|
|
334
|
+
const w = widthOf(` ${opt.label} `);
|
|
335
|
+
if (m.x >= col && m.x < col + w) {
|
|
336
|
+
part.api.select(opt.value, key);
|
|
337
|
+
return true;
|
|
338
|
+
}
|
|
339
|
+
col += w + 1;
|
|
340
|
+
}
|
|
341
|
+
return false;
|
|
342
|
+
},
|
|
343
|
+
draw: (part) => {
|
|
344
|
+
const { frame, rect, props, focused } = part;
|
|
345
|
+
if (rect.columns < 8 || rect.rows < 1)
|
|
346
|
+
return;
|
|
347
|
+
const opts = part.api.options();
|
|
348
|
+
const active = part.api.value();
|
|
349
|
+
const prefix = focused ? "▸ " : " ";
|
|
350
|
+
const twoRow = Boolean(props.label) && rect.rows >= 2;
|
|
351
|
+
let x = rect.x;
|
|
352
|
+
let y = rect.y;
|
|
353
|
+
if (props.label) {
|
|
354
|
+
x = text(frame, x, y, `${prefix}${props.label}`, focused ? frame.theme.primary : frame.theme.heading, rect);
|
|
355
|
+
if (twoRow) {
|
|
356
|
+
y = rect.y + 1;
|
|
357
|
+
x = rect.x + 2;
|
|
358
|
+
}
|
|
359
|
+
else {
|
|
360
|
+
x += 2;
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
else {
|
|
364
|
+
x = text(frame, x, y, prefix, focused ? frame.theme.primary : frame.theme.muted, rect);
|
|
365
|
+
}
|
|
366
|
+
for (const opt of opts) {
|
|
367
|
+
const label = ` ${opt.label} `;
|
|
368
|
+
const isSelected = opt.value === active;
|
|
369
|
+
const style = isSelected
|
|
370
|
+
? focused
|
|
371
|
+
? frame.theme.highlight
|
|
372
|
+
: { fg: "#e6edf7", bg: "#273552", bold: true }
|
|
373
|
+
: { fg: "#8b93a6", bg: "#171c26" };
|
|
374
|
+
x = text(frame, x, y, label, style, rect) + 1;
|
|
375
|
+
if (x >= rect.x + rect.columns)
|
|
376
|
+
break;
|
|
377
|
+
}
|
|
378
|
+
},
|
|
379
|
+
});
|
|
380
|
+
return { Root, Group };
|
|
381
|
+
},
|
|
382
|
+
});
|
|
383
|
+
function statusGlyph(status) {
|
|
384
|
+
switch (status) {
|
|
385
|
+
case "done":
|
|
386
|
+
return "✓";
|
|
387
|
+
case "active":
|
|
388
|
+
return "◉";
|
|
389
|
+
case "warn":
|
|
390
|
+
return "▲";
|
|
391
|
+
case "error":
|
|
392
|
+
return "×";
|
|
393
|
+
default:
|
|
394
|
+
return "○";
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
function statusStyle(status) {
|
|
398
|
+
switch (status) {
|
|
399
|
+
case "done":
|
|
400
|
+
return { fg: "#5fd787", bold: true };
|
|
401
|
+
case "active":
|
|
402
|
+
return { fg: "#7aa2ff", bold: true };
|
|
403
|
+
case "warn":
|
|
404
|
+
return { fg: "#f5a623", bold: true };
|
|
405
|
+
case "error":
|
|
406
|
+
return { fg: "#ff7a90", bold: true };
|
|
407
|
+
default:
|
|
408
|
+
return { fg: "#8b93a6" };
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
/**
|
|
412
|
+
* Interactive timeline primitive supporting both `"steps"` (vertical milestone rail)
|
|
413
|
+
* and `"gantt"` (proportional horizontal span waterfall with ruler).
|
|
414
|
+
*/
|
|
415
|
+
export const Timeline = definePrimitive({
|
|
416
|
+
name: "timeline",
|
|
417
|
+
state: (context) => ({
|
|
418
|
+
value: controllable(context, "value", (props) => props.value, context.props().defaultValue ?? context.props().items[0]?.id ?? "", (props, value, details) => props.onValueChange?.(value, details)),
|
|
419
|
+
}),
|
|
420
|
+
api: (context, state) => {
|
|
421
|
+
function items() {
|
|
422
|
+
return context.props().items;
|
|
423
|
+
}
|
|
424
|
+
function value() {
|
|
425
|
+
const cur = state.value.get();
|
|
426
|
+
const list = items();
|
|
427
|
+
return list.some((it) => it.id === cur) ? cur : (list[0]?.id ?? "");
|
|
428
|
+
}
|
|
429
|
+
function select(next, key = null) {
|
|
430
|
+
const reason = key?.mouse ? "item-press" : key ? "list-navigation" : "imperative";
|
|
431
|
+
return next !== value() && state.value.set(next, reason, key);
|
|
432
|
+
}
|
|
433
|
+
function onKey(key) {
|
|
434
|
+
const list = items();
|
|
435
|
+
if (!list.length || key.ctrl || key.alt)
|
|
436
|
+
return false;
|
|
437
|
+
const idx = Math.max(0, list.findIndex((it) => it.id === value()));
|
|
438
|
+
if (key.name === "up" ||
|
|
439
|
+
key.name === "left" ||
|
|
440
|
+
(key.name === "char" && (key.text === "k" || key.text === "h"))) {
|
|
441
|
+
select(list[Math.max(0, idx - 1)].id, key);
|
|
442
|
+
return true;
|
|
443
|
+
}
|
|
444
|
+
if (key.name === "down" ||
|
|
445
|
+
key.name === "right" ||
|
|
446
|
+
(key.name === "char" && (key.text === "j" || key.text === "l"))) {
|
|
447
|
+
select(list[Math.min(list.length - 1, idx + 1)].id, key);
|
|
448
|
+
return true;
|
|
449
|
+
}
|
|
450
|
+
if (key.name === "home") {
|
|
451
|
+
select(list[0].id, key);
|
|
452
|
+
return true;
|
|
453
|
+
}
|
|
454
|
+
if (key.name === "end") {
|
|
455
|
+
select(list[list.length - 1].id, key);
|
|
456
|
+
return true;
|
|
457
|
+
}
|
|
458
|
+
return false;
|
|
459
|
+
}
|
|
460
|
+
return { items, value, select, onKey };
|
|
461
|
+
},
|
|
462
|
+
agent: (root) => ({
|
|
463
|
+
state: { value: root.api.value() },
|
|
464
|
+
items: root.api.items().map((it) => ({ value: it.id, label: it.label })),
|
|
465
|
+
actions: {
|
|
466
|
+
select: (args) => {
|
|
467
|
+
const next = args[0] ?? "";
|
|
468
|
+
if (!root.api.items().some((it) => it.id === next))
|
|
469
|
+
return `no timeline item "${next}"`;
|
|
470
|
+
root.api.select(next, null);
|
|
471
|
+
return "";
|
|
472
|
+
},
|
|
473
|
+
},
|
|
474
|
+
}),
|
|
475
|
+
slots: (slot, Root) => {
|
|
476
|
+
const Track = slot("track", {
|
|
477
|
+
focusable: true,
|
|
478
|
+
keys: (key, part) => part.api.onKey(key),
|
|
479
|
+
mouse: (key, part) => {
|
|
480
|
+
const m = key.mouse;
|
|
481
|
+
if (!m || m.action !== "press" || m.button !== "left")
|
|
482
|
+
return false;
|
|
483
|
+
const list = part.api.items();
|
|
484
|
+
const rowOffset = m.y - part.rect.y;
|
|
485
|
+
const spaced = part.props.mode === "steps" && part.rect.rows >= list.length * 2 - 1;
|
|
486
|
+
const idx = spaced ? Math.floor(rowOffset / 2) : rowOffset;
|
|
487
|
+
if (idx >= 0 && idx < list.length) {
|
|
488
|
+
part.api.select(list[idx].id, key);
|
|
489
|
+
return true;
|
|
490
|
+
}
|
|
491
|
+
return false;
|
|
492
|
+
},
|
|
493
|
+
draw: (part) => {
|
|
494
|
+
const { frame, rect, props, focused } = part;
|
|
495
|
+
const list = part.api.items();
|
|
496
|
+
if (rect.columns < 12 || rect.rows < 1 || list.length === 0)
|
|
497
|
+
return;
|
|
498
|
+
const activeId = part.api.value();
|
|
499
|
+
if (props.mode === "gantt") {
|
|
500
|
+
const starts = list.map((it, i) => it.start ?? i);
|
|
501
|
+
const ends = list.map((it, i) => it.end ?? (it.start ?? i) + 1);
|
|
502
|
+
const minV = props.min ?? Math.min(...starts);
|
|
503
|
+
const maxV = props.max ?? Math.max(...ends, minV + 1);
|
|
504
|
+
const span = Math.max(1e-9, maxV - minV);
|
|
505
|
+
const showRuler = rect.rows > list.length;
|
|
506
|
+
const maxVisible = showRuler ? list.length : Math.min(list.length, rect.rows);
|
|
507
|
+
const labelW = Math.max(12, Math.min(22, Math.floor(rect.columns * 0.34)));
|
|
508
|
+
const metaW = Math.max(6, Math.min(12, Math.floor(rect.columns * 0.16)));
|
|
509
|
+
const barX = rect.x + labelW + 1;
|
|
510
|
+
const barW = Math.max(8, rect.columns - labelW - metaW - 3);
|
|
511
|
+
for (let i = 0; i < maxVisible; i++) {
|
|
512
|
+
const it = list[i];
|
|
513
|
+
const y = rect.y + i;
|
|
514
|
+
const isSel = it.id === activeId;
|
|
515
|
+
if (isSel) {
|
|
516
|
+
paint(frame, { x: rect.x, y, columns: rect.columns, rows: 1 }, { bg: focused ? "#1e2c48" : "#171f30" });
|
|
517
|
+
}
|
|
518
|
+
const glyph = statusGlyph(it.status);
|
|
519
|
+
const sStyle = statusStyle(it.status);
|
|
520
|
+
text(frame, rect.x, y, isSel ? "▸" : " ", focused ? frame.theme.primary : frame.theme.muted, rect);
|
|
521
|
+
text(frame, rect.x + 1, y, glyph, sStyle, rect);
|
|
522
|
+
text(frame, rect.x + 3, y, truncate(it.label, Math.max(4, labelW - 3)), isSel ? { fg: "#e6edf7", bold: true, bg: focused ? "#1e2c48" : "#171f30" } : frame.theme.heading, rect);
|
|
523
|
+
text(frame, barX, y, "┄".repeat(barW), { fg: "#2a3242" }, rect);
|
|
524
|
+
const s0 = clamp(((it.start ?? i) - minV) / span, 0, 1);
|
|
525
|
+
const s1 = clamp(((it.end ?? (it.start ?? i) + 1) - minV) / span, s0, 1);
|
|
526
|
+
const px0 = Math.floor(s0 * (barW - 1));
|
|
527
|
+
const px1 = Math.max(px0 + 1, Math.ceil(s1 * barW));
|
|
528
|
+
const segLen = Math.max(1, Math.min(barW - px0, px1 - px0));
|
|
529
|
+
const fillChar = isSel ? "█" : it.status === "pending" ? "▒" : "▓";
|
|
530
|
+
text(frame, barX + px0, y, fillChar.repeat(segLen), sStyle, rect);
|
|
531
|
+
const durVal = (it.end ?? (it.start ?? i) + 1) - (it.start ?? i);
|
|
532
|
+
const metaText = it.meta ?? formatNum(durVal, props.unit);
|
|
533
|
+
const mw = widthOf(metaText);
|
|
534
|
+
text(frame, rect.x + rect.columns - Math.min(mw, metaW), y, truncate(metaText, metaW), isSel ? frame.theme.code : frame.theme.muted, rect);
|
|
535
|
+
}
|
|
536
|
+
if (showRuler && rect.y + list.length < rect.y + rect.rows) {
|
|
537
|
+
const ry = rect.y + list.length;
|
|
538
|
+
const unit = props.unit ?? "";
|
|
539
|
+
const leftLbl = formatNum(minV, unit);
|
|
540
|
+
const midLbl = formatNum(minV + span / 2, unit);
|
|
541
|
+
const rightLbl = formatNum(maxV, unit);
|
|
542
|
+
text(frame, barX, ry, "─".repeat(barW), frame.theme.border, rect);
|
|
543
|
+
text(frame, barX, ry, leftLbl, frame.theme.muted, rect);
|
|
544
|
+
const midX = barX + Math.max(widthOf(leftLbl) + 1, Math.floor((barW - widthOf(midLbl)) / 2));
|
|
545
|
+
if (midX + widthOf(midLbl) < barX + barW - widthOf(rightLbl)) {
|
|
546
|
+
text(frame, midX, ry, midLbl, frame.theme.muted, rect);
|
|
547
|
+
}
|
|
548
|
+
text(frame, barX + barW - widthOf(rightLbl), ry, rightLbl, frame.theme.muted, rect);
|
|
549
|
+
}
|
|
550
|
+
return;
|
|
551
|
+
}
|
|
552
|
+
const spaced = rect.rows >= list.length * 2 - 1;
|
|
553
|
+
let y = rect.y;
|
|
554
|
+
for (let i = 0; i < list.length && y < rect.y + rect.rows; i++) {
|
|
555
|
+
const it = list[i];
|
|
556
|
+
const isSel = it.id === activeId;
|
|
557
|
+
if (isSel) {
|
|
558
|
+
paint(frame, { x: rect.x, y, columns: rect.columns, rows: 1 }, { bg: focused ? "#1e2c48" : "#171f30" });
|
|
559
|
+
}
|
|
560
|
+
const glyph = statusGlyph(it.status);
|
|
561
|
+
const sStyle = statusStyle(it.status);
|
|
562
|
+
text(frame, rect.x, y, isSel ? "▸" : " ", focused ? frame.theme.primary : frame.theme.muted, rect);
|
|
563
|
+
text(frame, rect.x + 2, y, glyph, sStyle, rect);
|
|
564
|
+
const metaStr = it.meta ?? "";
|
|
565
|
+
const metaW = metaStr ? widthOf(metaStr) + 2 : 0;
|
|
566
|
+
text(frame, rect.x + 4, y, truncate(it.label, Math.max(6, rect.columns - 5 - metaW)), isSel ? { fg: "#e6edf7", bold: true, bg: focused ? "#1e2c48" : "#171f30" } : frame.theme.heading, rect);
|
|
567
|
+
if (metaStr) {
|
|
568
|
+
text(frame, rect.x + rect.columns - widthOf(metaStr), y, metaStr, frame.theme.muted, rect);
|
|
569
|
+
}
|
|
570
|
+
y++;
|
|
571
|
+
if (spaced && i < list.length - 1 && y < rect.y + rect.rows) {
|
|
572
|
+
text(frame, rect.x + 2, y, "│", frame.theme.border, rect);
|
|
573
|
+
if (it.detail && isSel) {
|
|
574
|
+
text(frame, rect.x + 4, y, truncate(it.detail, Math.max(6, rect.columns - 5)), frame.theme.muted, rect);
|
|
575
|
+
}
|
|
576
|
+
y++;
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
},
|
|
580
|
+
});
|
|
581
|
+
return { Root, Track };
|
|
582
|
+
},
|
|
583
|
+
});
|
|
584
|
+
/**
|
|
585
|
+
* Interactive terminal button with optional human-only gate (`humanOnly: true` blocks agent clicks).
|
|
586
|
+
*/
|
|
587
|
+
export const Button = definePrimitive({
|
|
588
|
+
name: "button",
|
|
589
|
+
state: (_context) => ({}),
|
|
590
|
+
api: (context) => {
|
|
591
|
+
function press(source, key) {
|
|
592
|
+
const p = context.props();
|
|
593
|
+
if (p.disabled)
|
|
594
|
+
return false;
|
|
595
|
+
if (p.humanOnly && source === "agent")
|
|
596
|
+
return false;
|
|
597
|
+
p.onPress?.({ source, key });
|
|
598
|
+
return true;
|
|
599
|
+
}
|
|
600
|
+
return { press };
|
|
601
|
+
},
|
|
602
|
+
agent: (root) => ({
|
|
603
|
+
state: { disabled: Boolean(root.props.disabled), humanOnly: Boolean(root.props.humanOnly) },
|
|
604
|
+
items: [],
|
|
605
|
+
actions: {
|
|
606
|
+
press: () => {
|
|
607
|
+
if (root.props.humanOnly)
|
|
608
|
+
return "human-only button cannot be triggered by an agent";
|
|
609
|
+
if (root.props.disabled)
|
|
610
|
+
return "button is disabled";
|
|
611
|
+
root.api.press("agent", null);
|
|
612
|
+
return "";
|
|
613
|
+
},
|
|
614
|
+
},
|
|
615
|
+
}),
|
|
616
|
+
slots: (slot, Root) => {
|
|
617
|
+
const Trigger = slot("trigger", {
|
|
618
|
+
focusable: true,
|
|
619
|
+
keys: (key, part) => {
|
|
620
|
+
if (key.ctrl || key.alt)
|
|
621
|
+
return false;
|
|
622
|
+
if (key.name === "enter" || isSpace(key)) {
|
|
623
|
+
part.api.press("keyboard", key);
|
|
624
|
+
return true;
|
|
625
|
+
}
|
|
626
|
+
return false;
|
|
627
|
+
},
|
|
628
|
+
mouse: (key, part) => {
|
|
629
|
+
const m = key.mouse;
|
|
630
|
+
if (!m || m.action !== "press" || m.button !== "left")
|
|
631
|
+
return false;
|
|
632
|
+
part.api.press("mouse", key);
|
|
633
|
+
return true;
|
|
634
|
+
},
|
|
635
|
+
draw: (part) => {
|
|
636
|
+
const { frame, rect, props, focused } = part;
|
|
637
|
+
if (rect.columns < 4 || rect.rows < 1)
|
|
638
|
+
return;
|
|
639
|
+
const variant = props.variant ?? "default";
|
|
640
|
+
const icon = variant === "primary" ? "▶ " : variant === "danger" ? "× " : "";
|
|
641
|
+
const textLabel = ` ${icon}${props.label} `;
|
|
642
|
+
let style;
|
|
643
|
+
if (props.disabled) {
|
|
644
|
+
style = frame.theme.muted;
|
|
645
|
+
}
|
|
646
|
+
else if (focused) {
|
|
647
|
+
style =
|
|
648
|
+
variant === "danger"
|
|
649
|
+
? { fg: "#0e131d", bg: "#ff7a90", bold: true }
|
|
650
|
+
: variant === "primary"
|
|
651
|
+
? { fg: "#0e131d", bg: "#5fd787", bold: true }
|
|
652
|
+
: frame.theme.highlight;
|
|
653
|
+
}
|
|
654
|
+
else {
|
|
655
|
+
style =
|
|
656
|
+
variant === "primary"
|
|
657
|
+
? { fg: "#7aa2ff", bg: "#1d2a44", bold: true }
|
|
658
|
+
: variant === "danger"
|
|
659
|
+
? { fg: "#ff7a90", bg: "#321a22", bold: true }
|
|
660
|
+
: { fg: "#e6edf7", bg: "#222938" };
|
|
661
|
+
}
|
|
662
|
+
const prefix = focused ? "▸ " : " ";
|
|
663
|
+
const x = text(frame, rect.x, rect.y, prefix, focused ? frame.theme.primary : frame.theme.muted, rect);
|
|
664
|
+
text(frame, x, rect.y, truncate(textLabel, Math.max(2, rect.columns - 2)), style, rect);
|
|
665
|
+
},
|
|
666
|
+
});
|
|
667
|
+
return { Root, Trigger };
|
|
668
|
+
},
|
|
669
|
+
});
|