feat(menu): add back key navigation
Build / bun-build (push) Successful in 21s

This commit is contained in:
2026-06-11 19:51:33 +08:00
parent 7e662b25cc
commit e1354e8651
4 changed files with 78 additions and 33 deletions
+44 -22
View File
@@ -13,7 +13,8 @@ import {
commit,
} from "./src/git";
import { selectFiles } from "./src/selector";
import { selectOne } from "./src/menu";
import { BACK, selectOne } from "./src/menu";
import type { PromptBack } from "./src/menu";
import { collectProjectContext } from "./src/context";
import { buildPrompt, SYSTEM_PROMPT } from "./src/prompt";
import { generateCommitMessage } from "./src/ai";
@@ -304,22 +305,34 @@ async function showMenu(): Promise<void> {
process.exit(1);
}
const selected = await selectOne({
title: "gai",
subtitle: "Choose a workflow",
items: MENU_ACTIONS.map((action) => ({
label: action.label,
value: action.key,
description: action.description,
})),
});
while (true) {
const selected = await selectOne({
title: "gai",
subtitle: "Choose a workflow",
allowBack: false,
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();
if (selected === null || selected === BACK) return;
const result =
selected === "commit"
? await handleCommit(false, false)
: selected === "pr"
? await handlePR(false)
: await handleConfig().then(() => "done" as const);
if (result !== "back") return;
}
}
async function selectPlatform(hostname: string): Promise<Platform | null> {
async function selectPlatform(
hostname: string,
): Promise<Platform | null | PromptBack> {
if (!process.stdin.isTTY) {
console.error(` ${RED}Error: Platform selection requires a TTY.${RESET}`);
process.exit(1);
@@ -336,7 +349,10 @@ async function selectPlatform(hostname: string): Promise<Platform | null> {
});
}
async function handleCommit(autoMode: boolean, dryRun: boolean): Promise<void> {
async function handleCommit(
autoMode: boolean,
dryRun: boolean,
): Promise<"done" | "back"> {
const config = await loadConfig();
if (!config.apiKey) {
@@ -356,7 +372,7 @@ async function handleCommit(autoMode: boolean, dryRun: boolean): Promise<void> {
if (stagedFiles.length === 0 && unstagedFiles.length === 0) {
console.log(" Nothing to commit.");
return;
return "done";
}
if (unstagedFiles.length > 0) {
@@ -367,6 +383,7 @@ async function handleCommit(autoMode: boolean, dryRun: boolean): Promise<void> {
);
} else {
const selected = await selectFiles(stagedFiles, unstagedFiles);
if (selected === BACK) return "back";
if (selected.length > 0) {
await stageFiles(selected);
console.log(
@@ -379,7 +396,7 @@ async function handleCommit(autoMode: boolean, dryRun: boolean): Promise<void> {
const diff = await getStagedDiff();
if (!diff) {
console.log(" No staged changes to commit.");
return;
return "done";
}
const MAX_DIFF_SIZE = 15000;
@@ -417,7 +434,7 @@ async function handleCommit(autoMode: boolean, dryRun: boolean): Promise<void> {
console.log(` ${GREEN}${message}${RESET}`);
await copyToClipboard(message);
console.log(`\n ${DIM}(dry-run, message copied to clipboard)${RESET}`);
return;
return "done";
}
const action = await confirmCommit(message);
@@ -455,9 +472,11 @@ async function handleCommit(autoMode: boolean, dryRun: boolean): Promise<void> {
: ` Aborted. Message: ${message}`,
);
}
return "done";
}
async function handlePR(draft: boolean): Promise<void> {
async function handlePR(draft: boolean): Promise<"done" | "back"> {
const config = await loadConfig();
if (!config.apiKey) {
@@ -476,6 +495,7 @@ async function handlePR(draft: boolean): Promise<void> {
if (!platform) {
const hostname = (await getRemoteHostname()) || "unknown";
const chosen = await selectPlatform(hostname);
if (chosen === BACK) return "back";
if (!chosen) {
console.log(" Aborted.");
process.exit(0);
@@ -523,7 +543,7 @@ async function handlePR(draft: boolean): Promise<void> {
if (answer.toLowerCase() === "n") {
console.log(" Aborted.");
return;
return "done";
}
console.log(` Pushing ${CYAN}${branchName}${RESET}...`);
@@ -592,7 +612,7 @@ async function handlePR(draft: boolean): Promise<void> {
if (lower === "n") {
console.log(" Aborted.");
return;
return "done";
}
if (lower === "e") {
@@ -600,7 +620,7 @@ async function handlePR(draft: boolean): Promise<void> {
const newBody = await ask(" Body (optional): ");
if (!newTitle.trim()) {
console.log(" Aborted.");
return;
return "done";
}
title = newTitle;
body = newBody;
@@ -618,6 +638,8 @@ async function handlePR(draft: boolean): Promise<void> {
);
process.exit(1);
}
return "done";
}
async function main() {