@hit-sudomaker/ccw 0.4.1 → 0.5.1
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 +54 -52
- package/assets/ccline/config.toml +34 -34
- package/assets/skills/amirc-research/SKILL.md +39 -39
- package/dist/ccline.js +149 -617
- package/dist/ccw.js +166 -381
- package/dist/claude-program.js +191 -0
- package/dist/claude.js +177 -0
- package/dist/cleanup.js +148 -362
- package/dist/effect-machine.js +65 -0
- package/dist/frozen-json.js +99 -0
- package/dist/instance.js +71 -0
- package/dist/machine.js +610 -572
- package/dist/main.js +66 -67
- package/dist/management-catalog.js +56 -56
- package/dist/messages.js +182 -0
- package/dist/model.js +37 -155
- package/dist/npm-registry.js +108 -29
- package/dist/officecli.js +270 -407
- package/dist/planning.js +141 -0
- package/dist/plugins.js +134 -319
- package/dist/program-channels.js +94 -393
- package/dist/program-operations.js +155 -0
- package/dist/read-machine.js +94 -38
- package/dist/render.js +345 -396
- package/dist/settings.js +380 -0
- package/dist/skill-files.js +182 -96
- package/dist/skills.js +393 -259
- package/dist/terminal-view.js +303 -455
- package/dist/terminal.js +207 -508
- package/dist/work.js +135 -0
- package/package.json +1 -2
- package/dist/claude-config.js +0 -349
- package/dist/collect.js +0 -147
- package/dist/errors.js +0 -140
- package/dist/legacy-amirc.js +0 -60
- package/dist/mcp.js +0 -317
- package/dist/programs.js +0 -797
- package/dist/run-presentation.js +0 -157
- package/dist/storage.js +0 -44
- package/dist/titles.js +0 -46
- package/dist/warnings.js +0 -6
package/dist/terminal-view.js
CHANGED
|
@@ -1,446 +1,36 @@
|
|
|
1
1
|
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
2
|
import os from 'node:os';
|
|
3
|
-
import { Box, Text, render,
|
|
4
|
-
import {
|
|
5
|
-
import stringWidth from 'string-width';
|
|
3
|
+
import { Box, Text, render, useInput, usePaste, useWindowSize, useApp } from 'ink';
|
|
4
|
+
import { useState, useRef, useLayoutEffect } from 'react';
|
|
6
5
|
import wrapAnsi from 'wrap-ansi';
|
|
7
|
-
import
|
|
8
|
-
import { statusLine } from './
|
|
6
|
+
import stringWidth from 'string-width';
|
|
7
|
+
import { BACK_TO_HOME, BRAND, safeText, statusLine } from './render.js';
|
|
9
8
|
const ACCENT = '#D97757';
|
|
10
|
-
|
|
11
|
-
const
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
!key.rightArrow &&
|
|
36
|
-
!key.pageUp &&
|
|
37
|
-
!key.pageDown &&
|
|
38
|
-
!key.home &&
|
|
39
|
-
!key.end &&
|
|
40
|
-
!key.ctrl &&
|
|
41
|
-
!key.meta);
|
|
42
|
-
}
|
|
43
|
-
/** 归一化捕获的未知异常:保留原 Error 实例,其余包裹为携带 cause 的 Error */
|
|
44
|
-
function asError(error, code) {
|
|
45
|
-
return error instanceof Error ? error : new Error(code, { cause: error });
|
|
46
|
-
}
|
|
47
|
-
function HomeHeader({ size }) {
|
|
48
|
-
const platform = process.platform === 'win32' ? 'Windows' : process.platform === 'darwin' ? 'macOS' : 'Linux';
|
|
49
|
-
const system = platformSummary(platform, os.arch());
|
|
50
|
-
return (_jsxs(Box, { flexDirection: "column", children: [_jsx(Text, { color: ACCENT, children: '═'.repeat(Math.min(BANNER_WIDTH, size.columns)) }), size.rows >= 28 && _jsx(Text, { children: " " }), size.rows >= 20 && size.columns >= 30 ? (BRAND.logo.map((line, index) => _jsx(Text, { color: ACCENT, bold: true, children: ` ${line}` }, index))) : (_jsxs(Text, { color: ACCENT, bold: true, children: [' ', "CCW"] })), size.rows >= 28 && _jsx(Text, { children: " " }), _jsxs(Text, { bold: true, wrap: "truncate-end", children: [' ', BRAND.title, " ", _jsx(Text, { dimColor: true, children: BRAND.version })] }), _jsxs(Text, { dimColor: true, children: [" ", BRAND.credit] }), size.rows >= 24 ? (_jsxs(_Fragment, { children: [_jsx(Text, { color: ACCENT, children: '─'.repeat(Math.min(BANNER_WIDTH, size.columns)) }), _jsx(Text, { bold: true, children: " \u5F53\u524D\u73AF\u5883\u72B6\u6001" }), _jsxs(Text, { children: [" 1. \u7CFB\u7EDF ", system] }), _jsxs(Text, { children: [" 2. Node ", process.version] }), _jsxs(Text, { wrap: "truncate-end", children: [" 3. \u4E3B\u76EE\u5F55 ", safeText(os.homedir())] })] })) : (_jsxs(_Fragment, { children: [_jsx(Text, { dimColor: true, children: ` ${system} · Node ${process.version}` }), _jsxs(Text, { dimColor: true, wrap: "truncate-end", children: [' ', safeText(os.homedir())] })] })), _jsx(Text, { color: ACCENT, children: '═'.repeat(Math.min(BANNER_WIDTH, size.columns)) }), _jsx(Text, { bold: true, children: "\u8BF7\u9009\u62E9\uFF1A" })] }));
|
|
51
|
-
}
|
|
52
|
-
function Frame({ title, footer, page = 0, children }) {
|
|
53
|
-
const terminalSize = useWindowSize();
|
|
54
|
-
const size = { columns: Math.max(8, terminalSize.columns - 1), rows: Math.max(10, terminalSize.rows) };
|
|
55
|
-
const [name = '', ...body] = safeText(title).split('\n');
|
|
56
|
-
const details = useMemo(() => wrapAnsi(body.join('\n'), size.columns, { hard: true, trim: false }).split('\n').filter(Boolean), [title, size.columns]);
|
|
57
|
-
const detailHeight = Math.min(Math.max(3, Math.floor(size.rows / 3) - 1), details.length);
|
|
58
|
-
const offset = Math.min(page * Math.max(1, detailHeight), Math.max(0, details.length - detailHeight));
|
|
59
|
-
const hasPages = details.length > detailHeight;
|
|
60
|
-
const isHome = title === HOME_TITLE;
|
|
61
|
-
const headerHeight = isHome
|
|
62
|
-
? 5 + (size.rows >= 28 ? 2 : 0) + (size.rows >= 20 && size.columns >= 30 ? 6 : 1) + (size.rows >= 24 ? 5 : 2)
|
|
63
|
-
: 3 + detailHeight + (hasPages ? 1 : 0);
|
|
64
|
-
const height = Math.max(1, size.rows - headerHeight - 4);
|
|
65
|
-
return (_jsxs(Box, { flexDirection: "column", width: size.columns, height: size.rows - 1, children: [isHome ? (_jsx(HomeHeader, { size: size })) : (_jsxs(_Fragment, { children: [_jsx(Box, { borderStyle: "double", borderColor: ACCENT, width: Math.min(BANNER_WIDTH, size.columns), paddingX: 1, flexShrink: 0, children: _jsxs(Text, { bold: true, color: ACCENT, wrap: "truncate-end", children: ["CCW \u00B7 ", name] }) }), details.slice(offset, offset + detailHeight).map((line, index) => (_jsx(Text, { children: line }, index))), hasPages && _jsxs(Text, { dimColor: true, children: ["PgUp/PgDn \u00B7 ", windowIndicator(offset, detailHeight, details.length)] })] })), _jsx(Box, { flexDirection: "column", height: height, marginTop: 1, flexShrink: 0, children: children(height, size.columns) }), _jsx(Box, { marginTop: 1, children: _jsx(Text, { dimColor: true, wrap: "truncate-end", children: footer }) })] }));
|
|
66
|
-
}
|
|
67
|
-
function Menu({ title, choices, focus, selection, change, interrupt, done, invalidate }) {
|
|
68
|
-
const initialCount = selection ? selection.rows.length + 2 : choices.length;
|
|
69
|
-
const live = useRef({
|
|
70
|
-
index: Math.max(0, Math.min(focus, initialCount - 1)),
|
|
71
|
-
page: 0,
|
|
72
|
-
...(selection ? { selection } : {}),
|
|
73
|
-
});
|
|
74
|
-
const [state, setState] = useState(live.current);
|
|
75
|
-
useInput((input, key) => {
|
|
76
|
-
const prior = live.current;
|
|
77
|
-
if (prior.result)
|
|
78
|
-
return;
|
|
79
|
-
const event = { input, key };
|
|
80
|
-
let next = prior;
|
|
81
|
-
const current = prior.selection;
|
|
82
|
-
const row = current?.rows[prior.index];
|
|
83
|
-
const count = current ? current.rows.length + 2 : choices.length;
|
|
84
|
-
const toggle = () => (change ? { ...prior, selection: change({ kind: 'toggle', index: prior.index }) } : prior);
|
|
85
|
-
const exit = isExit(event);
|
|
86
|
-
if (exit)
|
|
87
|
-
interrupt();
|
|
88
|
-
if (key.escape || exit)
|
|
89
|
-
next = { ...prior, result: { kind: 'cancel', index: prior.index } };
|
|
90
|
-
else if (key.upArrow || key.downArrow)
|
|
91
|
-
next = { ...prior, index: (prior.index + count + (key.upArrow ? -1 : 1)) % count };
|
|
92
|
-
else if (key.pageDown || key.pageUp)
|
|
93
|
-
next = { ...prior, page: Math.max(0, prior.page + (key.pageUp ? -1 : 1)) };
|
|
94
|
-
else if (key.return)
|
|
95
|
-
next =
|
|
96
|
-
row?.enter === 'toggle'
|
|
97
|
-
? toggle()
|
|
98
|
-
: {
|
|
99
|
-
...prior,
|
|
100
|
-
result: {
|
|
101
|
-
kind: !current || row ? 'open' : prior.index === current.rows.length ? 'submit' : 'cancel',
|
|
102
|
-
index: prior.index,
|
|
103
|
-
},
|
|
104
|
-
};
|
|
105
|
-
else if (current && input.toLowerCase() === 'r')
|
|
106
|
-
next = { ...prior, result: { kind: 'refresh', index: prior.index } };
|
|
107
|
-
else if (current && change && input.toLowerCase() === 'a')
|
|
108
|
-
next = { ...prior, selection: change({ kind: 'all' }) };
|
|
109
|
-
else if (row && input === ' ')
|
|
110
|
-
next = toggle();
|
|
111
|
-
// isIgnoredText 判定为组合回显:清屏强制重绘(机制见 invalidate)
|
|
112
|
-
if (next === prior && isIgnoredText(input, key)) {
|
|
113
|
-
invalidate?.();
|
|
114
|
-
next = { ...prior };
|
|
115
|
-
}
|
|
116
|
-
if (next.selection && !next.result)
|
|
117
|
-
next = { ...next, index: Math.min(next.index, next.selection.rows.length + 1) };
|
|
118
|
-
live.current = next;
|
|
119
|
-
setState(next);
|
|
120
|
-
});
|
|
121
|
-
useEffect(() => {
|
|
122
|
-
if (state.result)
|
|
123
|
-
done(state.result);
|
|
124
|
-
}, [state.result, done]);
|
|
125
|
-
const rows = useMemo(() => {
|
|
126
|
-
const result = [];
|
|
127
|
-
const labels = state.selection?.rows ?? choices.map(choice => (typeof choice === 'string' ? { label: choice } : choice));
|
|
128
|
-
const labelWidth = Math.max(0, ...labels.filter(row => row.detail).map(row => stringWidth(row.label)));
|
|
129
|
-
let previous;
|
|
130
|
-
for (const [index, choice] of labels.entries()) {
|
|
131
|
-
const group = choice.group;
|
|
132
|
-
if (group && group !== previous)
|
|
133
|
-
result.push({ label: group, detail: '', index: null, labelWidth: 0 });
|
|
134
|
-
previous = group;
|
|
135
|
-
result.push({
|
|
136
|
-
label: choice.label,
|
|
137
|
-
detail: choice.detail ?? '',
|
|
138
|
-
index,
|
|
139
|
-
labelWidth: choice.detail ? labelWidth : 0,
|
|
140
|
-
});
|
|
141
|
-
}
|
|
142
|
-
if (state.selection)
|
|
143
|
-
result.push({ label: state.selection.submitLabel, detail: '', index: labels.length, labelWidth: 0 }, { label: BACK_TO_HOME, detail: '', index: labels.length + 1, labelWidth: 0 });
|
|
144
|
-
return result;
|
|
145
|
-
}, [choices, state.selection]);
|
|
146
|
-
const currentRow = state.selection?.rows[state.index];
|
|
147
|
-
const enter = currentRow?.enter === 'toggle'
|
|
148
|
-
? '勾选'
|
|
149
|
-
: currentRow?.enter === 'edit'
|
|
150
|
-
? '编辑'
|
|
151
|
-
: currentRow
|
|
152
|
-
? '详情'
|
|
153
|
-
: state.index === state.selection?.rows.length
|
|
154
|
-
? '提交'
|
|
155
|
-
: '返回';
|
|
156
|
-
const footer = state.selection
|
|
157
|
-
? `空格勾选 · A 全选普通项 · R 刷新 · Enter ${enter} · Esc 返回`
|
|
158
|
-
: '↑↓ 移动 · Enter 选择 · Esc 返回';
|
|
159
|
-
return (_jsx(Frame, { title: state.selection?.title ?? title, page: state.page, footer: footer, children: (height, width) => {
|
|
160
|
-
const activeRow = Math.max(0, rows.findIndex(row => row.index === state.index));
|
|
161
|
-
const start = windowStart(activeRow, height, rows.length);
|
|
162
|
-
return rows.slice(start, start + height).map((row, position) => {
|
|
163
|
-
if (row.index === null) {
|
|
164
|
-
const label = ` ${GROUP_MARK}${safeText(row.label)} `;
|
|
165
|
-
return (_jsxs(Text, { color: ACCENT, dimColor: true, wrap: "truncate-end", children: [label, '─'.repeat(Math.max(0, Math.min(BANNER_WIDTH, width) - stringWidth(label)))] }, `heading-${start + position}`));
|
|
166
|
-
}
|
|
167
|
-
const active = row.index === state.index;
|
|
168
|
-
const selectedRow = state.selection?.rows[row.index];
|
|
169
|
-
const prefix = `${active ? CURSOR_PREFIX : BLANK_PREFIX}${selectedRow ? (selectedRow.checked ? '[x] ' : '[ ] ') : ''}`;
|
|
170
|
-
return (_jsxs(Box, { flexShrink: 0, children: [_jsx(Text, { ...(active ? { color: ACCENT } : {}), bold: active, children: prefix }), _jsx(Box, { width: row.detail ? Math.min(row.labelWidth + 2, Math.max(10, width - 24)) : width - stringWidth(prefix), flexShrink: 0, children: _jsx(Text, { ...(active ? { color: ACCENT } : {}), bold: active, wrap: "truncate-end", children: safeText(row.label).replaceAll('\n', ' ') }) }), row.detail && (_jsx(Text, { ...(active ? { color: ACCENT } : {}), dimColor: !active, wrap: "truncate-end", children: safeText(row.detail) }))] }, row.index));
|
|
171
|
-
});
|
|
172
|
-
} }));
|
|
173
|
-
}
|
|
174
|
-
const segmenter = new Intl.Segmenter(undefined, { granularity: 'grapheme' });
|
|
175
|
-
const characters = (text) => Array.from(segmenter.segment(text), part => part.segment);
|
|
176
|
-
function editText(prior, event) {
|
|
177
|
-
if ('key' in event) {
|
|
178
|
-
const { key, input } = event;
|
|
179
|
-
if (key.ctrl && input === 'u')
|
|
180
|
-
return { text: [], cursor: 0 };
|
|
181
|
-
if (key.leftArrow || key.rightArrow)
|
|
182
|
-
return { ...prior, cursor: Math.max(0, Math.min(prior.text.length, prior.cursor + (key.leftArrow ? -1 : 1))) };
|
|
183
|
-
if (key.home || key.end)
|
|
184
|
-
return { ...prior, cursor: key.home ? 0 : prior.text.length };
|
|
185
|
-
if (key.backspace && prior.cursor > 0)
|
|
186
|
-
return { text: prior.text.filter((_part, index) => index !== prior.cursor - 1), cursor: prior.cursor - 1 };
|
|
187
|
-
if (key.delete)
|
|
188
|
-
return { ...prior, text: prior.text.filter((_part, index) => index !== prior.cursor) };
|
|
189
|
-
if (key.ctrl || key.meta || key.tab)
|
|
190
|
-
return prior;
|
|
191
|
-
}
|
|
192
|
-
const added = characters(safeText('paste' in event ? event.paste : event.input).replaceAll('\n', ' '));
|
|
193
|
-
if (!added.length)
|
|
194
|
-
return prior;
|
|
195
|
-
return {
|
|
196
|
-
text: [...prior.text.slice(0, prior.cursor), ...added, ...prior.text.slice(prior.cursor)],
|
|
197
|
-
cursor: prior.cursor + added.length,
|
|
198
|
-
};
|
|
199
|
-
}
|
|
200
|
-
function InlineForm({ title, fields, validate, interrupt, done, invalidate, }) {
|
|
201
|
-
const live = useRef({
|
|
202
|
-
index: 0,
|
|
203
|
-
edits: fields.map(field => {
|
|
204
|
-
const chars = characters(field.value);
|
|
205
|
-
return { text: chars, cursor: chars.length };
|
|
206
|
-
}),
|
|
207
|
-
problem: null,
|
|
208
|
-
details: false,
|
|
209
|
-
page: 0,
|
|
210
|
-
finished: false,
|
|
211
|
-
});
|
|
212
|
-
const [state, setState] = useState(live.current);
|
|
213
|
-
const dispatch = (event) => {
|
|
214
|
-
const prior = live.current;
|
|
215
|
-
if (prior.finished)
|
|
216
|
-
return;
|
|
217
|
-
if (prior.details) {
|
|
218
|
-
let next = prior;
|
|
219
|
-
if ('key' in event && (event.key.escape || event.key.return))
|
|
220
|
-
next = { ...prior, details: false };
|
|
221
|
-
else if ('key' in event && (event.key.pageUp || event.key.pageDown))
|
|
222
|
-
next = { ...prior, page: Math.max(0, prior.page + (event.key.pageUp ? -1 : 1)) };
|
|
223
|
-
else if ('key' in event && isExit(event)) {
|
|
224
|
-
live.current = { ...prior, finished: true };
|
|
225
|
-
interrupt();
|
|
226
|
-
done(null);
|
|
227
|
-
return;
|
|
228
|
-
}
|
|
229
|
-
live.current = next;
|
|
230
|
-
setState(next);
|
|
231
|
-
return;
|
|
232
|
-
}
|
|
233
|
-
let next = prior;
|
|
234
|
-
if ('key' in event && (event.key.escape || isExit(event))) {
|
|
235
|
-
live.current = { ...prior, finished: true };
|
|
236
|
-
if (isExit(event))
|
|
237
|
-
interrupt();
|
|
238
|
-
done(null);
|
|
239
|
-
return;
|
|
240
|
-
}
|
|
241
|
-
if ('key' in event &&
|
|
242
|
-
(event.key.upArrow || event.key.downArrow || event.key.tab || (event.key.return && prior.index < fields.length))) {
|
|
243
|
-
const direction = event.key.upArrow || (event.key.tab && event.key.shift) ? -1 : 1;
|
|
244
|
-
const count = fields.length + 2 + Number(Boolean(prior.problem?.detail));
|
|
245
|
-
next = { ...prior, index: (prior.index + count + direction) % count };
|
|
246
|
-
}
|
|
247
|
-
else if ('key' in event && event.key.return) {
|
|
248
|
-
if (prior.index === fields.length + 2 && prior.problem?.detail) {
|
|
249
|
-
live.current = { ...prior, details: true, page: 0 };
|
|
250
|
-
setState(live.current);
|
|
251
|
-
return;
|
|
252
|
-
}
|
|
253
|
-
if (prior.index === fields.length + 1) {
|
|
254
|
-
live.current = { ...prior, finished: true };
|
|
255
|
-
done(null);
|
|
256
|
-
return;
|
|
257
|
-
}
|
|
258
|
-
const values = Object.fromEntries(fields.map((field, index) => [field.key, prior.edits[index]?.text.join('') ?? '']));
|
|
259
|
-
const problem = validate(values);
|
|
260
|
-
if (!problem) {
|
|
261
|
-
live.current = { ...prior, finished: true };
|
|
262
|
-
done(values);
|
|
263
|
-
return;
|
|
264
|
-
}
|
|
265
|
-
next = {
|
|
266
|
-
...prior,
|
|
267
|
-
problem,
|
|
268
|
-
index: Math.max(0, fields.findIndex(field => field.key === problem.field)),
|
|
269
|
-
};
|
|
270
|
-
}
|
|
271
|
-
else if (prior.index < fields.length) {
|
|
272
|
-
next = {
|
|
273
|
-
...prior,
|
|
274
|
-
problem: null,
|
|
275
|
-
edits: prior.edits.map((edit, index) => (index === prior.index ? editText(edit, event) : edit)),
|
|
276
|
-
};
|
|
277
|
-
}
|
|
278
|
-
// 未处理按键(含组合回显)强制重绘,机制见 createTerminal.invalidate
|
|
279
|
-
if (next === prior && 'key' in event && isIgnoredText(event.input, event.key)) {
|
|
280
|
-
invalidate?.();
|
|
281
|
-
next = { ...prior };
|
|
282
|
-
}
|
|
283
|
-
live.current = next;
|
|
284
|
-
setState(next);
|
|
285
|
-
};
|
|
286
|
-
useInput((input, key) => dispatch({ input, key }));
|
|
287
|
-
usePaste(paste => dispatch({ paste }));
|
|
288
|
-
if (state.details)
|
|
289
|
-
return (_jsx(Frame, { title: `填写说明\n${state.problem?.detail ?? ''}`, page: state.page, footer: "PgUp/PgDn \u7FFB\u9875 \u00B7 Enter/Esc \u8FD4\u56DE", children: () => _jsxs(Text, { color: ACCENT, children: [CURSOR_PREFIX, "\u8FD4\u56DE\u7F16\u8F91"] }) }));
|
|
290
|
-
return (_jsx(Frame, { title: `${title}${state.problem ? `\n${PROBLEM_MARK}${state.problem.message}` : ''}`, footer: state.index < fields.length ? '输入/粘贴 · ↑↓/Tab 切换 · Esc 取消' : '↑↓ 选择 · Enter 确认 · Esc 取消', children: (height, width) => {
|
|
291
|
-
const labels = [
|
|
292
|
-
...fields.map(field => field.label),
|
|
293
|
-
'保存配置',
|
|
294
|
-
'返回',
|
|
295
|
-
...(state.problem?.detail ? ['查看详情'] : []),
|
|
296
|
-
];
|
|
297
|
-
const start = windowStart(state.index, height, labels.length);
|
|
298
|
-
return labels.slice(start, start + height).map((label, offset) => {
|
|
299
|
-
const index = start + offset;
|
|
300
|
-
const active = index === state.index;
|
|
301
|
-
const field = fields[index];
|
|
302
|
-
const edit = state.edits[index];
|
|
303
|
-
const value = field && edit ? (field.secret ? '•'.repeat(edit.text.length) : edit.text.join('')) : '';
|
|
304
|
-
let display = value || field?.placeholder || '';
|
|
305
|
-
if (active && edit) {
|
|
306
|
-
const before = field?.secret ? '•'.repeat(edit.cursor) : edit.text.slice(0, edit.cursor).join('');
|
|
307
|
-
const after = field?.secret
|
|
308
|
-
? '•'.repeat(edit.text.length - edit.cursor)
|
|
309
|
-
: edit.text.slice(edit.cursor).join('');
|
|
310
|
-
display = `${wrapAnsi(before, Math.max(1, width - 20), { hard: true, trim: false })
|
|
311
|
-
.split('\n')
|
|
312
|
-
.at(-1) ?? ''}▏${after || (!value ? (field?.placeholder ?? '') : '')}`;
|
|
313
|
-
}
|
|
314
|
-
return (_jsxs(Text, { ...(active ? { color: ACCENT } : {}), wrap: "truncate-end", children: [active ? CURSOR_PREFIX : BLANK_PREFIX, label.padEnd(15), safeText(display)] }, fields[index]?.key ?? label));
|
|
315
|
-
});
|
|
316
|
-
} }));
|
|
317
|
-
}
|
|
318
|
-
function Waiting({ title, message = '检查环境', cancel, interrupt, }) {
|
|
319
|
-
const { frame } = useAnimation({ interval: 120 });
|
|
320
|
-
useInput((input, key) => {
|
|
321
|
-
const exit = isExit({ input, key });
|
|
322
|
-
if (key.escape || exit) {
|
|
323
|
-
cancel();
|
|
324
|
-
if (exit)
|
|
325
|
-
interrupt();
|
|
326
|
-
}
|
|
327
|
-
});
|
|
328
|
-
return (_jsx(Frame, { title: title, footer: "Esc \u53D6\u6D88 \u00B7 Ctrl+C \u9000\u51FA", children: () => (_jsxs(Text, { color: ACCENT, children: [['⠋', '⠙', '⠹', '⠸'][frame % 4], " ", safeText(message)] })) }));
|
|
329
|
-
}
|
|
330
|
-
function toneColor(tone) {
|
|
331
|
-
if (tone === 'accent')
|
|
332
|
-
return ACCENT;
|
|
333
|
-
if (tone === 'success')
|
|
334
|
-
return 'green';
|
|
335
|
-
if (tone === 'danger')
|
|
336
|
-
return 'red';
|
|
337
|
-
if (tone === 'warning')
|
|
338
|
-
return 'yellow';
|
|
339
|
-
return undefined;
|
|
340
|
-
}
|
|
341
|
-
function StatusRow({ row, text }) {
|
|
342
|
-
const color = toneColor(row.tone);
|
|
343
|
-
const name = safeText(row.title).replaceAll('\n', ' ');
|
|
344
|
-
if (text !== undefined)
|
|
345
|
-
return (_jsx(Text, { ...(color ? { color } : {}), dimColor: row.tone === 'muted', children: text }));
|
|
346
|
-
return (_jsxs(Box, { children: [_jsx(Box, { width: 2, flexShrink: 0, children: _jsx(Text, { ...(color ? { color } : {}), dimColor: row.tone === 'muted', children: row.symbol }) }), _jsx(Box, { width: 10, flexShrink: 0, children: _jsx(Text, { ...(color ? { color } : {}), dimColor: row.tone === 'muted', children: row.status }) }), _jsx(Text, { wrap: "truncate-end", children: name })] }));
|
|
347
|
-
}
|
|
348
|
-
function DetailRow({ detail, width }) {
|
|
349
|
-
const color = toneColor(detail.tone);
|
|
350
|
-
const indent = detailIndent(width);
|
|
351
|
-
return (_jsxs(Box, { marginLeft: indent, children: [detail.symbol && (_jsx(Box, { width: 2, flexShrink: 0, children: _jsx(Text, { ...(color ? { color } : {}), dimColor: detail.tone === 'muted', children: detail.symbol }) })), _jsx(Text, { dimColor: detail.tone === 'muted', wrap: "truncate-end", children: safeText(detail.text) })] }));
|
|
352
|
-
}
|
|
353
|
-
function runLines(view, width) {
|
|
354
|
-
return view.rows.flatMap(row => {
|
|
355
|
-
const status = width < 24
|
|
356
|
-
? wrapAnsi(statusLine({ ...row, title: safeText(row.title) }), width, { hard: true, trim: false })
|
|
357
|
-
.split('\n')
|
|
358
|
-
.map(text => ({ kind: 'status', row, text }))
|
|
359
|
-
: [{ kind: 'status', row }];
|
|
360
|
-
return [
|
|
361
|
-
...status,
|
|
362
|
-
...row.details.flatMap(detail => {
|
|
363
|
-
const { symbol, ...appearance } = detail;
|
|
364
|
-
const indent = detailIndent(width);
|
|
365
|
-
return wrapAnsi(safeText(detail.text), Math.max(1, width - indent - (symbol ? 2 : 0)), {
|
|
366
|
-
hard: true,
|
|
367
|
-
trim: false,
|
|
368
|
-
})
|
|
369
|
-
.split('\n')
|
|
370
|
-
.map((text, index) => ({
|
|
371
|
-
kind: 'detail',
|
|
372
|
-
id: row.id,
|
|
373
|
-
detail: { ...appearance, text, ...(symbol ? { symbol: index === 0 ? symbol : ' ' } : {}) },
|
|
374
|
-
}));
|
|
375
|
-
}),
|
|
376
|
-
];
|
|
377
|
-
});
|
|
378
|
-
}
|
|
379
|
-
function RunScreen({ view, choices = [], focus = 0, cancelIndex = choices.length - 1, interrupt, done, invalidate, }) {
|
|
380
|
-
const [offset, setOffset] = useState(null);
|
|
381
|
-
const [action, setAction] = useState(Math.max(0, Math.min(focus, choices.length - 1)));
|
|
382
|
-
const [, setRepaint] = useState(0);
|
|
383
|
-
const finished = useRef(false);
|
|
384
|
-
const viewport = useRef({ offset: 0, height: 1, total: 1 });
|
|
385
|
-
useInput((input, key) => {
|
|
386
|
-
const exit = isExit({ input, key });
|
|
387
|
-
if (exit)
|
|
388
|
-
interrupt();
|
|
389
|
-
if (choices.length && (key.escape || exit) && !finished.current) {
|
|
390
|
-
finished.current = true;
|
|
391
|
-
done?.(cancelIndex);
|
|
392
|
-
return;
|
|
393
|
-
}
|
|
394
|
-
if (choices.length && (key.upArrow || key.downArrow))
|
|
395
|
-
setAction(previous => (previous + choices.length + (key.upArrow ? -1 : 1)) % choices.length);
|
|
396
|
-
if (choices.length && key.return && !finished.current) {
|
|
397
|
-
finished.current = true;
|
|
398
|
-
done?.(action);
|
|
399
|
-
return;
|
|
400
|
-
}
|
|
401
|
-
if (key.pageUp)
|
|
402
|
-
setOffset(previous => Math.max(0, (previous ?? viewport.current.offset) - viewport.current.height));
|
|
403
|
-
if (key.pageDown)
|
|
404
|
-
setOffset(previous => Math.min(Math.max(0, viewport.current.total - viewport.current.height), (previous ?? viewport.current.offset) + viewport.current.height));
|
|
405
|
-
if (key.end)
|
|
406
|
-
setOffset(null);
|
|
407
|
-
if (isIgnoredText(input, key)) {
|
|
408
|
-
invalidate?.();
|
|
409
|
-
setRepaint(count => count + 1);
|
|
410
|
-
}
|
|
411
|
-
});
|
|
412
|
-
const footer = choices.length
|
|
413
|
-
? '↑↓ 选择 · Enter 确认 · PgUp/PgDn 翻页 · Esc 返回'
|
|
414
|
-
: 'PgUp/PgDn 翻页 · End 跟随 · Ctrl+C 停止';
|
|
415
|
-
return (_jsx(Frame, { title: view.title, footer: footer, children: (height, width) => {
|
|
416
|
-
const lines = runLines(view, width);
|
|
417
|
-
const actionHeight = choices.length ? choices.length + 1 : 0;
|
|
418
|
-
const available = Math.max(1, height - 1 - actionHeight);
|
|
419
|
-
const visible = Math.max(1, available - (lines.length > available ? 1 : 0));
|
|
420
|
-
const current = Math.max(0, lines.findIndex(line => line.kind === 'status' && line.row.id === view.activeId));
|
|
421
|
-
const automatic = view.phase === 'result' ? 0 : windowStart(current, visible, lines.length);
|
|
422
|
-
const start = clampWindow(offset ?? automatic, visible, lines.length);
|
|
423
|
-
// Ink 同步渲染保证 viewport 每帧恰写一次
|
|
424
|
-
viewport.current = { offset: start, height: visible, total: lines.length };
|
|
425
|
-
return (_jsxs(Box, { flexDirection: "column", children: [_jsx(Text, { bold: true, wrap: "truncate-end", children: safeText(view.summary) }), lines
|
|
426
|
-
.slice(start, start + visible)
|
|
427
|
-
.map((line, index) => line.kind === 'status' ? (_jsx(StatusRow, { row: line.row, ...(line.text !== undefined ? { text: line.text } : {}) }, `${line.row.id}-status-${start + index}`)) : (_jsx(DetailRow, { detail: line.detail, width: width }, `${line.id}-detail-${start + index}`))), lines.length > visible && _jsx(Text, { dimColor: true, children: windowIndicator(start, visible, lines.length) }), choices.length > 0 && (_jsx(Box, { flexDirection: "column", marginTop: 1, children: choices.map((choice, index) => (_jsxs(Text, { ...(index === action ? { color: ACCENT } : {}), bold: index === action, children: [index === action ? CURSOR_PREFIX : BLANK_PREFIX, safeText(typeof choice === 'string' ? choice : `${choice.label} ${choice.detail ?? ''}`)] }, index))) }))] }));
|
|
428
|
-
} }));
|
|
429
|
-
}
|
|
430
|
-
function Host({ children, ready, }) {
|
|
431
|
-
const { suspendTerminal } = useApp();
|
|
432
|
-
useLayoutEffect(() => {
|
|
433
|
-
ready(suspendTerminal);
|
|
434
|
-
}, [ready, suspendTerminal]);
|
|
435
|
-
return children;
|
|
436
|
-
}
|
|
437
|
-
async function pauseReading() {
|
|
438
|
-
// Ink 使用 readable 模式,pause() 可能不发事件;通知 Node 停止底层读取后再交接
|
|
439
|
-
process.stdin.pause();
|
|
440
|
-
process.stdin.emit('pause');
|
|
441
|
-
await new Promise(resolve => process.nextTick(resolve));
|
|
442
|
-
}
|
|
443
|
-
/** 复用一个 Ink 实例切换页面,关闭时恢复终端;交接期间由外部命令使用终端 */
|
|
9
|
+
const BANNER_WIDTH = 62;
|
|
10
|
+
const MAX_RENDER_FPS = 30;
|
|
11
|
+
const LAYOUT = {
|
|
12
|
+
minimumColumns: 8,
|
|
13
|
+
minimumRows: 10,
|
|
14
|
+
defaultContentRows: 8,
|
|
15
|
+
menuPageOverheadRows: 10,
|
|
16
|
+
labelMinimumColumns: 10,
|
|
17
|
+
formDecorationColumns: 5,
|
|
18
|
+
groupHeadingPaddingColumns: 3,
|
|
19
|
+
headerRows: 3,
|
|
20
|
+
footerRows: 3,
|
|
21
|
+
sectionGapRows: 1,
|
|
22
|
+
homeCreditRows: 18,
|
|
23
|
+
homeExpandedRows: 24,
|
|
24
|
+
homeLogoColumns: 32,
|
|
25
|
+
homeFixedRows: 5,
|
|
26
|
+
homeEnvironmentRows: 5,
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* 装配复用单个 Ink 实例的终端适配器,首次显示页面时才接管终端
|
|
30
|
+
*
|
|
31
|
+
* @param interrupt - 用户按 Ctrl+C 时调用的中断通知,不由适配器直接退出进程
|
|
32
|
+
* @returns Terminal 实现及关闭方法;输入、取消与交接合同见 Terminal
|
|
33
|
+
*/
|
|
444
34
|
export function createTerminal(interrupt = () => { }) {
|
|
445
35
|
let instance;
|
|
446
36
|
let suspension;
|
|
@@ -452,10 +42,6 @@ export function createTerminal(interrupt = () => { }) {
|
|
|
452
42
|
const ready = (suspend) => {
|
|
453
43
|
suspension = suspend;
|
|
454
44
|
};
|
|
455
|
-
/** 擦除当前帧并重置帧高:下一次渲染将全量重绘,覆盖终端宿主画入的输入法组合回显 */
|
|
456
|
-
const invalidate = () => {
|
|
457
|
-
instance?.clear();
|
|
458
|
-
};
|
|
459
45
|
const display = (view) => {
|
|
460
46
|
if (closed)
|
|
461
47
|
throw new Error('terminal-closed');
|
|
@@ -466,7 +52,7 @@ export function createTerminal(interrupt = () => { }) {
|
|
|
466
52
|
instance = render(tree, {
|
|
467
53
|
alternateScreen: true,
|
|
468
54
|
incrementalRendering: true,
|
|
469
|
-
maxFps:
|
|
55
|
+
maxFps: MAX_RENDER_FPS,
|
|
470
56
|
exitOnCtrlC: false,
|
|
471
57
|
patchConsole: false,
|
|
472
58
|
});
|
|
@@ -507,18 +93,20 @@ export function createTerminal(interrupt = () => { }) {
|
|
|
507
93
|
});
|
|
508
94
|
};
|
|
509
95
|
return {
|
|
510
|
-
choose(title, choices, focus = 0,
|
|
511
|
-
return
|
|
512
|
-
? prompt(done => (_jsx(Menu, { title: title, choices: choices, focus: focus, interrupt: interrupt, invalidate: invalidate, done: result => done(result.kind === 'cancel' ? cancelIndex : result.index) }, ++sequence)))
|
|
513
|
-
: prompt(done => (_jsx(RunScreen, { view: title, choices: choices, focus: focus, cancelIndex: cancelIndex, interrupt: interrupt, invalidate: invalidate, done: done }, ++sequence)));
|
|
96
|
+
choose(title, choices, focus = 0, options) {
|
|
97
|
+
return prompt(done => (_jsx(Menu, { title: title, choices: choices, focus: focus, home: options?.home ?? false, interrupt: interrupt, done: result => done(result.kind === 'cancel' ? choices.length - 1 : result.index) }, ++sequence)));
|
|
514
98
|
},
|
|
515
|
-
select(page, change
|
|
516
|
-
return prompt(done => (_jsx(Menu, { title: page.title, choices: [], selection: page, change: change, focus: focus, interrupt: interrupt,
|
|
99
|
+
select(page, change) {
|
|
100
|
+
return prompt(done => (_jsx(Menu, { title: page.title, choices: [], selection: page, change: change, focus: page.focus ?? 0, interrupt: interrupt, done: result => done(result.kind === 'edit'
|
|
101
|
+
? { kind: 'edit', index: result.index }
|
|
102
|
+
: result.kind === 'cancel'
|
|
103
|
+
? 'cancel'
|
|
104
|
+
: 'submit') }, ++sequence)));
|
|
517
105
|
},
|
|
518
106
|
form(title, fields, validate) {
|
|
519
|
-
return prompt(done => (_jsx(InlineForm, { title: title, fields: fields, validate: validate, interrupt: interrupt,
|
|
107
|
+
return prompt(done => (_jsx(InlineForm, { title: title, fields: fields, validate: validate, interrupt: interrupt, done: done }, ++sequence)));
|
|
520
108
|
},
|
|
521
|
-
async wait(title, work, signal
|
|
109
|
+
async wait(title, work, signal) {
|
|
522
110
|
if (signal?.aborted)
|
|
523
111
|
return null;
|
|
524
112
|
const reading = new AbortController();
|
|
@@ -531,7 +119,7 @@ export function createTerminal(interrupt = () => { }) {
|
|
|
531
119
|
const key = ++sequence;
|
|
532
120
|
const result = prompt(done => {
|
|
533
121
|
finish = done;
|
|
534
|
-
return
|
|
122
|
+
return _jsx(Waiting, { title: title, cancel: cancel, interrupt: interrupt }, key);
|
|
535
123
|
});
|
|
536
124
|
const update = (message) => {
|
|
537
125
|
if (!reading.signal.aborted)
|
|
@@ -566,7 +154,7 @@ export function createTerminal(interrupt = () => { }) {
|
|
|
566
154
|
await ownership.resume();
|
|
567
155
|
// 交互命令仅限执行屏活动期发起;恢复重放最近执行视图
|
|
568
156
|
if (progressView !== undefined)
|
|
569
|
-
display(_jsx(RunScreen, { view: progressView, interrupt: interrupt
|
|
157
|
+
display(_jsx(RunScreen, { view: progressView, interrupt: interrupt }));
|
|
570
158
|
// 快速任务可能在 readable 监听恢复前结束;重新启动读取但不消费输入
|
|
571
159
|
process.stdin.read(0);
|
|
572
160
|
}
|
|
@@ -574,8 +162,9 @@ export function createTerminal(interrupt = () => { }) {
|
|
|
574
162
|
show(view) {
|
|
575
163
|
progressView = view;
|
|
576
164
|
if (!isSuspended)
|
|
577
|
-
display(_jsx(RunScreen, { view: view, interrupt: interrupt
|
|
165
|
+
display(_jsx(RunScreen, { view: view, interrupt: interrupt }));
|
|
578
166
|
},
|
|
167
|
+
/** 终止待决输入并卸载界面;等待 Ink 退出和清理,失败时拒绝 Promise */
|
|
579
168
|
async close() {
|
|
580
169
|
closed = true;
|
|
581
170
|
rejectPending?.(new Error('terminal-closed'));
|
|
@@ -588,3 +177,262 @@ export function createTerminal(interrupt = () => { }) {
|
|
|
588
177
|
},
|
|
589
178
|
};
|
|
590
179
|
}
|
|
180
|
+
function Host({ children, ready, }) {
|
|
181
|
+
const { suspendTerminal } = useApp();
|
|
182
|
+
useLayoutEffect(() => {
|
|
183
|
+
ready(suspendTerminal);
|
|
184
|
+
}, [ready, suspendTerminal]);
|
|
185
|
+
return children;
|
|
186
|
+
}
|
|
187
|
+
function Menu({ title, choices, selection, change, focus, home = false, interrupt, done, }) {
|
|
188
|
+
const [page, setPage] = useState(selection);
|
|
189
|
+
const rows = page ? [...page.rows, { label: page.submitLabel }, { label: BACK_TO_HOME }] : choices;
|
|
190
|
+
const size = useWindowSize();
|
|
191
|
+
const labelWidth = Math.max(0, ...rows.map(row => (typeof row === 'string' ? 0 : stringWidth(row.label))));
|
|
192
|
+
const displayRows = [];
|
|
193
|
+
let previousGroup;
|
|
194
|
+
for (const [index, choice] of rows.entries()) {
|
|
195
|
+
const row = typeof choice === 'string' ? { label: choice } : choice;
|
|
196
|
+
if (row.group && row.group !== previousGroup)
|
|
197
|
+
displayRows.push({ row: { label: '── ' + row.group }, index: null });
|
|
198
|
+
displayRows.push({ row, index });
|
|
199
|
+
previousGroup = row.group;
|
|
200
|
+
}
|
|
201
|
+
const [active, setActive] = useState(Math.max(0, Math.min(focus, rows.length - 1)));
|
|
202
|
+
const activeRef = useRef(active);
|
|
203
|
+
const move = (index) => {
|
|
204
|
+
activeRef.current = index;
|
|
205
|
+
setActive(index);
|
|
206
|
+
};
|
|
207
|
+
const [offset, setOffset] = useState(0);
|
|
208
|
+
const toggle = (event) => {
|
|
209
|
+
if (change)
|
|
210
|
+
setPage(change(event));
|
|
211
|
+
};
|
|
212
|
+
useInput((input, key) => {
|
|
213
|
+
const index = activeRef.current;
|
|
214
|
+
if (key.ctrl && input === 'c') {
|
|
215
|
+
interrupt();
|
|
216
|
+
done({ kind: 'cancel', index });
|
|
217
|
+
}
|
|
218
|
+
else if (key.escape)
|
|
219
|
+
done({ kind: 'cancel', index });
|
|
220
|
+
else if (key.pageDown || key.pageUp) {
|
|
221
|
+
if (title.includes('\n'))
|
|
222
|
+
setOffset(previous => Math.max(0, previous + (key.pageUp ? -1 : 1)));
|
|
223
|
+
else
|
|
224
|
+
move(Math.max(0, Math.min(rows.length - 1, index + (key.pageUp ? -1 : 1) * Math.max(1, size.rows - LAYOUT.menuPageOverheadRows))));
|
|
225
|
+
}
|
|
226
|
+
else if (key.home)
|
|
227
|
+
move(0);
|
|
228
|
+
else if (key.end)
|
|
229
|
+
move(rows.length - 1);
|
|
230
|
+
else if (key.upArrow)
|
|
231
|
+
move((index + rows.length - 1) % rows.length);
|
|
232
|
+
else if (key.downArrow)
|
|
233
|
+
move((index + 1) % rows.length);
|
|
234
|
+
else if (page && input.toLowerCase() === 'a')
|
|
235
|
+
toggle({ kind: 'all' });
|
|
236
|
+
else if (page && input === ' ' && index < page.rows.length)
|
|
237
|
+
toggle({ kind: 'toggle', index });
|
|
238
|
+
else if (key.return) {
|
|
239
|
+
if (page && index === page.rows.length + 1)
|
|
240
|
+
done({ kind: 'cancel', index });
|
|
241
|
+
else if (page?.rows[index]?.editable)
|
|
242
|
+
done({ kind: 'edit', index });
|
|
243
|
+
else if (page && index < page.rows.length)
|
|
244
|
+
toggle({ kind: 'toggle', index });
|
|
245
|
+
else
|
|
246
|
+
done({ kind: 'confirm', index });
|
|
247
|
+
}
|
|
248
|
+
});
|
|
249
|
+
return (_jsx(Frame, { title: title, home: home, page: offset, reserveRows: Math.min(rows.length, LAYOUT.defaultContentRows) + 1, footer: page ? '↑↓ 移动 · 空格勾选 · A 全选 · Enter 编辑/确认 · Esc 返回' : '↑↓ 移动 · Enter 确认 · Esc 返回', children: (height, width) => {
|
|
250
|
+
const visible = Math.max(1, height - 1);
|
|
251
|
+
const activeRow = displayRows.findIndex(row => row.index === active);
|
|
252
|
+
const start = Math.max(0, Math.min(activeRow - Math.floor(visible / 2), displayRows.length - visible));
|
|
253
|
+
return (_jsxs(_Fragment, { children: [displayRows.slice(start, start + visible).map(({ row, index }, offset) => {
|
|
254
|
+
if (index === null)
|
|
255
|
+
return (_jsxs(Text, { color: ACCENT, dimColor: true, wrap: "truncate-end", children: [' ' + safeText(row.label) + ' ', '─'.repeat(Math.max(0, Math.min(BANNER_WIDTH, width) - stringWidth(row.label) - LAYOUT.groupHeadingPaddingColumns))] }, 'group-' + (start + offset)));
|
|
256
|
+
return (_jsxs(Box, { flexShrink: 0, children: [_jsxs(Text, { bold: index === active, ...(index === active ? { color: ACCENT } : {}), children: [index === active ? '› ' : ' ', page && index < page.rows.length ? (page.rows[index]?.checked ? '[x] ' : '[ ] ') : ''] }), _jsx(Box, { width: row.detail
|
|
257
|
+
? Math.min(labelWidth + 2, Math.max(LAYOUT.labelMinimumColumns, Math.floor(width / 2)))
|
|
258
|
+
: undefined, flexShrink: row.detail ? 0 : 1, children: _jsx(Text, { bold: index === active, wrap: "truncate-end", ...(index === active ? { color: ACCENT } : {}), children: safeText(row.label) }) }), row.detail && (_jsx(Text, { dimColor: index !== active, wrap: "truncate-end", ...(index === active ? { color: ACCENT } : {}), children: safeText(row.detail) }))] }, index));
|
|
259
|
+
}), displayRows.length > visible && (_jsxs(Text, { dimColor: true, children: [start + 1, "\u2013", Math.min(displayRows.length, start + visible), "/", displayRows.length] }))] }));
|
|
260
|
+
} }));
|
|
261
|
+
}
|
|
262
|
+
function InlineForm({ title, fields, validate, interrupt, done, }) {
|
|
263
|
+
const [values, setValues] = useState(() => Object.fromEntries(fields.map(field => [field.key, field.value])));
|
|
264
|
+
const valuesRef = useRef(values);
|
|
265
|
+
const cursorRef = useRef(Array.from(fields[0]?.value ?? '').length);
|
|
266
|
+
const [cursor, setCursor] = useState(cursorRef.current);
|
|
267
|
+
const position = (next) => {
|
|
268
|
+
cursorRef.current = next;
|
|
269
|
+
setCursor(next);
|
|
270
|
+
};
|
|
271
|
+
const replace = (field, text, next) => {
|
|
272
|
+
valuesRef.current = { ...valuesRef.current, [field.key]: text };
|
|
273
|
+
setValues(valuesRef.current);
|
|
274
|
+
position(next);
|
|
275
|
+
setProblem(null);
|
|
276
|
+
};
|
|
277
|
+
const [active, setActive] = useState(0);
|
|
278
|
+
const activeRef = useRef(active);
|
|
279
|
+
const move = (index) => {
|
|
280
|
+
activeRef.current = index;
|
|
281
|
+
setActive(index);
|
|
282
|
+
position(Array.from(valuesRef.current[fields[index]?.key ?? ''] ?? '').length);
|
|
283
|
+
};
|
|
284
|
+
const [problem, setProblem] = useState(null);
|
|
285
|
+
const size = useWindowSize();
|
|
286
|
+
const labelWidth = Math.max(0, ...fields.map(field => stringWidth(field.label)));
|
|
287
|
+
const append = (text) => {
|
|
288
|
+
const field = fields[activeRef.current];
|
|
289
|
+
if (field) {
|
|
290
|
+
const characters = Array.from(valuesRef.current[field.key] ?? '');
|
|
291
|
+
const inserted = Array.from(safeText(text).replace(/\r?\n/g, ' '));
|
|
292
|
+
characters.splice(cursorRef.current, 0, ...inserted);
|
|
293
|
+
replace(field, characters.join(''), cursorRef.current + inserted.length);
|
|
294
|
+
}
|
|
295
|
+
};
|
|
296
|
+
usePaste(append);
|
|
297
|
+
useInput((input, key) => {
|
|
298
|
+
const index = activeRef.current;
|
|
299
|
+
const field = fields[index];
|
|
300
|
+
const characters = Array.from(valuesRef.current[field?.key ?? ''] ?? '');
|
|
301
|
+
if (key.ctrl && input === 'c') {
|
|
302
|
+
interrupt();
|
|
303
|
+
done(null);
|
|
304
|
+
}
|
|
305
|
+
else if (key.escape)
|
|
306
|
+
done(null);
|
|
307
|
+
else if (key.upArrow)
|
|
308
|
+
move(Math.max(0, index - 1));
|
|
309
|
+
else if (key.tab && key.shift)
|
|
310
|
+
move(Math.max(0, index - 1));
|
|
311
|
+
else if (key.downArrow || key.tab)
|
|
312
|
+
move(Math.min(fields.length + 1, index + 1));
|
|
313
|
+
else if (field && key.leftArrow)
|
|
314
|
+
position(Math.max(0, cursorRef.current - 1));
|
|
315
|
+
else if (field && key.rightArrow)
|
|
316
|
+
position(Math.min(characters.length, cursorRef.current + 1));
|
|
317
|
+
else if (field && key.home)
|
|
318
|
+
position(0);
|
|
319
|
+
else if (field && key.end)
|
|
320
|
+
position(characters.length);
|
|
321
|
+
else if (field && key.ctrl && input === 'u')
|
|
322
|
+
replace(field, '', 0);
|
|
323
|
+
else if (key.return) {
|
|
324
|
+
if (index === fields.length + 1)
|
|
325
|
+
done(null);
|
|
326
|
+
else if (index < fields.length - 1)
|
|
327
|
+
move(index + 1);
|
|
328
|
+
else {
|
|
329
|
+
const error = validate(valuesRef.current);
|
|
330
|
+
if (error) {
|
|
331
|
+
setProblem(error);
|
|
332
|
+
move(Math.max(0, fields.findIndex(field => field.key === error.field)));
|
|
333
|
+
}
|
|
334
|
+
else
|
|
335
|
+
done(valuesRef.current);
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
else if (key.backspace || key.delete) {
|
|
339
|
+
if (field) {
|
|
340
|
+
const next = Math.max(0, cursorRef.current - (key.backspace ? 1 : 0));
|
|
341
|
+
if (!key.backspace || cursorRef.current > 0)
|
|
342
|
+
characters.splice(next, 1);
|
|
343
|
+
replace(field, characters.join(''), next);
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
else if (!key.ctrl && !key.meta && input)
|
|
347
|
+
append(input);
|
|
348
|
+
});
|
|
349
|
+
return (_jsx(Frame, { title: title, footer: "Tab \u5207\u6362 \u00B7 \u2190\u2192 \u7F16\u8F91 \u00B7 Ctrl+U \u6E05\u7A7A \u00B7 Enter \u7EE7\u7EED \u00B7 Esc \u53D6\u6D88", children: height => {
|
|
350
|
+
const visible = Math.max(1, height - 2 - (problem ? 1 : 0));
|
|
351
|
+
const start = Math.max(0, Math.min(active - Math.floor(visible / 2), fields.length - visible));
|
|
352
|
+
return (_jsxs(_Fragment, { children: [fields.slice(start, start + visible).map((field, offset) => {
|
|
353
|
+
const index = start + offset;
|
|
354
|
+
const text = field.secret
|
|
355
|
+
? '•'.repeat(Array.from(values[field.key] ?? '').length)
|
|
356
|
+
: (values[field.key] ?? '');
|
|
357
|
+
const characters = Array.from(text);
|
|
358
|
+
const caret = index === active ? cursor : characters.length;
|
|
359
|
+
const available = Math.max(1, size.columns - labelWidth - LAYOUT.formDecorationColumns);
|
|
360
|
+
let from = caret;
|
|
361
|
+
while (from > 0 && stringWidth(characters.slice(from - 1, caret).join('')) < available)
|
|
362
|
+
from--;
|
|
363
|
+
let to = caret;
|
|
364
|
+
while (to < characters.length && stringWidth(characters.slice(from, to + 1).join('')) < available)
|
|
365
|
+
to++;
|
|
366
|
+
return (_jsxs(Text, { wrap: "truncate-end", ...(index === active ? { color: ACCENT } : {}), children: [index === active ? '› ' : ' ', field.label, ' '.repeat(labelWidth - stringWidth(field.label)), "\uFF1A", safeText(characters.slice(from, caret).join('')), index === active && _jsx(Text, { inverse: true, children: characters[caret] ?? ' ' }), safeText(characters.slice(caret + (index === active ? 1 : 0), to).join('')), !values[field.key] && field.placeholder ? ' ' + field.placeholder : ''] }, field.key));
|
|
367
|
+
}), _jsxs(Text, { ...(active === fields.length ? { color: ACCENT } : {}), children: [active === fields.length ? '› ' : ' ', "\u4FDD\u5B58"] }), _jsxs(Text, { ...(active === fields.length + 1 ? { color: ACCENT } : {}), children: [active === fields.length + 1 ? '› ' : ' ', "\u8FD4\u56DE"] }), problem && _jsx(Text, { color: "red", children: problem.message })] }));
|
|
368
|
+
} }));
|
|
369
|
+
}
|
|
370
|
+
function Waiting({ title, message, cancel, interrupt, }) {
|
|
371
|
+
useInput((input, key) => {
|
|
372
|
+
if (key.ctrl && input === 'c') {
|
|
373
|
+
interrupt();
|
|
374
|
+
cancel();
|
|
375
|
+
}
|
|
376
|
+
else if (key.escape)
|
|
377
|
+
cancel();
|
|
378
|
+
});
|
|
379
|
+
return (_jsx(Frame, { title: title, footer: "Esc \u53D6\u6D88", children: () => _jsx(Text, { children: message ?? '请稍候…' }) }));
|
|
380
|
+
}
|
|
381
|
+
function RunScreen({ view, interrupt }) {
|
|
382
|
+
const [page, setPage] = useState(0);
|
|
383
|
+
useInput((input, key) => {
|
|
384
|
+
if (key.ctrl && input === 'c')
|
|
385
|
+
interrupt();
|
|
386
|
+
else if (key.pageDown)
|
|
387
|
+
setPage(previous => previous + 1);
|
|
388
|
+
else if (key.pageUp)
|
|
389
|
+
setPage(previous => Math.max(0, previous - 1));
|
|
390
|
+
});
|
|
391
|
+
return (_jsx(Frame, { title: view.title, footer: "PgUp/PgDn \u67E5\u770B\u8FDB\u5EA6 \u00B7 Ctrl+C \u505C\u6B62\u540E\u7EED\u64CD\u4F5C", children: (height, width) => {
|
|
392
|
+
const lines = wrapAnsi(view.rows.map(statusLine).join('\n'), width, { hard: true }).split('\n');
|
|
393
|
+
const visible = Math.max(1, height - 2);
|
|
394
|
+
const start = Math.min(page * visible, Math.max(0, lines.length - visible));
|
|
395
|
+
return (_jsxs(_Fragment, { children: [_jsx(Text, { dimColor: true, children: view.summary }), _jsx(Text, { children: lines.slice(start, start + visible).join('\n') }), lines.length > visible && (_jsxs(Text, { dimColor: true, children: [start + 1, "\u2013", Math.min(lines.length, start + visible), "/", lines.length] }))] }));
|
|
396
|
+
} }));
|
|
397
|
+
}
|
|
398
|
+
function homeLayout(width, height) {
|
|
399
|
+
const showEnvironment = height >= LAYOUT.homeExpandedRows;
|
|
400
|
+
const showCredit = height >= LAYOUT.homeCreditRows;
|
|
401
|
+
const showLogo = showEnvironment && width >= LAYOUT.homeLogoColumns;
|
|
402
|
+
return {
|
|
403
|
+
showEnvironment,
|
|
404
|
+
showCredit,
|
|
405
|
+
showLogo,
|
|
406
|
+
rows: LAYOUT.homeFixedRows +
|
|
407
|
+
(showLogo ? BRAND.logo.length : 1) +
|
|
408
|
+
(showCredit ? 1 : 0) +
|
|
409
|
+
(showEnvironment ? LAYOUT.homeEnvironmentRows : showCredit ? 1 : 0),
|
|
410
|
+
};
|
|
411
|
+
}
|
|
412
|
+
function HomeHeader({ width, height }) {
|
|
413
|
+
const platform = process.platform === 'win32' ? 'Windows' : process.platform === 'darwin' ? 'macOS' : 'Linux';
|
|
414
|
+
const { showLogo, showCredit, showEnvironment } = homeLayout(width, height);
|
|
415
|
+
return (_jsxs(Box, { flexDirection: "column", children: [_jsx(Text, { color: ACCENT, children: '═'.repeat(Math.min(BANNER_WIDTH, width)) }), showLogo ? (BRAND.logo.map((line, index) => (_jsx(Text, { color: ACCENT, bold: true, children: ' ' + line }, index)))) : (_jsxs(Text, { bold: true, color: ACCENT, children: [' ', "CCW"] })), _jsxs(Text, { bold: true, wrap: "truncate-end", children: [' ', BRAND.title, " ", _jsxs(Text, { dimColor: true, children: ["V", BRAND.versionNumber] })] }), showCredit && _jsxs(Text, { dimColor: true, children: [" ", BRAND.credit] }), showEnvironment ? (_jsxs(_Fragment, { children: [_jsx(Text, { color: ACCENT, children: '─'.repeat(Math.min(BANNER_WIDTH, width)) }), _jsx(Text, { bold: true, children: " \u5F53\u524D\u73AF\u5883\u72B6\u6001" }), _jsxs(Text, { children: [' ', "1. \u7CFB\u7EDF ", platform, " (", os.arch(), ")"] }), _jsxs(Text, { children: [" 2. Node ", process.version] }), _jsxs(Text, { wrap: "truncate-end", children: [" 3. \u4E3B\u76EE\u5F55 ", safeText(os.homedir())] })] })) : (showCredit && (_jsxs(Text, { dimColor: true, wrap: "truncate-end", children: [' ', platform, " (", os.arch(), ") \u00B7 Node ", process.version] }))), _jsx(Text, { color: ACCENT, children: '═'.repeat(Math.min(BANNER_WIDTH, width)) }), _jsx(Text, { bold: true, children: "\u8BF7\u9009\u62E9\uFF1A" })] }));
|
|
416
|
+
}
|
|
417
|
+
function Frame({ title, footer, page = 0, reserveRows = LAYOUT.defaultContentRows, home = false, children, }) {
|
|
418
|
+
const size = useWindowSize();
|
|
419
|
+
const width = Math.max(LAYOUT.minimumColumns, size.columns - 1);
|
|
420
|
+
const height = Math.max(LAYOUT.minimumRows, size.rows - 1);
|
|
421
|
+
const [name = '', ...body] = safeText(title).split('\n');
|
|
422
|
+
const lines = wrapAnsi(body.join('\n'), width, { hard: true }).split('\n').filter(Boolean);
|
|
423
|
+
const detailHeight = Math.min(Math.max(1, height - reserveRows - LAYOUT.headerRows - LAYOUT.footerRows - LAYOUT.sectionGapRows), lines.length);
|
|
424
|
+
const start = Math.min(page * detailHeight, Math.max(0, lines.length - detailHeight));
|
|
425
|
+
const hasPages = lines.length > detailHeight;
|
|
426
|
+
const headerHeight = home ? homeLayout(width, height).rows : LAYOUT.headerRows + detailHeight + (hasPages ? 1 : 0);
|
|
427
|
+
const contentHeight = Math.max(1, height - headerHeight - LAYOUT.footerRows);
|
|
428
|
+
return (_jsxs(Box, { flexDirection: "column", width: width, height: height, children: [home ? (_jsx(HomeHeader, { width: width, height: height })) : (_jsxs(_Fragment, { children: [_jsx(Box, { borderStyle: "double", borderColor: ACCENT, paddingX: 1, width: Math.min(BANNER_WIDTH, width), flexShrink: 0, children: _jsxs(Text, { bold: true, color: ACCENT, wrap: "truncate-end", children: ["CCW \u00B7 ", name] }) }), lines.slice(start, start + detailHeight).map((line, index) => (_jsx(Text, { children: line }, index))), hasPages && (_jsxs(Text, { dimColor: true, children: ["PgUp/PgDn \u9605\u8BFB \u00B7 ", start + 1, "\u2013", Math.min(lines.length, start + detailHeight), "/", lines.length] }))] })), _jsx(Box, { flexDirection: "column", height: contentHeight, marginTop: 1, flexShrink: 0, children: children(contentHeight, width) }), _jsx(Box, { marginTop: 1, children: _jsx(Text, { dimColor: true, wrap: "truncate-end", children: footer }) })] }));
|
|
429
|
+
}
|
|
430
|
+
async function pauseReading() {
|
|
431
|
+
// Ink 使用 readable 模式,pause() 可能不发事件;通知 Node 停止底层读取后再交接
|
|
432
|
+
process.stdin.pause();
|
|
433
|
+
process.stdin.emit('pause');
|
|
434
|
+
await new Promise(resolve => process.nextTick(resolve));
|
|
435
|
+
}
|
|
436
|
+
function asError(error, fallback) {
|
|
437
|
+
return error instanceof Error ? error : new Error(fallback);
|
|
438
|
+
}
|