From e1354e8651cffedd7c9e620b1f89c106b6520663 Mon Sep 17 00:00:00 2001 From: Mplan Date: Thu, 11 Jun 2026 19:51:33 +0800 Subject: [PATCH] feat(menu): add back key navigation --- README.md | 2 +- index.ts | 66 ++++++++++++++++++++++++++++++++----------------- src/menu.ts | 37 +++++++++++++++++++++------ src/selector.ts | 6 +++-- 4 files changed, 78 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index 7f27de8..f82e76c 100644 --- a/README.md +++ b/README.md @@ -84,7 +84,7 @@ $ gai commit Select files to stage: 2 unstaged files available - ↑/↓ navigate · space toggle · enter confirm · ctrl+c cancel + ↑/↓ navigate · space toggle · enter confirm · ←/backspace back · ctrl+c cancel ❯ □ Select all □ src/ai.ts modified diff --git a/index.ts b/index.ts index eeeae5a..83c1419 100644 --- a/index.ts +++ b/index.ts @@ -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 { 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 { +async function selectPlatform( + hostname: string, +): Promise { 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 { }); } -async function handleCommit(autoMode: boolean, dryRun: boolean): Promise { +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 { 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 { ); } 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 { 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 { 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 { : ` Aborted. Message: ${message}`, ); } + + return "done"; } -async function handlePR(draft: boolean): Promise { +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 { 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 { 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 { if (lower === "n") { console.log(" Aborted."); - return; + return "done"; } if (lower === "e") { @@ -600,7 +620,7 @@ async function handlePR(draft: boolean): Promise { 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 { ); process.exit(1); } + + return "done"; } async function main() { diff --git a/src/menu.ts b/src/menu.ts index 11fe07a..4d59295 100644 --- a/src/menu.ts +++ b/src/menu.ts @@ -2,11 +2,17 @@ import { BOLD, GREEN, CYAN, DIM, RESET } from "./terminal"; const UP = "\x1b[A"; const DOWN = "\x1b[B"; +const LEFT = "\x1b[D"; const ALT_UP = "\x1bOA"; const ALT_DOWN = "\x1bOB"; +const ALT_LEFT = "\x1bOD"; const SPACE = " "; const ENTER = "\r"; const CTRL_C = "\x03"; +const BACKSPACE = "\x7f"; + +export const BACK = Symbol("prompt-back"); +export type PromptBack = typeof BACK; export interface Choice { label: string; @@ -19,6 +25,7 @@ interface BasePromptOptions { title: string; subtitle?: string; cancelMessage?: string; + allowBack?: boolean; } interface SinglePromptOptions extends BasePromptOptions { @@ -55,11 +62,13 @@ function padLabel(label: string, width: number) { return label + " ".repeat(Math.max(1, width - visibleLength(label))); } -function controls(mode: "single" | "multi") { +function controls(mode: "single" | "multi", showBackHint = true) { if (mode === "single") { - return `${DIM}↑/↓ navigate · enter/space select · ctrl+c cancel${RESET}`; + const backHint = showBackHint ? " · ←/backspace back" : ""; + return `${DIM}↑/↓ navigate · enter/space select${backHint} · ctrl+c cancel${RESET}`; } - return `${DIM}↑/↓ navigate · space toggle · enter confirm · ctrl+c cancel${RESET}`; + const backHint = showBackHint ? " · ←/backspace back" : ""; + return `${DIM}↑/↓ navigate · space toggle · enter confirm${backHint} · ctrl+c cancel${RESET}`; } function renderPrompt(lines: string[], previousLines: number) { @@ -89,6 +98,9 @@ function clearPrompt(lines: number) { function normalizeKey(key: string, escapeBuf: string) { if (key === UP || key === ALT_UP) return { action: "up", escapeBuf: "" }; if (key === DOWN || key === ALT_DOWN) return { action: "down", escapeBuf: "" }; + if (key === LEFT || key === ALT_LEFT || key === BACKSPACE) { + return { action: "back", escapeBuf: "" }; + } if (key === SPACE) return { action: "space", escapeBuf: "" }; if (key === ENTER) return { action: "enter", escapeBuf: "" }; if (key === CTRL_C) return { action: "cancel", escapeBuf: "" }; @@ -101,6 +113,9 @@ function normalizeKey(key: string, escapeBuf: string) { const next = escapeBuf + key; if (next === UP || next === ALT_UP) return { action: "up", escapeBuf: "" }; if (next === DOWN || next === ALT_DOWN) return { action: "down", escapeBuf: "" }; + if (next === LEFT || next === ALT_LEFT) { + return { action: "back", escapeBuf: "" }; + } return { action: null, escapeBuf: /^[A-Za-z~]$/.test(key) || next.length > 8 ? "" : next, @@ -126,7 +141,7 @@ function createLines( ]; if (options.subtitle) lines.push(` ${DIM}${options.subtitle}${RESET}`); - lines.push(` ${controls(mode)}`, ""); + lines.push(` ${controls(mode, options.allowBack !== false)}`, ""); for (let i = 0; i < options.items.length; i++) { const item = options.items[i]!; @@ -156,7 +171,7 @@ function ensureTTY(title: string) { export async function selectOne( options: SinglePromptOptions, -): Promise { +): Promise { ensureTTY(options.title); let cursor = 0; @@ -178,7 +193,7 @@ export async function selectOne( render(); return new Promise((resolve) => { - const finish = (value: T | null) => { + const finish = (value: T | null | PromptBack) => { process.stdin.setRawMode(wasRaw === true); process.stdin.pause(); process.stdin.removeListener("data", onData); @@ -195,6 +210,9 @@ export async function selectOne( escapeBuf = result.escapeBuf; if (result.action === "cancel") return finish(null); + if (result.action === "back" && options.allowBack !== false) { + return finish(BACK); + } if (result.action === "up" && cursor > 0) { cursor--; render(); @@ -212,7 +230,7 @@ export async function selectOne( export async function selectMany( options: MultiPromptOptions, -): Promise { +): Promise { ensureTTY(options.title); const items: Choice[] = options.selectAllLabel @@ -267,7 +285,7 @@ export async function selectMany( render(); return new Promise((resolve) => { - const finish = (value: T[] | null) => { + const finish = (value: T[] | null | PromptBack) => { process.stdin.setRawMode(wasRaw === true); process.stdin.pause(); process.stdin.removeListener("data", onData); @@ -284,6 +302,9 @@ export async function selectMany( escapeBuf = result.escapeBuf; if (result.action === "cancel") return finish(null); + if (result.action === "back" && options.allowBack !== false) { + return finish(BACK); + } if (result.action === "up" && cursor > 0) { cursor--; render(); diff --git a/src/selector.ts b/src/selector.ts index 9702cc5..96019b1 100644 --- a/src/selector.ts +++ b/src/selector.ts @@ -1,11 +1,12 @@ import type { FileEntry } from "./types"; import { BOLD, GREEN, YELLOW, RESET } from "./terminal"; -import { selectMany } from "./menu"; +import { BACK, selectMany } from "./menu"; +import type { PromptBack } from "./menu"; export async function selectFiles( stagedFiles: FileEntry[], unstagedFiles: FileEntry[], -): Promise { +): Promise { if (unstagedFiles.length === 0) return []; if (stagedFiles.length > 0) { @@ -30,6 +31,7 @@ export async function selectFiles( }); if (selected === null) process.exit(1); + if (selected === BACK) return BACK; if (selected.length > 0) { process.stdout.write(