refactor(cli): extract interactive menu logic into reusable module
This commit is contained in:
@@ -13,6 +13,7 @@ import {
|
||||
commit,
|
||||
} from "./src/git";
|
||||
import { selectFiles } from "./src/selector";
|
||||
import { selectOne } from "./src/menu";
|
||||
import { collectProjectContext } from "./src/context";
|
||||
import { buildPrompt, SYSTEM_PROMPT } from "./src/prompt";
|
||||
import { generateCommitMessage } from "./src/ai";
|
||||
@@ -286,7 +287,7 @@ function printCommitResult(
|
||||
}
|
||||
|
||||
interface MenuAction {
|
||||
key: string;
|
||||
key: "commit" | "pr" | "config";
|
||||
label: string;
|
||||
description: string;
|
||||
}
|
||||
@@ -298,233 +299,39 @@ const MENU_ACTIONS: MenuAction[] = [
|
||||
];
|
||||
|
||||
async function showMenu(): Promise<void> {
|
||||
const actions = MENU_ACTIONS;
|
||||
let cursor = 0;
|
||||
|
||||
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");
|
||||
const a = actions[i]!;
|
||||
const pointer = i === cursor ? `${CYAN}❯${RESET} ` : " ";
|
||||
const dot = i === cursor ? `${GREEN}◉${RESET}` : `${DIM}○${RESET}`;
|
||||
const name = i === cursor ? `${BOLD}${a.label}${RESET}` : a.label;
|
||||
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(`\x1b[${actions.length}A`);
|
||||
}
|
||||
|
||||
function clearMenu() {
|
||||
process.stdout.write(`\x1b[${headerLines}A`);
|
||||
for (let i = 0; i < totalLines; i++) {
|
||||
process.stdout.write("\r\x1b[2K\n");
|
||||
}
|
||||
process.stdout.write(`\x1b[${totalLines}A`);
|
||||
}
|
||||
|
||||
if (!process.stdin.isTTY) {
|
||||
console.error(` ${RED}Error: Interactive menu requires a TTY.${RESET}`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const savedRaw = process.stdin.isRaw;
|
||||
process.stdin.setRawMode(true);
|
||||
process.stdin.resume();
|
||||
process.stdout.write("\x1b[?25l");
|
||||
|
||||
render();
|
||||
|
||||
return new Promise((resolve) => {
|
||||
let escapeBuf = "";
|
||||
|
||||
function handleSeq(seq: string) {
|
||||
if (seq === "\x1b[A" || seq === "\x1bOA") {
|
||||
if (cursor > 0) {
|
||||
cursor--;
|
||||
render();
|
||||
}
|
||||
} else if (seq === "\x1b[B" || seq === "\x1bOB") {
|
||||
if (cursor < actions.length - 1) {
|
||||
cursor++;
|
||||
render();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
process.stdin.on("data", (data: Buffer) => {
|
||||
const key = data.toString();
|
||||
|
||||
if (key === "\x03") {
|
||||
process.stdin.setRawMode(savedRaw === true);
|
||||
process.stdin.pause();
|
||||
process.stdin.removeAllListeners("data");
|
||||
clearMenu();
|
||||
process.stdout.write("\x1b[?25h");
|
||||
resolve();
|
||||
return;
|
||||
}
|
||||
|
||||
if (key === "\x1b" || key.startsWith("\x1b[")) {
|
||||
escapeBuf = key;
|
||||
if (key.length >= 3) {
|
||||
handleSeq(key);
|
||||
escapeBuf = "";
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (escapeBuf) {
|
||||
escapeBuf += key;
|
||||
if (/^[A-Za-z~]$/.test(key)) {
|
||||
handleSeq(escapeBuf);
|
||||
escapeBuf = "";
|
||||
} else if (escapeBuf.length > 8) {
|
||||
escapeBuf = "";
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (key === " " || key === "\r") {
|
||||
const selected = actions[cursor]!;
|
||||
process.stdin.setRawMode(savedRaw === true);
|
||||
process.stdin.pause();
|
||||
process.stdin.removeAllListeners("data");
|
||||
|
||||
clearMenu();
|
||||
process.stdout.write("\x1b[?25h");
|
||||
|
||||
if (selected.key === "commit") {
|
||||
handleCommit(false, false).then(resolve);
|
||||
} else if (selected.key === "pr") {
|
||||
handlePR(false).then(resolve);
|
||||
} else if (selected.key === "config") {
|
||||
handleConfig().then(resolve);
|
||||
} else {
|
||||
resolve();
|
||||
}
|
||||
return;
|
||||
}
|
||||
});
|
||||
const selected = await selectOne({
|
||||
title: "gai",
|
||||
subtitle: "Choose a workflow",
|
||||
items: MENU_ACTIONS.map((action) => ({
|
||||
label: action.label,
|
||||
value: action.key,
|
||||
description: action.description,
|
||||
})),
|
||||
});
|
||||
|
||||
if (selected === "commit") await handleCommit(false, false);
|
||||
if (selected === "pr") await handlePR(false);
|
||||
if (selected === "config") await handleConfig();
|
||||
}
|
||||
|
||||
async function selectPlatform(hostname: string): Promise<Platform | null> {
|
||||
const options = [
|
||||
{ platform: "github" as Platform, label: "GitHub", desc: "gh CLI" },
|
||||
{ platform: "gitea" as Platform, label: "Gitea", desc: "tea CLI" },
|
||||
];
|
||||
let cursor = 0;
|
||||
|
||||
const headerLines = 4;
|
||||
|
||||
process.stdout.write(`\n Remote: ${CYAN}${hostname}${RESET} — could not auto-detect platform.\n`);
|
||||
process.stdout.write(` ${DIM}↑/↓ navigate, space/enter select${RESET}\n\n`);
|
||||
|
||||
const totalLines = headerLines + options.length;
|
||||
|
||||
function render() {
|
||||
for (let i = 0; i < options.length; i++) {
|
||||
process.stdout.write("\x1b[2K\r");
|
||||
const opt = options[i]!;
|
||||
const pointer = i === cursor ? `${CYAN}❯${RESET} ` : " ";
|
||||
const dot = i === cursor ? `${GREEN}◉${RESET}` : `${DIM}○${RESET}`;
|
||||
const name = i === cursor ? `${BOLD}${opt.label}${RESET}` : opt.label;
|
||||
const desc = i === cursor ? opt.desc : `${DIM}${opt.desc}${RESET}`;
|
||||
process.stdout.write(`${pointer} ${dot} ${name}${" ".repeat(Math.max(1, 10 - opt.label.length))}${desc}\n`);
|
||||
}
|
||||
process.stdout.write(`\x1b[${options.length}A`);
|
||||
}
|
||||
|
||||
function clearMenu() {
|
||||
process.stdout.write(`\x1b[${headerLines}A`);
|
||||
for (let i = 0; i < totalLines; i++) {
|
||||
process.stdout.write("\r\x1b[2K\n");
|
||||
}
|
||||
process.stdout.write(`\x1b[${totalLines}A`);
|
||||
}
|
||||
|
||||
if (!process.stdin.isTTY) {
|
||||
console.error(` ${RED}Error: Platform selection requires a TTY.${RESET}`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const savedRaw = process.stdin.isRaw;
|
||||
process.stdin.setRawMode(true);
|
||||
process.stdin.resume();
|
||||
process.stdout.write("\x1b[?25l");
|
||||
|
||||
render();
|
||||
|
||||
return new Promise((resolve) => {
|
||||
let escapeBuf = "";
|
||||
|
||||
function handleSeq(seq: string) {
|
||||
if (seq === "\x1b[A" || seq === "\x1bOA") {
|
||||
if (cursor > 0) {
|
||||
cursor--;
|
||||
render();
|
||||
}
|
||||
} else if (seq === "\x1b[B" || seq === "\x1bOB") {
|
||||
if (cursor < options.length - 1) {
|
||||
cursor++;
|
||||
render();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
process.stdin.on("data", (data: Buffer) => {
|
||||
const key = data.toString();
|
||||
|
||||
if (key === "\x03") {
|
||||
process.stdin.setRawMode(savedRaw === true);
|
||||
process.stdin.pause();
|
||||
process.stdin.removeAllListeners("data");
|
||||
clearMenu();
|
||||
process.stdout.write("\x1b[?25h");
|
||||
resolve(null);
|
||||
return;
|
||||
}
|
||||
|
||||
if (key === "\x1b" || key.startsWith("\x1b[")) {
|
||||
escapeBuf = key;
|
||||
if (key.length >= 3) {
|
||||
handleSeq(key);
|
||||
escapeBuf = "";
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (escapeBuf) {
|
||||
escapeBuf += key;
|
||||
if (/^[A-Za-z~]$/.test(key)) {
|
||||
handleSeq(escapeBuf);
|
||||
escapeBuf = "";
|
||||
} else if (escapeBuf.length > 8) {
|
||||
escapeBuf = "";
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (key === " " || key === "\r") {
|
||||
const selected = options[cursor]!;
|
||||
process.stdin.setRawMode(savedRaw === true);
|
||||
process.stdin.pause();
|
||||
process.stdin.removeAllListeners("data");
|
||||
|
||||
clearMenu();
|
||||
process.stdout.write("\x1b[?25h");
|
||||
|
||||
resolve(selected.platform);
|
||||
return;
|
||||
}
|
||||
});
|
||||
return selectOne({
|
||||
title: "Select remote platform",
|
||||
subtitle: `Remote ${hostname} could not be auto-detected`,
|
||||
items: [
|
||||
{ label: "GitHub", value: "github" as Platform, description: "Use gh CLI" },
|
||||
{ label: "Gitea", value: "gitea" as Platform, description: "Use tea CLI" },
|
||||
],
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user