refactor(cli): improve terminal clearing in interactive menu

This commit is contained in:
2026-06-09 17:59:21 +08:00
parent 50dd0a7201
commit 309b6e94d4
+14 -8
View File
@@ -283,9 +283,13 @@ async function showMenu(): Promise<void> {
const actions = MENU_ACTIONS; const actions = MENU_ACTIONS;
let cursor = 0; let cursor = 0;
console.log(`\n ${BOLD}gai${RESET}\n`); const headerLines = 4;
process.stdout.write(`\n ${BOLD}gai${RESET}\n`);
process.stdout.write(` ${DIM}↑/↓ navigate, space/enter select${RESET}\n\n`); process.stdout.write(` ${DIM}↑/↓ navigate, space/enter select${RESET}\n\n`);
const totalLines = headerLines + actions.length;
function render() { function render() {
for (let i = 0; i < actions.length; i++) { for (let i = 0; i < actions.length; i++) {
process.stdout.write("\x1b[2K\r"); process.stdout.write("\x1b[2K\r");
@@ -296,11 +300,15 @@ async function showMenu(): Promise<void> {
const desc = i === cursor ? a.description : `${DIM}${a.description}${RESET}`; const desc = i === cursor ? a.description : `${DIM}${a.description}${RESET}`;
process.stdout.write(`${pointer} ${dot} ${name}${" ".repeat(Math.max(1, 14 - a.label.length))}${desc}\n`); process.stdout.write(`${pointer} ${dot} ${name}${" ".repeat(Math.max(1, 14 - a.label.length))}${desc}\n`);
} }
moveUp(actions.length); process.stdout.write(`\x1b[${actions.length}A`);
} }
function moveUp(n: number) { function clearMenu() {
process.stdout.write(`\x1b[${n}A`); process.stdout.write(`\x1b[${totalLines}A`);
for (let i = 0; i < totalLines; i++) {
process.stdout.write("\x1b[2K\x1b[1B");
}
process.stdout.write(`\x1b[${totalLines}A`);
} }
if (!process.stdin.isTTY) { if (!process.stdin.isTTY) {
@@ -339,8 +347,7 @@ async function showMenu(): Promise<void> {
process.stdin.setRawMode(savedRaw === true); process.stdin.setRawMode(savedRaw === true);
process.stdin.pause(); process.stdin.pause();
process.stdin.removeAllListeners("data"); process.stdin.removeAllListeners("data");
for (let i = 0; i < actions.length; i++) process.stdout.write("\x1b[2K\n"); clearMenu();
moveUp(actions.length);
process.stdout.write("\x1b[?25h"); process.stdout.write("\x1b[?25h");
resolve(); resolve();
return; return;
@@ -372,8 +379,7 @@ async function showMenu(): Promise<void> {
process.stdin.pause(); process.stdin.pause();
process.stdin.removeAllListeners("data"); process.stdin.removeAllListeners("data");
for (let i = 0; i < actions.length; i++) process.stdout.write("\x1b[2K\n"); clearMenu();
moveUp(actions.length);
process.stdout.write("\x1b[?25h"); process.stdout.write("\x1b[?25h");
if (selected.key === "commit") { if (selected.key === "commit") {