dotop-gpu-cli 0.0.0-stage → 0.1.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 +148 -2
- package/bin/dotop-gpu.mjs +597 -0
- package/package.json +33 -4
- package/src/api.mjs +214 -0
- package/src/config.mjs +91 -0
- package/src/prompts.mjs +529 -0
- package/src/render.mjs +185 -0
- package/src/rentargs.mjs +232 -0
- package/src/rentflow.mjs +186 -0
- package/src/sshkeys.mjs +69 -0
- package/src/wizard.mjs +1056 -0
package/src/prompts.mjs
ADDED
|
@@ -0,0 +1,529 @@
|
|
|
1
|
+
// 极简交互组件:单选(↑/↓ 光标 + Enter 确认)、多选、文本、确认。
|
|
2
|
+
// 只依赖 node:readline/promises,不引入任何第三方包。
|
|
3
|
+
//
|
|
4
|
+
// 三种输入模式:
|
|
5
|
+
// - TTY 单选:raw mode 直接读按键,↑/↓ 移动光标、Enter 确认;菜单原地重绘,不刷屏;
|
|
6
|
+
// - TTY 文本:走 readline;
|
|
7
|
+
// - 非 TTY(管道 / 重定向,如 CI、`echo ... | dotop-gpu`):先把 stdin 全部读成
|
|
8
|
+
// 一个行队列再按需取用。readline 在管道下会一次性收到所有数据并在 EOF 时
|
|
9
|
+
// 直接 close,来不及回答第二个问题,所以这里必须分开处理。
|
|
10
|
+
import { createInterface } from 'node:readline/promises';
|
|
11
|
+
import { stdin, stdout } from 'node:process';
|
|
12
|
+
import { paint } from './render.mjs';
|
|
13
|
+
|
|
14
|
+
let rl = null;
|
|
15
|
+
let piped = null; // 非 TTY 时的行队列
|
|
16
|
+
|
|
17
|
+
// stdin 是否已经耗尽(EOF)。
|
|
18
|
+
//
|
|
19
|
+
// 管道/重定向模式下这个必须对外可见:ask() 在 EOF 时会退回默认值,而某些步骤的
|
|
20
|
+
// 默认值是「破坏性」的(比如确认下单)。如果调用方不知道「这其实是没输入了」,
|
|
21
|
+
// 就可能在不该下单的时候把单下了。危险步骤要自己判断这个标志。
|
|
22
|
+
let exhausted = false;
|
|
23
|
+
|
|
24
|
+
export function inputExhausted() {
|
|
25
|
+
return exhausted;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function isTTY() {
|
|
29
|
+
return !!stdin.isTTY;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async function readPiped() {
|
|
33
|
+
if (piped) return piped;
|
|
34
|
+
const chunks = [];
|
|
35
|
+
for await (const chunk of stdin) chunks.push(chunk);
|
|
36
|
+
const text = Buffer.concat(chunks.map((c) => (Buffer.isBuffer(c) ? c : Buffer.from(c)))).toString('utf8');
|
|
37
|
+
piped = text.split(/\r?\n/);
|
|
38
|
+
// 结尾换行会产生一个空串,去掉它,避免最后一个问题读到「空行」而不是 EOF
|
|
39
|
+
if (piped.length && piped[piped.length - 1] === '') piped.pop();
|
|
40
|
+
return piped;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function ensureRL() {
|
|
44
|
+
if (!rl) rl = createInterface({ input: stdin, output: stdout });
|
|
45
|
+
return rl;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function closeRL() {
|
|
49
|
+
if (rl) {
|
|
50
|
+
rl.close();
|
|
51
|
+
rl = null;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// 统一取一行的入口:TTY 走 readline,非 TTY 走队列
|
|
56
|
+
async function readLine() {
|
|
57
|
+
if (!isTTY()) {
|
|
58
|
+
const lines = await readPiped();
|
|
59
|
+
if (!lines.length) {
|
|
60
|
+
exhausted = true;
|
|
61
|
+
return null;
|
|
62
|
+
}
|
|
63
|
+
return lines.shift();
|
|
64
|
+
}
|
|
65
|
+
// 单选菜单退出时 pause() 过 stdin,这里必须重新 resume,
|
|
66
|
+
// 否则 readline 挂在暂停的流上等不到数据,表现为「卡住不动」。
|
|
67
|
+
stdin.resume();
|
|
68
|
+
const answer = await ensureRL().question('');
|
|
69
|
+
if (answer === undefined || answer === null) {
|
|
70
|
+
exhausted = true;
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
return answer;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export async function ask(prompt, { defaultValue = '' } = {}) {
|
|
77
|
+
const suffix = defaultValue ? ` (${defaultValue})` : '';
|
|
78
|
+
stdout.write(`${prompt}${suffix}: `);
|
|
79
|
+
const raw = await readLine();
|
|
80
|
+
if (raw === null) {
|
|
81
|
+
// 输入耗尽:用默认值继续,没有默认值就返回空串
|
|
82
|
+
stdout.write('\n');
|
|
83
|
+
return defaultValue;
|
|
84
|
+
}
|
|
85
|
+
const answer = String(raw).trim();
|
|
86
|
+
return answer || defaultValue;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export async function askPassword(prompt) {
|
|
90
|
+
// readline 没有内置隐藏输入;Key 不长且只落本地 600 文件,普通输入可接受
|
|
91
|
+
return await ask(prompt);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// 是/否确认。
|
|
95
|
+
//
|
|
96
|
+
// 这个提示的设计要求(吃过两次亏):
|
|
97
|
+
// 1. **操作说明必须单独一行、放在输入光标正上方**。塞在问题正文末尾会被指纹等
|
|
98
|
+
// 附加信息挤走,用户滚屏后只看到「确认删除公钥X?」却看不到要按什么。
|
|
99
|
+
// 2. **两个动作都要写出来**:按什么确认、按什么取消、直接回车等于什么。
|
|
100
|
+
// 只给一个 [y/N] 符号,中文用户会猜错。
|
|
101
|
+
// 3. **y/n 以外的输入要重新问**,不能默默按默认值处理 —— 用户可能输入 1 以为
|
|
102
|
+
// 「选完序号就确认了」,静默按默认值会把想删的当成放弃。
|
|
103
|
+
//
|
|
104
|
+
// actionYes / actionNo 由调用方给出具体动词(删除 / 销毁 / 上传 / 跳过)。
|
|
105
|
+
export async function confirm(
|
|
106
|
+
prompt,
|
|
107
|
+
{ defaultValue = false, actionYes = '确认', actionNo = '取消' } = {},
|
|
108
|
+
) {
|
|
109
|
+
// 提示只保留最必要的信息:怎么按键 + 两个键各自做什么。
|
|
110
|
+
// 「直接回车 = 哪个」单独一行小字,避免和主提示挤成一句话。
|
|
111
|
+
const head = `${prompt}\n [y/n] y ${actionYes},n ${actionNo}`;
|
|
112
|
+
const tail = paint.dim(` (直接回车 = ${defaultValue ? 'y' : 'n'},即 ${defaultValue ? actionYes : actionNo})`);
|
|
113
|
+
stdout.write(`${head}\n${tail}\n`);
|
|
114
|
+
|
|
115
|
+
for (;;) {
|
|
116
|
+
stdout.write('> ');
|
|
117
|
+
const raw = await readLine();
|
|
118
|
+
if (raw === null) {
|
|
119
|
+
// 输入已耗尽(管道读完),按默认值处理
|
|
120
|
+
stdout.write('\n');
|
|
121
|
+
return defaultValue;
|
|
122
|
+
}
|
|
123
|
+
const answer = String(raw).trim().toLowerCase();
|
|
124
|
+
if (answer === '') return defaultValue;
|
|
125
|
+
if (answer === 'y' || answer === 'yes' || answer === '是') return true;
|
|
126
|
+
if (answer === 'n' || answer === 'no' || answer === '否') return false;
|
|
127
|
+
stdout.write(` 只接受 y 或 n(回车 = ${defaultValue ? 'y' : 'n'})。\n`);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// ─── 按键读取(TTY raw mode) ──────────────────────────────
|
|
132
|
+
//
|
|
133
|
+
// 单选菜单要支持 ↑/↓ 就必须拿到「单次按键」而不是「一行文本」,readline 做不到,
|
|
134
|
+
// 所以在 TTY 下临时切 raw mode 直接读 stdin 的 data 事件,选完立刻还原。
|
|
135
|
+
//
|
|
136
|
+
// 注意:进入 raw 模式前先把 readline 关掉,否则两边同时读同一个 stdin 会抢输入。
|
|
137
|
+
|
|
138
|
+
const K = {
|
|
139
|
+
up: 'up',
|
|
140
|
+
down: 'down',
|
|
141
|
+
left: 'left',
|
|
142
|
+
enter: 'enter',
|
|
143
|
+
backspace: 'backspace',
|
|
144
|
+
abort: 'abort',
|
|
145
|
+
digit: 'digit',
|
|
146
|
+
char: 'char',
|
|
147
|
+
other: 'other',
|
|
148
|
+
eof: 'eof',
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
// 把一段 stdin 数据解析成按键序列。方向键是 3 字节的 CSI 序列,
|
|
152
|
+
// 不能逐字符处理,否则 ESC 会被当成一个独立按键。
|
|
153
|
+
function parseKeys(chunk) {
|
|
154
|
+
const s = String(chunk);
|
|
155
|
+
const out = [];
|
|
156
|
+
let i = 0;
|
|
157
|
+
while (i < s.length) {
|
|
158
|
+
const c = s[i];
|
|
159
|
+
if (c === '\x1b') {
|
|
160
|
+
if (s.startsWith('\x1b[A', i) || s.startsWith('\x1bOA', i)) {
|
|
161
|
+
out.push({ type: K.up });
|
|
162
|
+
i += 3;
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
if (s.startsWith('\x1b[B', i) || s.startsWith('\x1bOB', i)) {
|
|
166
|
+
out.push({ type: K.down });
|
|
167
|
+
i += 3;
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
if (s.startsWith('\x1b[D', i) || s.startsWith('\x1bOD', i)) {
|
|
171
|
+
out.push({ type: K.left });
|
|
172
|
+
i += 3;
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
// Delete / Home / PageUp 等:整段忽略,别把 ESC 单独抛给调用方
|
|
176
|
+
const m = /^\x1b\[[0-9;]*[A-Za-z]/.exec(s.slice(i));
|
|
177
|
+
i += m ? m[0].length : 1;
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
180
|
+
if (c === '\r' || c === '\n') {
|
|
181
|
+
out.push({ type: K.enter });
|
|
182
|
+
i += 1;
|
|
183
|
+
continue;
|
|
184
|
+
}
|
|
185
|
+
if (c === '\x03') {
|
|
186
|
+
out.push({ type: K.abort });
|
|
187
|
+
i += 1;
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
if (c === '\x7f' || c === '\b') {
|
|
191
|
+
out.push({ type: K.backspace });
|
|
192
|
+
i += 1;
|
|
193
|
+
continue;
|
|
194
|
+
}
|
|
195
|
+
if (c >= '0' && c <= '9') {
|
|
196
|
+
out.push({ type: K.digit, value: c });
|
|
197
|
+
i += 1;
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
200
|
+
out.push({ type: K.char, value: c });
|
|
201
|
+
i += 1;
|
|
202
|
+
}
|
|
203
|
+
return out;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
let keyQueue = [];
|
|
207
|
+
let keyWaiter = null;
|
|
208
|
+
let rawAttached = false;
|
|
209
|
+
|
|
210
|
+
function onRawData(chunk) {
|
|
211
|
+
for (const key of parseKeys(chunk)) {
|
|
212
|
+
if (keyWaiter) {
|
|
213
|
+
const resolve = keyWaiter;
|
|
214
|
+
keyWaiter = null;
|
|
215
|
+
resolve(key);
|
|
216
|
+
} else {
|
|
217
|
+
keyQueue.push(key);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
function onRawEnd() {
|
|
223
|
+
if (keyWaiter) {
|
|
224
|
+
const resolve = keyWaiter;
|
|
225
|
+
keyWaiter = null;
|
|
226
|
+
resolve({ type: K.eof });
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
function attachRaw() {
|
|
231
|
+
if (rawAttached) return;
|
|
232
|
+
closeRL(); // readline 和 raw 模式不能同时读 stdin
|
|
233
|
+
stdin.setRawMode(true);
|
|
234
|
+
stdin.resume();
|
|
235
|
+
stdin.on('data', onRawData);
|
|
236
|
+
stdin.on('end', onRawEnd);
|
|
237
|
+
rawAttached = true;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
function detachRaw() {
|
|
241
|
+
if (!rawAttached) return;
|
|
242
|
+
stdin.removeListener('data', onRawData);
|
|
243
|
+
stdin.removeListener('end', onRawEnd);
|
|
244
|
+
stdin.setRawMode(false);
|
|
245
|
+
stdin.pause();
|
|
246
|
+
rawAttached = false;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function readKey() {
|
|
250
|
+
if (keyQueue.length) return Promise.resolve(keyQueue.shift());
|
|
251
|
+
if (stdin.readableEnded) return Promise.resolve({ type: K.eof });
|
|
252
|
+
attachRaw();
|
|
253
|
+
return new Promise((resolve) => {
|
|
254
|
+
keyWaiter = resolve;
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
function abortOut() {
|
|
259
|
+
detachRaw();
|
|
260
|
+
stdout.write('\n');
|
|
261
|
+
process.exit(130);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// 原地重绘一块菜单:先上移已打印的行数,再逐行清行 + 重写。
|
|
265
|
+
// 这样按 ↑/↓ 时光标只是在菜单里移动,终端不会一行行往下滚。
|
|
266
|
+
class Block {
|
|
267
|
+
constructor() {
|
|
268
|
+
this.lines = 0;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
draw(lines) {
|
|
272
|
+
if (this.lines > 0) stdout.write(`\x1b[${this.lines}A`);
|
|
273
|
+
for (const line of lines) stdout.write(`\x1b[2K${line}\n`);
|
|
274
|
+
this.lines = lines.length;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// 收尾:把菜单留在屏幕上(保留最后状态),光标移到菜单下方
|
|
278
|
+
finish() {
|
|
279
|
+
this.lines = 0;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
// 单选:TTY 下 ↑/↓ 移动光标、Enter 确认;非 TTY 下按序号回答。
|
|
284
|
+
//
|
|
285
|
+
// allowBack=true 时多接受一个「b / 上一步」输入(TTY 下也可以按 ←),命中就返回
|
|
286
|
+
// BACK 哨兵,由调用方的步骤机负责回退。这样把「返回上一步」的能力收在提示层,
|
|
287
|
+
// 每个步骤不用各自实现一遍。
|
|
288
|
+
export const BACK = Symbol('back');
|
|
289
|
+
|
|
290
|
+
export async function selectOne(
|
|
291
|
+
title,
|
|
292
|
+
items,
|
|
293
|
+
{ render, defaultIndex = 0, allowBack = false, backLabel = '返回上一步', explicitOnly = false } = {},
|
|
294
|
+
) {
|
|
295
|
+
if (!items || items.length === 0) return null;
|
|
296
|
+
|
|
297
|
+
if (!isTTY()) {
|
|
298
|
+
return await selectOnePiped(title, items, { render, defaultIndex, allowBack, backLabel, explicitOnly });
|
|
299
|
+
}
|
|
300
|
+
return await selectOneTTY(title, items, { render, defaultIndex, allowBack, backLabel });
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
function optionText(render, item, i) {
|
|
304
|
+
return render ? render(item, i) : String(item);
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
async function selectOnePiped(title, items, { render, defaultIndex, allowBack, backLabel, explicitOnly }) {
|
|
308
|
+
stdout.write(`\n${title}\n`);
|
|
309
|
+
items.forEach((item, i) => {
|
|
310
|
+
const mark = i === defaultIndex ? '›' : ' ';
|
|
311
|
+
stdout.write(` ${mark} ${i + 1}) ${optionText(render, item, i)}\n`);
|
|
312
|
+
});
|
|
313
|
+
if (allowBack) stdout.write(` b) ← ${backLabel}\n`);
|
|
314
|
+
|
|
315
|
+
while (true) {
|
|
316
|
+
// 输入已经耗尽:不能再退回默认值重问,否则「售罄→退回上一步」这类
|
|
317
|
+
// 循环会拿默认答案无限转下去。返回 null 让调用方正常收尾。
|
|
318
|
+
if (exhausted) return null;
|
|
319
|
+
const hint = allowBack ? `请选择 1-${items.length},或 b 返回` : `请选择 1-${items.length}`;
|
|
320
|
+
// explicitOnly:不给默认值是关键。管道输入里一个空行(比如喂的行数多了一行)
|
|
321
|
+
// 会被 ask() 当成「取默认值」,而确认下单页的默认值就是「确认下单」——
|
|
322
|
+
// 2026-12-04 就这样真的发出过一次下单请求。会产生费用的步骤必须靠显式序号确认。
|
|
323
|
+
const raw = await ask(hint, { defaultValue: explicitOnly ? '' : String(defaultIndex + 1) });
|
|
324
|
+
if (exhausted) return null;
|
|
325
|
+
if (allowBack && /^(b|back|上一步|返回)$/i.test(raw)) return BACK;
|
|
326
|
+
if (explicitOnly && !String(raw).trim()) {
|
|
327
|
+
stdout.write(' 这一步会产生费用,请明确输入序号(直接回车不再等于确认)。\n');
|
|
328
|
+
continue;
|
|
329
|
+
}
|
|
330
|
+
const n = Number.parseInt(raw, 10);
|
|
331
|
+
if (Number.isInteger(n) && n >= 1 && n <= items.length) return items[n - 1];
|
|
332
|
+
stdout.write(` 输入无效,请输入 1 到 ${items.length} 之间的序号。\n`);
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
async function selectOneTTY(title, items, { render, defaultIndex, allowBack, backLabel }) {
|
|
337
|
+
let index = Math.min(Math.max(defaultIndex, 0), items.length - 1);
|
|
338
|
+
const block = new Block();
|
|
339
|
+
const hint = allowBack
|
|
340
|
+
? '↑/↓ 移动,Enter 确认,← 或 b 返回'
|
|
341
|
+
: '↑/↓ 移动,Enter 确认';
|
|
342
|
+
|
|
343
|
+
const draw = () => {
|
|
344
|
+
const lines = ['', title];
|
|
345
|
+
items.forEach((item, i) => {
|
|
346
|
+
// 选中项整行上色(不只靠 `›` 前缀):光标停在哪一行一眼能看出来。
|
|
347
|
+
const row = ` ${i === index ? '›' : ' '} ${i + 1}) ${optionText(render, item, i)}`;
|
|
348
|
+
lines.push(i === index ? paint.highlight(row) : row);
|
|
349
|
+
});
|
|
350
|
+
if (allowBack) lines.push(` b) ← ${backLabel}`);
|
|
351
|
+
lines.push(` ${paint.dim(hint)}`);
|
|
352
|
+
block.draw(lines);
|
|
353
|
+
};
|
|
354
|
+
|
|
355
|
+
draw();
|
|
356
|
+
for (;;) {
|
|
357
|
+
const key = await readKey();
|
|
358
|
+
if (key.type === K.abort) abortOut();
|
|
359
|
+
if (key.type === K.eof) {
|
|
360
|
+
block.finish();
|
|
361
|
+
detachRaw();
|
|
362
|
+
exhausted = true;
|
|
363
|
+
return null;
|
|
364
|
+
}
|
|
365
|
+
if (key.type === K.up) {
|
|
366
|
+
index = (index - 1 + items.length) % items.length;
|
|
367
|
+
draw();
|
|
368
|
+
continue;
|
|
369
|
+
}
|
|
370
|
+
if (key.type === K.down) {
|
|
371
|
+
index = (index + 1) % items.length;
|
|
372
|
+
draw();
|
|
373
|
+
continue;
|
|
374
|
+
}
|
|
375
|
+
if (key.type === K.enter) {
|
|
376
|
+
block.finish();
|
|
377
|
+
detachRaw();
|
|
378
|
+
return items[index];
|
|
379
|
+
}
|
|
380
|
+
if (allowBack && key.type === K.left) {
|
|
381
|
+
block.finish();
|
|
382
|
+
detachRaw();
|
|
383
|
+
return BACK;
|
|
384
|
+
}
|
|
385
|
+
if (allowBack && (key.type === K.char && /^(b|B)$/.test(key.value))) {
|
|
386
|
+
block.finish();
|
|
387
|
+
detachRaw();
|
|
388
|
+
return BACK;
|
|
389
|
+
}
|
|
390
|
+
if (key.type === K.digit) {
|
|
391
|
+
// 数字仍然可以直接跳选,老习惯不用改
|
|
392
|
+
const n = Number.parseInt(key.value, 10);
|
|
393
|
+
if (n >= 1 && n <= items.length) {
|
|
394
|
+
index = n - 1;
|
|
395
|
+
draw();
|
|
396
|
+
} else {
|
|
397
|
+
stdout.write(`\x07`); // 响铃提示:这个序号不存在
|
|
398
|
+
}
|
|
399
|
+
continue;
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
// 期数选择:默认 1 期,↑/↓ 在 1..maxPeriod 里挑,Enter 确认;
|
|
405
|
+
// 直接输入数字可以选到列表之外(比如 24 期),Backspace 退格。
|
|
406
|
+
// maxInput 是服务端允许的上限(gpu.MaxCyclePeriods = 99),超了直接夹住,
|
|
407
|
+
// 免得用户输个 120 再到服务端吃一个 INVALID_PARAMS。
|
|
408
|
+
// 返回 null 表示取消(输入耗尽 / Ctrl+C)。
|
|
409
|
+
export async function selectCount(
|
|
410
|
+
title,
|
|
411
|
+
{ current = 1, maxPeriod = 12, maxInput = 99, allowBack = false } = {},
|
|
412
|
+
) {
|
|
413
|
+
const base = Number.isInteger(current) && current > 0 ? current : 1;
|
|
414
|
+
|
|
415
|
+
if (!isTTY()) {
|
|
416
|
+
const raw = await ask('期数', { defaultValue: String(base) });
|
|
417
|
+
if (exhausted) return null;
|
|
418
|
+
if (allowBack && /^(b|back|上一步|返回)$/i.test(raw)) return BACK;
|
|
419
|
+
const n = Number.parseInt(raw, 10);
|
|
420
|
+
if (!Number.isInteger(n) || n <= 0) return base;
|
|
421
|
+
return Math.min(n, maxInput);
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
const block = new Block();
|
|
425
|
+
let typed = '';
|
|
426
|
+
let index = Math.min(base, maxPeriod) - 1;
|
|
427
|
+
|
|
428
|
+
const value = () => (typed ? Math.min(Number.parseInt(typed, 10), maxInput) : index + 1);
|
|
429
|
+
|
|
430
|
+
const draw = () => {
|
|
431
|
+
const lines = ['', title];
|
|
432
|
+
for (let i = 0; i < maxPeriod; i += 1) {
|
|
433
|
+
const n = i + 1;
|
|
434
|
+
const active = !typed && i === index;
|
|
435
|
+
const row = ` ${active ? '›' : ' '} ${n} 期${n === 1 ? '(默认)' : ''}`;
|
|
436
|
+
lines.push(active ? paint.highlight(row) : row);
|
|
437
|
+
}
|
|
438
|
+
// 键入数字时没有光标项,把「当前会提交几期」直接标出来,
|
|
439
|
+
// 否则用户看不到自己敲的数字到底生效没有。
|
|
440
|
+
const typedRow = typed ? ` 将购买 ${value()} 期` : null;
|
|
441
|
+
if (typedRow) lines.push(paint.highlight(typedRow));
|
|
442
|
+
lines.push(
|
|
443
|
+
paint.dim(
|
|
444
|
+
typed
|
|
445
|
+
? 'Enter 确认,Backspace 重来'
|
|
446
|
+
: `↑/↓ 移动,Enter 确认;1-${maxPeriod} 之外可直接输入数字(最多 ${maxInput} 期)`,
|
|
447
|
+
),
|
|
448
|
+
);
|
|
449
|
+
if (allowBack) lines.push(paint.dim(' b 返回,不改了'));
|
|
450
|
+
block.draw(lines);
|
|
451
|
+
};
|
|
452
|
+
|
|
453
|
+
draw();
|
|
454
|
+
for (;;) {
|
|
455
|
+
const key = await readKey();
|
|
456
|
+
if (key.type === K.abort) abortOut();
|
|
457
|
+
if (key.type === K.eof) {
|
|
458
|
+
block.finish();
|
|
459
|
+
detachRaw();
|
|
460
|
+
exhausted = true;
|
|
461
|
+
return null;
|
|
462
|
+
}
|
|
463
|
+
if (key.type === K.up) {
|
|
464
|
+
typed = '';
|
|
465
|
+
index = (index - 1 + maxPeriod) % maxPeriod;
|
|
466
|
+
draw();
|
|
467
|
+
continue;
|
|
468
|
+
}
|
|
469
|
+
if (key.type === K.down) {
|
|
470
|
+
typed = '';
|
|
471
|
+
index = (index + 1) % maxPeriod;
|
|
472
|
+
draw();
|
|
473
|
+
continue;
|
|
474
|
+
}
|
|
475
|
+
if (key.type === K.digit) {
|
|
476
|
+
typed = `${typed}${key.value}`.replace(/^0+(?=\d)/, '').slice(0, 4);
|
|
477
|
+
draw();
|
|
478
|
+
continue;
|
|
479
|
+
}
|
|
480
|
+
if (key.type === K.backspace) {
|
|
481
|
+
typed = typed.slice(0, -1);
|
|
482
|
+
draw();
|
|
483
|
+
continue;
|
|
484
|
+
}
|
|
485
|
+
if (key.type === K.enter) {
|
|
486
|
+
const n = value();
|
|
487
|
+
block.finish();
|
|
488
|
+
detachRaw();
|
|
489
|
+
return Number.isInteger(n) && n > 0 ? n : base;
|
|
490
|
+
}
|
|
491
|
+
if (allowBack && key.type === K.char && /^(b|B)$/.test(key.value)) {
|
|
492
|
+
block.finish();
|
|
493
|
+
detachRaw();
|
|
494
|
+
return BACK;
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
// 把 stdin 数据解析成按键序列的能力对外暴露,便于单测(方向键是 3 字节 CSI 序列,
|
|
500
|
+
// 不能逐字符处理)。
|
|
501
|
+
export { parseKeys as parseKeysForTest };
|
|
502
|
+
|
|
503
|
+
// 多选:逗号分隔序号,返回选中项数组(保持行输入,多选场景用光标不直观)
|
|
504
|
+
export async function selectMany(title, items, { render, defaultIndexes = [] } = {}) {
|
|
505
|
+
if (!items || items.length === 0) return [];
|
|
506
|
+
stdout.write(`\n${title}\n`);
|
|
507
|
+
items.forEach((item, i) => {
|
|
508
|
+
const mark = defaultIndexes.includes(i) ? '›' : ' ';
|
|
509
|
+
stdout.write(` ${mark} ${i + 1}) ${optionText(render, item, i)}\n`);
|
|
510
|
+
});
|
|
511
|
+
|
|
512
|
+
while (true) {
|
|
513
|
+
const def = defaultIndexes.map((i) => i + 1).join(',');
|
|
514
|
+
const raw = await ask('请选择(逗号分隔序号,可留空表示全不选)', { defaultValue: def });
|
|
515
|
+
if (!raw) return [];
|
|
516
|
+
const picks = raw
|
|
517
|
+
.split(/[,,\s]+/)
|
|
518
|
+
.filter(Boolean)
|
|
519
|
+
.map((s) => Number.parseInt(s, 10));
|
|
520
|
+
if (picks.every((n) => Number.isInteger(n) && n >= 1 && n <= items.length)) {
|
|
521
|
+
return [...new Set(picks)].map((n) => items[n - 1]);
|
|
522
|
+
}
|
|
523
|
+
stdout.write(` 输入无效,请用逗号分隔 1 到 ${items.length} 之间的序号。\n`);
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
export async function pressEnter(prompt = '按回车继续') {
|
|
528
|
+
await ask(prompt);
|
|
529
|
+
}
|