refactor(cli): improve terminal clearing in interactive menu
This commit is contained in:
@@ -283,9 +283,13 @@ async function showMenu(): Promise<void> {
|
||||
const actions = MENU_ACTIONS;
|
||||
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`);
|
||||
|
||||
const totalLines = headerLines + actions.length;
|
||||
|
||||
function render() {
|
||||
for (let i = 0; i < actions.length; i++) {
|
||||
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}`;
|
||||
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) {
|
||||
process.stdout.write(`\x1b[${n}A`);
|
||||
function clearMenu() {
|
||||
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) {
|
||||
@@ -339,8 +347,7 @@ async function showMenu(): Promise<void> {
|
||||
process.stdin.setRawMode(savedRaw === true);
|
||||
process.stdin.pause();
|
||||
process.stdin.removeAllListeners("data");
|
||||
for (let i = 0; i < actions.length; i++) process.stdout.write("\x1b[2K\n");
|
||||
moveUp(actions.length);
|
||||
clearMenu();
|
||||
process.stdout.write("\x1b[?25h");
|
||||
resolve();
|
||||
return;
|
||||
@@ -372,8 +379,7 @@ async function showMenu(): Promise<void> {
|
||||
process.stdin.pause();
|
||||
process.stdin.removeAllListeners("data");
|
||||
|
||||
for (let i = 0; i < actions.length; i++) process.stdout.write("\x1b[2K\n");
|
||||
moveUp(actions.length);
|
||||
clearMenu();
|
||||
process.stdout.write("\x1b[?25h");
|
||||
|
||||
if (selected.key === "commit") {
|
||||
|
||||
Reference in New Issue
Block a user