diff --git a/index.ts b/index.ts index 2be4127..eec1d65 100644 --- a/index.ts +++ b/index.ts @@ -283,9 +283,13 @@ async function showMenu(): Promise { 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 { 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 { 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 { 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") {