优化代码

This commit is contained in:
yinsx
2025-12-20 13:02:42 +08:00
parent d9abc57b0b
commit 16ae511f21
8 changed files with 30 additions and 482 deletions

View File

@ -34,7 +34,24 @@ export function createStepUI(options) {
lines.push(color.dim(step.emptyMessage || "无可用选项"));
return lines.join("\n");
}
step.options.forEach((opt, i) => {
// 计算可显示的行数(终端高度 - 标题/导航/提示占用的行数)
const termHeight = process.stdout.rows || 24;
const reservedLines = 8;
const maxVisible = Math.max(5, termHeight - reservedLines);
const total = step.options.length;
// 计算滚动窗口
let start = 0;
if (total > maxVisible) {
start = Math.max(0, Math.min(currentOption - Math.floor(maxVisible / 2), total - maxVisible));
}
const end = Math.min(start + maxVisible, total);
if (start > 0) lines.push(color.dim(" ↑ 更多选项..."));
for (let i = start; i < end; i++) {
const opt = step.options[i];
const isCurrent = i === currentOption;
const isSelected = step.type === "multiselect"
? results[currentStep]?.includes(opt.value)
@ -46,13 +63,15 @@ export function createStepUI(options) {
const label = isCurrent ? color.cyan(opt.label) : opt.label;
const check = isSelected ? color.green(" ✓") : "";
lines.push(cursor + prefix + label + check);
if (opt.hint) lines.push(" " + color.dim(opt.hint));
});
}
if (end < total) lines.push(color.dim(" ↓ 更多选项..."));
return lines.join("\n");
}
function render() {
console.clear();
process.stdout.write('\x1Bc');
console.log(color.bgCyan(color.black(` ${title} `)));
console.log("\n" + renderNav());
console.log(color.dim("\n← → 切换步骤 | ↑ ↓ 选择 | Space 选中 | Tab 提交 | Esc 返回\n"));